-1

我正在写一个井字游戏,它反复提示用户移动。它要求选择超过 9 次,我不知道为什么。

我的设计有一个二维数组来存储有关电路板当前状态的信息,使用 JOptionPane 询问用户选择电路板(1 代表左上角,2 代表中上角,3 代表右上角,4 代表左中角, ETC)。每次移动后,将当前板子输出到控制台。如果某个点已被使用,请再次提示用户。(不需要赢家支票,因为我们被告知要再做一次。

这是我的整个代码:

import javax.swing.JOptionPane;

// Basic TicTacToe game.
public class TicTacToe1 {
    public static void main(String[] args){
        // Hello there!
        Object[] options = { "I'm ready to play!",};
        Object[] options2 = { "Select another number.",};
        JOptionPane.showOptionDialog(null,"To play TicTacToe, use the numbers 1 to 9 to choose where you place a mark. Are you ready?","TicTacToe1",
                JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
        // Define the board.
        char board[][] = new char[3][3];
        // Zero out the board.
        for(int a=0; a<3; a++) {
            for(int b=0; b<3; b++) {
                board[a][b] = ' ';
            }
        }
        // Print an initial, clean board.
        print(board);
        // Use a for loop to ask for the selection and nest the selector into it.
        for(int i=1; i<10 ; i++) {
            if ((i%2) == 1) {
                boolean goahead = true;
                while (goahead) {
                    int selection = Integer.parseInt(JOptionPane.showInputDialog(null,"Where do you want to place an X?"));
                    if ((board[(((selection-1)-((selection-1)%3))/3)][(selection-1)%3]=='X') || (board[(((selection-1)-((selection-1)%3))/3)][(selection-1)%3]=='O')) {
                        JOptionPane.showOptionDialog(null,"I'm sorry, the number "+selection+" spot is already being used.","TicTacToe1",
                                JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options2, options2[0]);
                    } else {
                        goahead = false;
                        board[(((selection-1)-((selection-1)%3))/3)][(selection-1)%3] = 'X';
                        print(board);
                    }
                }
            }
            if ((i%2) == 1) {
                boolean goahead = true;
                while (goahead) {
                    int selection = Integer.parseInt(JOptionPane.showInputDialog(null,"Where do you want to place an O?"));
                    if ((board[(((selection-1)-((selection-1)%3))/3)][(selection-1)%3]=='X') || (board[(((selection-1)-((selection-1)%3))/3)][(selection-1)%3]=='O')) {
                        JOptionPane.showOptionDialog(null,"I'm sorry, the number "+selection+" spot is already being used.","TicTacToe1",
                                JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options2, options2[0]);
                    } else {
                        goahead = false;
                        board[(((selection-1)-((selection-1)%3))/3)][(selection-1)%3] = 'O';
                        print(board);
                    }
                }
            }
//          didsomeonewinyet(board);
        }
    }

    // Make a helper function named print to print the board
    public static void print(char board[][]){
        for(int a=0; a<3; a++) {
            for(int b=0; b<3; b++) {
                System.out.print("|"+board[a][b]+"|");
            }
            System.out.println();
        }
        System.out.println();
    }
    // Winner checker.
//  public static void didsomeonewinyet(char board[][]){
        // Manually checking will yield 16 cases (2 diagonal, 3 horizontal, 3 vertical, either X or O), which is not that bad.
//  }
}

为什么它要求选择太多次?

4

1 回答 1

5

您在 for 循环中的 if 条件是相同的,因此它们都发生在循环的同一迭代中:

if ((i%2) == 1) {

if 语句之一应该是:

if ((i%2) == 0) {

取决于你想先去谁。

或者,您可以只使用 else 语句:

if ((i%2) == 1) {
 //code for x to go

}else {
//code for y to go
}
于 2013-07-17T18:26:00.523 回答