TicTacToe 的玩法 java

Play method for TicTacToe java

所以我们得到了一个练习,需要写一个 tictactoe class,我们需要使用一个 2d int 数组作为棋盘和 2 个玩家,其中一个有一个“1”作为 "X" 而另一个是“2”作为 "O",“0”表示空字段。现在我们需要为玩家编写一个方法来实际在场上设置一些东西,但我能想到的只是与字符串或字符板有关的东西,而与 int 板无关。一个int板上的一个数字的问题这个设置是怎么实现的呢?感谢您的帮助!

我已经有了一个方法来检查板上是否有任何空闲位置,这应该是正确的。

public class TicTacToe extends BasicBoard implements Game {

    int PlayerA = 1;
    int PlayerB = 2;
    int empty = 0;
    private int row, column;
    private int[][] board = new int[column][row];


    public boolean isboardfull(int row, int column) {
        for (int i = 0; i <= column; i++)
        {
            for (int j = 0; j <= row; j++)
            {
                if (board[i][j] == PlayerA || board[i][j] == PlayerB)
                    return true;
            }
        }
        return false;
     }
     public  void playerturn(){
        if (!isboardfull(row, column))

     }
}

您的 TicTacToe class 从 BasicBoardGame class 扩展而来,但您没有提供它们。我假设这些 classes 为您提供了渲染棋盘和控制游戏演变的方法,但由于我们没有它们,所以我在我的示例中包含了类似(和简单)的东西(以演示它是如何工作的)。如果 gameprintBoardresetBoardendGame 方法由 BasicBoardGame [=61= 提供,您可以跳过这些方法]es.

这是我所做的一系列假设:

  • 玩家被问到玩的坐标
  • 棋盘满时游戏结束。一个更复杂的版本应该检查游戏的每次迭代是否有玩家获胜。

这里是我的方法的一般解释:

  • X 和 O 到 1 和 2 之间的映射设置为 static constant,因为这永远不会改变。
  • 行数和列数可能因执行和 TicTacToe 构造函数中的参数化而异。
  • 玩家根据提示通过标准输入填写信息。
  • game 函数要求玩家移动并渲染棋盘(在标准输出上),直到棋盘完全填满。
  • isBoardFull函数检查棋盘上是否有空位。因此,如果我们找到一个空槽,我们就知道它没有满,否则我们需要继续寻找空槽。如果我们搜索所有板并且没有空槽,那么它是满的。 (在我看来,这部分在您提供的代码中写错了)
  • playerTurn 函数询问玩家想要玩的坐标并填充棋盘。为此,我们扫描标准输入的 2 行,将它们转换为 int 并检查该位置是否为空且在边界内。如果是这样,我们用玩家编号标记位置。

代码:

public class TicTacToe {

    private static final int PLAYER_A = 1;
    private static final int PLAYER_B = 2;
    private static final int EMPTY = 0;

    private final int numRows;
    private final int numColumns;
    private final int[][] board;

    private final Scanner inputScanner;


    public TicTacToe(int numRows, int numColumns) {
        // Retrieve board sizes
        this.numRows = numRows;
        this.numColumns = numColumns;

        // Instantiate board
        this.board = new int[numRows][numColumns];

        // Initialize board
        resetBoard();

        // Initialize the input scanner (for player choices)
        this.inputScanner = new Scanner(System.in);
    }

    public void game() {
        // Initialize the game
        int numMoves = 0;
        printBoard(numMoves);

        // Play until the game is over
        while (!isBoardFull() && !hasPlayerWon()) {
            // A or B player should move
            int currentPlayer = (numMoves % 2 == 0) ? PLAYER_A : PLAYER_B;
            playerTurn(currentPlayer);

            // We increase the number of moves
            numMoves += 1;

            // We render the board
            printBoard(numMoves);
        }

        // Check winner and close game
        endGame();
    }

    private void resetBoard() {
        for (int i = 0; i < this.numRows; ++i) {
            for (int j = 0; j < this.numColumns; ++j) {
                this.board[i][j] = EMPTY;
            }
        }
    }

    private void printBoard(int currentMove) {
        System.out.println("Move: " + currentMove);
        for (int i = 0; i < this.numRows; ++i) {
            for (int j = 0; j < this.numColumns; ++j) {
                System.out.print(this.board[i][j] + " ");
            }
            System.out.println();
        }

        // A new line to split moves
        System.out.println();
    }

    private boolean isBoardFull() {
        for (int i = 0; i < this.numRows; ++i) {
            for (int j = 0; j < this.numColumns; ++j) {
                if (this.board[i][j] == EMPTY) {
                    // If there is an empty cell, the board is not full
                    return false;
                }
            }
        }
        // If there are no empty cells, the board is full
        return true;
    }

    private boolean hasPlayerWon() {
        // TODO: Return whether a player has won the game or not
        return false;
    }

    private void playerTurn(int currentPlayer) {
        // Log player information
        System.out.println("Turn for player: " + currentPlayer);

        // Ask the player to pick a position
        boolean validPosition = false;
        while (!validPosition) {
            // Ask for X position
            int posX = askForPosition("row", this.numRows);
            // Ask for Y position
            int posY = askForPosition("column", this.numColumns);

            // Check position
            if (posX >= 0 && posX < this.numRows) {
                if (posY >= 0 && posY < this.numColumns) {
                    if (this.board[posX][posY] == EMPTY) {
                        // Mark as valid
                        validPosition = true;
                        // Mark the position
                        this.board[posX][posY] = currentPlayer;
                    } else {
                        System.out.println("Position is not empty. Please choose another one.");
                    }
                } else {
                    System.out.println("Column position is not within bounds. Please choose another one.");
                }
            } else {
                System.out.println("Row position is not within bounds. Please choose another one.");
            }
        }

    }

    private int askForPosition(String rc, int dimensionLimit) {
        System.out.println("Select a " + rc + " position between 0 and " + dimensionLimit);

        return Integer.valueOf(this.inputScanner.nextLine());
    }

    private void endGame() {
        // Close input scanner
        this.inputScanner.close();

        // Log game end
        System.out.println("GAME ENDED!");

        // TODO: Check the board status
        System.out.println("Draw");
    }

    public static void main(String[] args) {
        TicTacToe ttt = new TicTacToe(3, 4);
        ttt.game();
    }

}

示例输出:

Move: 0
0 0 0 0 
0 0 0 0 
0 0 0 0 

Turn for player: 1
Select a row position between 0 and 3
4
Select a column position between 0 and 4
1
Row position is not within bounds. Please choose another one.
Select a row position between 0 and 3
1
Select a column position between 0 and 4
1
Move: 1
0 0 0 0 
0 1 0 0 
0 0 0 0 

Turn for player: 2
.
.
.
Move: 12
2 2 1 2 
1 1 2 1 
1 2 1 2 

GAME ENDED!
Draw