-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
        }
    }

注意固定代码的注释行

这个答案假设 count 表示到目前为止所做的移动次数,并且从 0 开始。

于 2013-09-30T09:24:34.630 回答
0

你的问题在这里:

if (board[0][columnMove] != "_")
    {
        System.out.println("This column is full");
        count = 0;
    }

只要移动有效,您总是将计数设置为零。

于 2013-09-30T09:09:18.640 回答