-1
if (count % 2 == 0) {

    int columnMove;
    System.out.print("Player one, please enter a move or q to quit: ");
    columnMove = scan.nextInt() - 1;

    if (columnMove <= columns - 1 && columnMove >= 0) {
        turns = true;
        game.playerOnePrompt(board, columns, rows, columnMove);
        ++count;
        if (board[0][columnMove] != "_") {
            System.out.println("This column is full");
            count = 0;
        }
    } else {
        System.out.println("Invalid move");
        count = 0;
    }

} else {

    int columnMove;
    System.out.print("Player two, please enter a move: ");
    columnMove = scan.nextInt() - 1;

    if (columnMove <= columns - 1 && columnMove >= 0) {
        turns = true;
        game.playerTwoPrompt(board, columns, rows, columnMove);
        count++;
        if (board[0][columnMove] != "_") {
            System.out.println("This column is full");
            count = 1;
        }
    } else {
        System.out.println("Invalid move");
        count = 1;
    }
}

こんにちは!上記は、配列 (列) がいっぱいかどうかを判断するコードです。いっぱいになっている場合、ユーザーは別の動きをするように求められます。

ただし、プログラムがいっぱいであることを認識し、ユーザーにプロンプ​​トを表示し、ユーザーが有効な動きをした後、プログラムがプレーヤーをシフトしないという問題があります (プレーヤー 1 - 2 - 1 - 2 などから)。

何かアドバイス?

4

2 に答える 2

0
    maximumNumberOfMoves = boardWidth * boardHeight;
    if (count == maximumNumberOfMoves) {
        // end game
    } else if (count % 2 == 0) {
        int columnMove;
        System.out.print("Player one, please enter a move or q to quit: ");
        columnMove = scan.nextInt() - 1;
        if (columnMove <= columns - 1 && columnMove >= 0) {
            turns = true;
            game.playerOnePrompt(board, columns, rows, columnMove);
            ++count;
            if (board[0][columnMove] != "_") {
                System.out.println("This column is full");
                --count; // We must decrement count if we want the same
                            // player to move again
            }
        } else {
            System.out.println("Invalid move");
            // count = 0; //Remove this, count was not modified
        }

    } else {
        int columnMove;
        System.out.print("Player two, please enter a move: ");
        columnMove = scan.nextInt() - 1;
        if (columnMove <= columns - 1 && columnMove >= 0) {
            turns = true;
            game.playerTwoPrompt(board, columns, rows, columnMove);
            count++;
            if (board[0][columnMove] != "_") {
                System.out.println("This column is full");
                --count; // We must decrement count if we want the same
                            // player to move again
            }
        } else {
            System.out.println("Invalid move");
            // count = 1; //Again, remove this since count is not modified
        }
    }

固定コードのコメント行に注意してください

この回答では、カウントがこれまでに行われた移動の数を表し、0 から始まると想定しています。

于 2013-09-30T09:24:34.630 に答える