-1

TicTacToe ゲームを作成していますが、ユーザーに移動を繰り返し求めます。9 回以上の選択を要求していますが、その理由がわかりません。

私のデザインには、ボードの現在の状態に関する情報を格納するための 2 次元配列があります。JOptionPane を使用して、ユーザーにボードの選択を求めます (左上は 1、上中央は 2、右上は 3、中央左は 4、等)。移動するたびに、現在のボードをコンソールに出力します。スポットが既に使用されている場合は、ユーザーに再度プロンプトを表示します。(勝者チェックは、別の機会に行うように言われたので、必要ありません。

ここに私のコード全体があります:

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 ステートメントの 1 つを次のようにする必要があります。

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 に答える