TicTacToe计划的问题

时间:2018-03-05 05:06:07

标签: java

所以我已经在TicTacToe计划中取得了这么远,并且终端说无法找到符号"我"和" j"。从这一点来看,我很困惑。类和类客户端将无法编译,我必须声明" i"和" j"。对我的问题的任何帮助都将非常感激。

public class TicTacToe {
private char[][] board;
private String xPlayer;
private String oPlayer;
private boolean xturn;
private int plays;

public TicTacToe(Player X, Player O) {
    this("Player X", "Player O", true);
}

public TicTacToe(String xPlayer, String oPlayer, boolean xturn) {
    this.xPlayer = xPlayer;
    this.oPlayer = oPlayer;
    this.xturn = xturn;
    this.plays = 0;
    this.board = new char[3][3];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            this.board[i][j] = '-';
        }
    }
}

public boolean isXturn() {
    return xturn;
}

public boolean play(int row, int col) {
    this.makeMove(row, col);
    if (this.makeMove(row, col)) {
        plays = plays + 1;
        if (xturn) {
            xturn = false;
        } else {
            xturn = true;
        }
    }
    if (this.isGameOver() == 'x') {
        System.out.println("X won.");
        return true;
    } else if (this.isGameOver() == 'o') {
        System.out.println("O won.");
        return true;
    } else if (this.isGameOver() == 't') {
        System.out.println("Tie");
        return true;
    } else {
        return false;
    }

}

public boolean makeMove(int row, int col) {
    if (plays == 9) {
        System.out.println("The board is already completely filled. ");
        return false;
    }
    if (row < 0 || row > 2 || col < 0 || col > 2) {
        System.out.println(" The coordinate (x, y) is out of range.");
        return false;
    }
    if (this.board[row][col] != '-') {
        System.out.println("The coordinate (x, y) is already taken.");
        return false;
    } else {
        if (xturn == true) {
            this.board[row][col] = 'X';
        } else {
            this.board[row][col] = 'O';
        }
        return true;
    }

}

public char isGameOver() {
    boolean winner = true;
    for (i = 0; i < 3; i++) {
        if ((this.board[i][0] == this.board[i][1]) == (this.board[i][0] == this.board[i][2])) {
            return this.board[i][0];
        }
    }
    for (j = 0; j < 3; j++) {
        if ((this.board[0][j] == this.board[1][j]) == (this.board[0][j] == this.board[2][j])) {
            return this.board[0][j];
        }
    }
    if ((this.board[0][0] == this.board[1][1]) == (this.board[0][0] == this.board[2][2])) {
        return this.board[0][0];
    }
    if ((this.board[0][2] == this.board[1][1]) == (this.board[0][0] == this.board[2][0])) {
        return this.board[0][2];
    }
}

public String toString( ){
    String ans = " ";
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            System.out.println( this.board[i][j]);
}
    }
    return ans;
}
}

与TicTacToe的客户一起。

    public class TicTacToeClient {
    public static void main(String[] args) {
    TicTacToe ttt = new TicTacToe();
    System.out.println("Below should be an empty board: ");
    System.out.println(ttt);

    System.out.println("Testing diagonal win (X should win): ");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            boolean retVal = ttt.play(i, j);
            if (retVal)
                break;
        }
    }
    System.out.println("\n" + ttt);

    System.out.println("Testing other diagonal win (X should win): ");
    ttt = new TicTacToe();
    for (int i = 0; i < 3; i++) {
        for (int j = 2; j >= 0; j--) {
            boolean retVal = ttt.play(i, j);
            if (retVal)
                break;
        }
    }
    System.out.println("\n" + ttt);

    System.out.println("Testing row 1 win (X should win): ");
    ttt = new TicTacToe();
    ttt.play(1, 0);
    ttt.play(0, 1);
    ttt.play(1, 1);
    ttt.play(0, 2);
    ttt.play(1, 2);
    System.out.println(ttt);

    System.out.println("Testing row 1 win (O should win): ");
    ttt = new TicTacToe("Player X", "Player O", false);
    ttt.play(1, 0);
    ttt.play(0, 1);
    ttt.play(1, 1);
    ttt.play(0, 2);

    ttt.play(0, 0);
    ttt.play(2, 2);
    ttt.play(1, 2);
    System.out.println(ttt);

    System.out.println("Tie game: ");
    ttt = new TicTacToe("Arthur", "Ford", false);
    ttt.play(1, 1);
    ttt.play(2, 2);
    ttt.play(1, 2);
    ttt.play(1, 0);
    ttt.play(2, 0);
    ttt.play(0, 2);
    ttt.play(0, 1);
    ttt.play(2, 1);
    ttt.play(0, 0);
    System.out.println(ttt);

    System.out.println("Testing making a move after 9 plays: ");
    ttt.play(0, 1);
    System.out.println();

    System.out.println("Testing col 1 win (Arthur should win): ");
    ttt = new TicTacToe("Arthur", "Ford", true);
    ttt.play(1, 1);
    ttt.play(2, 2);
    ttt.play(0, 1);
    ttt.play(2, 0);
    ttt.play(2, 1);
    System.out.println(ttt);

    System.out.println("Testing playing the same position (it should be O's turn again): ");
    ttt = new TicTacToe();
    ttt.play(0, 0);
    ttt.play(0, 0);
    if (ttt.isXturn()) {
        System.out.println("Incorrect, it should be O's turn.");
    } else {
        System.out.println("It is O's turn!");
    }

    System.out.println();
    System.out.println("Testing the out of range error (it should be O's turn again): ");
    ttt = new TicTacToe();
    ttt.play(0, 0);
    ttt.play(-1, 0);
    ttt.play(0, -1);
    ttt.play(3, 0);
    ttt.play(0, 3);
    if (ttt.isXturn()) {
        System.out.println("Incorrect, it should be O's turn.");
    } else {
        System.out.println("It is O's turn!");
    }
    }
    }

1 个答案:

答案 0 :(得分:0)

在TicTacToe类的isGameOver()方法中,您在第一个循环中使用了i,在第二个循环中使用了j,两者都在声明之前使用。解决这个问题,这个问题将得到解决。

public char isGameOver() {
boolean winner = true;
for (int i = 0; i < 3; i++) {
    if ((this.board[i][0] == this.board[i][1]) == (this.board[i][0] == this.board[i][2])) {
        return this.board[i][0];
    }
}
for (int j = 0; j < 3; j++) {
    if ((this.board[0][j] == this.board[1][j]) == (this.board[0][j] == this.board[2][j])) {
        return this.board[0][j];
    }
}
if ((this.board[0][0] == this.board[1][1]) == (this.board[0][0] == this.board[2][2])) {
    return this.board[0][0];
}
if ((this.board[0][2] == this.board[1][1]) == (this.board[0][0] == this.board[2][0])) {
    return this.board[0][2];
}

}