TicTacToe java的播放方法

问题描述 投票:-2回答:1

所以我们有一项运动,需要编写一个tictactoe类,我们需要使用一个2d int数组作为棋盘和2个播放器,一个作为“ X”使用一个“ 1”,另一个作为“ 2”使用一个作为“ O”,“ 0”代表空白字段。现在我们需要编写一种方法让玩家在场上实际设置一些东西,但是我所能想到的只是与字符串或字符板有关的东西,而与整数板无关。您如何通过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))

     }
}
java tic-tac-toe
1个回答
1
投票

您的TicTacToe类是从BasicBoardGame类扩展的,但您没有提供它们。我假设这些类为您提供了渲染棋盘并控制游戏发展的方法,但是由于我们没有它们,因此我在示例中包含了类似(简单)的内容(以演示其工作原理)。如果gameprintBoard类提供了这些方法,则可以跳过resetBoardendGameBasicBoardGame方法。

以下是我所做的一系列假设:

  • 要求玩家提供比赛地点的坐标
  • 游戏在棋盘已满时结束。一个更复杂的版本应检查游戏的每个迭代,如果其中一位玩家获胜。

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

  • [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
© www.soinside.com 2019 - 2024. All rights reserved.