0

このコードを実行すると、ゲームは勝者または引き分けを宣言することはなく、次のプレイヤーの動きを継続的に求めます...

出力例:

Choose a position to play
1 | 2 | 3
---------
4 | 5 | 6
---------
7 | 8 | 9
Player X (Enter a position or -1 to resign): 1

Choose a position to play.
1 | 2 | 3
---------
4 | 5 | 6
---------
7 | 8 | 9
Player O (Enter a position or -1 to resign): 1

スペースの数をキャラクターに変更したり、勝者を宣言するために覚えたりすることはありません。

これは私のコードの runStandardGame() 部分です。問題はそこにあると思いますか?

//***RUN STANDARD GAME******************************************************

static void runStandardGame() {

    int position = QUIT;
    char playerChar = 'X';
    boolean isGameOver = false;

    fillPosition();

    System.out.println("Choose a position to play.\n");


    displayGrid();

    do {
        System.out.print("Player " + playerChar +
                         " (Enter a position or " + QUIT + " to resign): ");
        position = keyboard.nextInt();

        System.out.println("Choose a position to play.\n");

        displayGrid();


        if(isWin()) {
            System.out.print("\nPlayer " + playerChar + " WINS!!!\n");
            isGameOver = true;
        }
        else if (isTie()) {
            System.out.print("\nTIE.\n");
            isGameOver = true;
        }
        else {
            //switch players because one of the players has just played
            //and not won;, so, it is the other player's turn
            if (playerChar == 'X') {
                playerChar = 'O';
            }
            else{
                playerChar = 'X';
            }
        }
    }while(!isGameOver && position != QUIT);

}//end of runStandardGame

または、配列への割り当てが行われる場所であるため、再生部分だけである可能性もあります...

//***PLAY*******************************************************************    

static void play(int cell, char playerChar) {
    //the player has entered a number 1 through 9 for the position they want
    //to play, so, figure out which row and which column of the array this is
    //For example, if the player wants to play 'X' in position 7, this means
    //the 'X' must go into row 2, column 0 of the array
    int row = 0;
    int column = 0;

    if (cell > 0 && cell <= (STANDARD_GRID_ROWS * STANDARD_GRID_COLUMNS)){
        row = (cell - 1) / STANDARD_GRID_ROWS;
        column = (cell - 1) % STANDARD_GRID_COLUMNS;
        grid[row][column] = playerChar;
    }
}

どんな助けでも大歓迎です。ありがとうございました。

4

2 に答える 2

1

私にはそのように思えます

position = keyboard.nextInt();

表示して続行する前に、位置を処理する (ボードに書き込む) 必要がありますか?

于 2012-11-21T00:33:38.173 に答える
1

ユーザー入力を取得しているようです

position = keyboard.nextInt();

しかし、実際に呼び出しでそれを使用しているとは思いません

play(position, playerChar);
于 2012-11-21T00:39:59.073 に答える