当我运行我的程序时,没有任何显示

时间:2016-12-13 20:42:54

标签: java eclipse

我只是Java oop的初学者,我真的需要帮助,我想做一个Tic-Tac-Toe游戏。 没有错误,但它没有向我显示任何内容。 控制台工作正常,因为我尝试了别的东西,但我真的不知道问题出在哪里。 对不起,如果我在英语中有任何错误,那我的母语不是。

谢谢。

package tictacto;

public class Jeu {

    public static void main(String[] args) {
        Board grille = new Board();
        Joueur player = new Joueur("cc");
        boolean test;
        //char c;
        int i = 0;
        int[] tab;

        test = grille.checkBoard();
        System.out.println(test);
        player.choixSigne();
        //c = player.getSigne();
        while ((test == false) && (i < 9)) {
            grille.afficheBoard();
            player.seDeplacer();
            tab = player.getMove();
            grille.InsererMouvement(tab);
            i++;
            test = grille.checkBoard();
        }

    }

}

...

package tictacto;

import java.util.Scanner;

public class Joueur {
    // attributes 

    protected final String pseudo;
    protected int score;
    private char signe;
    private int[] move = new int[2];
    // méthodes
    Scanner sc = new Scanner(System.in);

    Joueur(String psd) {
        this.signe = ' ';
        this.pseudo = psd;
        this.score = 0;
        this.move[0] = 0;
        this.move[1] = 0;
    }

    public void choixSigne() {
        String s = new String();
        boolean test = false;
        while (test == false) {
            System.out.println("veuillez choisir votre signe");
            String sgne = sc.nextLine();
            char c = sgne.charAt(0);
            s = c + "";
            if (s.equals('X') || s.equals('O')) {
                test = true;
                System.out.println("Le Choix de signe est effectué avec succes ");
                signe = s.charAt(0);
            } else {
                test = false;
                System.out.println("Veuillez refaire votre choix");
            }
        }

    }

    public char getSigne() {
        return signe;
    }

    public int[] getMove() {
        return move;
    }

    public void seDeplacer() {
        System.out.println("Veuillez choisir la ligne");
        int a = sc.nextInt();
        move[0] = a;
        System.out.println("Veuillez choisir la colonne");
        int b = sc.nextInt();
        move[1] = b;

    }

}

...

package tictacto;

class Board {

    public char[][] Board = new char[3][3];
    protected Joueur J;

    Board() {
        for (int k = 0; k <= 2; k++) {
            for (int j = 0; j <= 2; j++) {
                Board[k][j] = ' ';
            }
        }

    }

    public void afficheBoard() {

        System.out.println("\t The Board");
        System.out.print("\n");
        for (int i = 0; i <= 2; i++) {

            System.out.print("\t |");
            System.out.print(Board[0][i]);
            System.out.print("|");
            System.out.print(Board[1][i]);
            System.out.print("|");
            System.out.print(Board[2][i]);
            System.out.print("|");
            System.out.print("\n");

        }
    }

    // Ajouter un mouvement à la grille
    public boolean InsererMouvement(int[] tab) {
        if (Board[tab[0]][tab[1]] == ' ') {
            Board[tab[0]][tab[1]] = J.getSigne();
            System.out.println("Next move");
            return (true);
        } else {
            System.out.println("Try again");
        }
        return false;
    }

    public char[][] getBoard() {
        return Board;
    }

    // Check Board
    public boolean checkBoard() {
        boolean a = false;
        boolean b = false;
        // vérifier les lignes
        while (a == false) {
            for (int i = 0; i <= 2; i++) {
                if ((Board[i][0] == 'X') && (Board[i][1] == 'X') && (Board[i][2] == 'X') || (Board[i][0] == 'O') && (Board[i][1] == 'O') && (Board[i][2] == 'O')
                        || ((Board[0][i] == 'X') && (Board[1][i] == 'X') && (Board[2][i] == 'X')
                        || (Board[0][i] == 'O') && (Board[1][i] == 'O') && (Board[2][i] == 'O'))) {
                    a = true;
                } else {
                    a = false;
                }
            }
        }
        // vérifier les diagonales

        if ((Board[0][0] == 'X') && (Board[1][1] == 'X') && (Board[2][2] == 'X')
                || (Board[0][0] == 'O') && (Board[1][1] == 'O') && (Board[2][2] == 'O')
                || (Board[2][0] == 'X') && (Board[1][1] == 'X') && (Board[0][2] == 'X')
                || (Board[2][0] == 'O') && (Board[1][1] == 'O') && (Board[0][2] == 'O')) {
            b = true;
        } else {
            b = false;
        }
        if (a || b) {
            return (true);
        } else {
            return (false);
        }
    }

}

