java - 井字游戏问题

标签 java multidimensional-array do-while

我被井字棋问题困扰了。定义一个名为 TicTacToe 的类。 TicTacToe 类型的对象是 TicTacToe 的单个游戏。将游戏板存储为具有三行三列的基本类型 char 的单个二维数组。包括添加一步、显示棋盘、告知轮到谁、告知是否有获胜者、说出谁是获胜者以及重新开始游戏的方法。为该类编写一个主要方法,该方法将允许两个玩家在同一个键盘上轮流输入他们的 Action 。

我已经编写了一些方法,并且一直在进行测试。当我测试我的代码时,我要么让它放置一个标记,但也打印出无效的条目,或者它会不断循环询问移动,然后说空间已被占用。我不知道如何解决这个问题。我确信这与我的 do while 循环以及 isEmpty 和 notValid 的 boolean 方法有关。此外,我还困惑于如何为每个玩家的胜利实现计数器。

这是我的代码:

    public void addMove()
    {
        checkTurn();
        int row, col;
        do
        {
        System.out.println("Enter a row (1-3): ");
        row = in.nextInt() - 1; //Array index starts at 0.
        System.out.println("Enter a column (1-3): ");
        col = in.nextInt() - 1;

        if (row>=0 && row<ROWS)
            if(col>=0 && col<COLUMNS)
                if (playerX)
                    {
                        gameBoard[row][col] = player1Move;
                    }
                    else 
                    {
                        gameBoard[row][col] = player2Move;
                    }
         checkForWin();
         changePlayer();
        }while (notValid(row,col));

            System.out.println("Invaild Entry.");
            //System.exit(0); 

        //checkForWin();
        //changePlayer();
    }
    public boolean notValid(int row, int col)
    {
        if (row < 0 || row > ROWS )
            return true;
        if (col < 0 || col > COLUMNS)
            return true;
        if (!isEmpty(row,col))
            return true;
        return false;
    }
    public boolean isEmpty(int row, int col)
    {
        if(gameBoard[row][col]==' ')
            return true;
        else
        {
            System.out.println("Space is already occupied.");
            return false;
        }
    }
}   

这是我的测试类(class):

public class TicTacToe
{

    public static void main(String[] args) 
    {
    TicTacToeClass game = new TicTacToeClass();
    game.addMove();
    game.printBoard();


    }

}

最佳答案

我会让你处理多游戏部分。这将玩一局游戏并退出。

import java.util.Scanner;
public class TicTacToe
{
    private final static int ROWS = 3;
    private final static int COLUMNS = 3;
    private char[][] gameBoard;
    private int player1WinCount = 0;
    private int player2WinCount = 0; 
    private char player1Move = 'X', player2Move = 'O';
    private boolean playerX = true;

    Scanner in = new Scanner(System.in);

    public TicTacToe()
    {
        gameBoard  = new char [ROWS][COLUMNS];
        playerX = true;
        startGame();
    }

    //Initiate the game board with all empty spaces. 
    public void startGame()
    {
        for (int row = 0; row < ROWS; row++) //Loop through rows.
            for(int col = 0; col < COLUMNS; col++) //Loop through columns.
                gameBoard[row][col]= ' ';
    }

    public boolean checkTurn()
    {
        if (playerX)
        {
            System.out.println("Player X's turn.");
        }
        else
        {
            System.out.println("Player O's turn.");
        }
        return playerX;
    }

    public void addMove()
    {
        int row, col;
        do
        {
        checkTurn();
        System.out.println("Enter a row (1-3): ");
        row = in.nextInt() - 1; //Array index starts at 0.
        System.out.println("Enter a column (1-3): ");
        col = in.nextInt() - 1;

        if(notValid(row,col)){
            // do not proceed
            System.out.println("Invalid Entry.");
            continue;
        }

        if (row>=0 && row<ROWS)
            if(col>=0 && col<COLUMNS)
                if (playerX)
                    {
                        gameBoard[row][col] = player1Move;
                    }
                    else 
                    {
                        gameBoard[row][col] = player2Move;
                    }
         boolean hasWon = checkForWin();
         if(hasWon)
         {
             System.out.println("You won");
             if(playerX)
             {
                 player1WinCount++;          
             }
             else
             {
                 player2WinCount++;
             }
             break;
         }
         changePlayer();
        }while (true);
    }

    public boolean notValid(int row, int col)
    {
        if (row < 0 || row > (ROWS - 1))
            return true;
        if (col < 0 || col > (COLUMNS - 1))
            return true;
        if (!isEmpty(row,col))
            return true;
        return false;
    }

    public boolean isEmpty(int row, int col)
    {
        if(gameBoard[row][col]==' ')
            return true;
        else
        {
            System.out.println("Space is already occupied.");
            return false;
        }
    }
    public void changePlayer()
    {
        if (playerX)
        {
            playerX = false;
        }
        else
        {
            playerX = true;
        }
    }
    public void printBoard()
    {
        for (int row = 0; row < ROWS; row++){
            for (int col = 0; col < COLUMNS; col++)
            {        
                System.out.print("" + gameBoard[row][col]);
                if(col == 0 || col == 1)
                    System.out.print("|");

            }
            if (row ==0 || row ==1)
            System.out.print("\n-----\n");
        }   
    }
    /**
     * This method checks to see if a winner.
     * return true is there is a winner. 
     */
    public boolean checkForWin()
    {
        //checks rows for win
        for(int row = 0; row < ROWS; row ++)
        {
            if (gameBoard[row][0] == gameBoard[row][1] && gameBoard[row][1]==gameBoard[row][2] && gameBoard[row][0]!= ' ')
            return true;    
        }       
        //checks columns for wins.
        for (int col = 0;  col < COLUMNS; col++)
        {   
            if (gameBoard[0][col] == gameBoard[1][col]&& gameBoard[1][col]==gameBoard[2][col] && gameBoard[0][col]!= ' ')
            return true;
        }
        //check the diagonals for wins.
        if (gameBoard[0][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[2][2] && gameBoard[0][0]!= ' ')
            return true;
        if (gameBoard[2][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[0][2] && gameBoard[0][2]!= ' ')
            return true;

        return false; 
    }

    public static void main(String args[])
    {
        TicTacToe game = new TicTacToe();
        game.addMove();
        game.printBoard();
    }
}

关于java - 井字游戏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36848939/

相关文章:

尝试在 xcode 中退出程序时出现 C++ 11db 错误。初学者级

java - 卢塞恩 : Search with partial words

java - LocalDate.EPOCH 不可用

c - 如何将数组分成 block

php - 从 PHP 中的关联数组中获取唯一值?

java - 在可变行长度的java 2D数组中查找列的最大元素

matlab - 有没有办法执行一段时间?

java - 使用 PKCS 证书 (.spc) 对 jar 文件进行签名

java - JBoss 7 将 jar 添加到类路径

excel - 如何重新启动文件输入循环