1 个答案:

答案 0 :(得分:1)

你的程序中似乎有很多错误。精确定位它们在这里有很多工作要做。因此,我建议您观察我为解决问题所做的更改。您也可以改进它以使您的计划更好。

package tictacto;

public class Jeu {

    public static void main(String[] args) {
        Board grille = new Board();
        Joueur player = new Joueur("cc");
        boolean test = false;
        //char c;
        int i = 0;
        int[] tab;

        //test = grille.checkBoard();
        System.out.println(test);
        player.choixSigne();
        //c = player.getSigne();
        while ((test == false) && (i < 9)) {
            grille.afficheBoard();
            player.seDeplacer();
            tab = player.getMove();
            grille.InsererMouvement(tab,player);
            i++;
            test = grille.checkBoard();
        }

    }

}

...

package tictacto;

class Board {

    public char[][] Board = new char[3][3];
    //protected Joueur J = new Joueur("cc");

    Board() {
        for (int k = 0; k <= 2; k++) {
            for (int j = 0; j <= 2; j++) {
                Board[k][j] = ' ';
            }
        }

    }

    public void afficheBoard() {

        System.out.println("\t The Board");
        System.out.print("\n");
        for (int i = 0; i <= 2; i++) {

            System.out.print("\t |");
            System.out.print(Board[0][i]);
            System.out.print("|");
            System.out.print(Board[1][i]);
            System.out.print("|");
            System.out.print(Board[2][i]);
            System.out.print("|");
            System.out.print("\n");

        }
    }

    // Ajouter un mouvement à la grille
    public boolean InsererMouvement(int[] tab,Joueur J) {
        if (Board[tab[0]][tab[1]] == ' ') {
            Board[tab[0]][tab[1]] = J.getSigne();

            System.out.println("Next move");
            return true;
        } else {
            System.out.println("Try again");
        }
        return false;
    }

    public char[][] getBoard() {
        return Board;
    }

    // Check Board
    public boolean checkBoard() {
        boolean a = false;
        boolean b = false;
        // vérifier les lignes
        for (int i = 0; i <= 2; i++) {
            if ((Board[i][0] == 'X') && (Board[i][1] == 'X') && (Board[i][2] == 'X') || (Board[i][0] == 'O') && (Board[i][1] == 'O') && (Board[i][2] == 'O')
                    || ((Board[0][i] == 'X') && (Board[1][i] == 'X') && (Board[2][i] == 'X')
                    || (Board[0][i] == 'O') && (Board[1][i] == 'O') && (Board[2][i] == 'O'))) {

                a = true;
            } else {
                a = false;
            }
        }

        // vérifier les diagonales
        if ((Board[0][0] == 'X') && (Board[1][1] == 'X') && (Board[2][2] == 'X')
                || (Board[0][0] == 'O') && (Board[1][1] == 'O') && (Board[2][2] == 'O')
                || (Board[2][0] == 'X') && (Board[1][1] == 'X') && (Board[0][2] == 'X')
                || (Board[2][0] == 'O') && (Board[1][1] == 'O') && (Board[0][2] == 'O')) {

            b = true;
        } else {
            b = false;
        }

        if (a || b) {
            return (true);
        } else {
            return (false);
        }
    }

}

...

package tictacto;

import java.util.Scanner;

public class Joueur {
    // attributes 

    protected final String pseudo;
    protected int score;
    private char signe;
    private int[] move = new int[2];
    // méthodes
    Scanner sc = new Scanner(System.in);

    Joueur(String psd) {
        this.signe = ' ';
        this.pseudo = psd;
        this.score = 0;
        this.move[0] = 0;
        this.move[1] = 0;
    }

    public void choixSigne() {
        String s = new String();
        boolean test = false;
        while (test == false) {
            System.out.println("veuillez choisir votre signe");
            String sgne = sc.nextLine();
            char c = sgne.charAt(0);
            s = c + "";
            if (s.equals("X") || s.equals("O")) {
                test = true;
                System.out.println("Le Choix de signe est effectué avec succes ");
                signe = s.charAt(0);
            } else {
                test = false;
                System.out.println("Veuillez refaire votre choix");
            }
        }

    }

    public char getSigne() {
        return signe;
    }

    public int[] getMove() {
        return move;
    }

    public void seDeplacer() {
        System.out.println("Veuillez choisir la ligne");
        int a = sc.nextInt();
        move[0] = a;
        System.out.println("Veuillez choisir la colonne");
        int b = sc.nextInt();
        move[1] = b;

    }

}