井字游戏问题 [英] TicTacToe Problems

查看:73
本文介绍了井字游戏问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的tictactoe问题上陷入困境。定义一个称为TicTacToe的成绩。 TicTacToe类型的对象是一个TicTacToe游戏。将游戏板存储为具有三行三列的基本类型char的单个2d数组。包括添加动作,显示棋盘,判断轮到谁,判断是否有赢家,说出谁是赢家以及重新开始游戏的方法。为该类编写一个主要方法,该方法将允许两个玩家在同一键盘上依次输入他们的动作。

I am stuck on my tictactoe problem. Define a calss called TicTacToe. An object of type TicTacToe is a single game of TicTacToe. Store the game board as a single 2d array of base type char that has three rows and three columns. Include methods to add a move, display the board, to tell whose turn it is, to tell whether there is a winnner, to say who the winner is, and to restart the game to the beginning. Write a main method for the class that will allow two players to enter their moves in turn at the same keyboard.

我已经编写了一些方法,并进行了测试我去。当我测试我的代码时,我要么得到它来放置一个标记,而且打印出无效的条目,否则它将不断地循环询问是否移动,然后说空间被占用了。我不知道该如何解决。我确定它与我的do while循环以及isEmpty和notValid的布尔方法有关。另外,我还停留在如何为每个玩家的获胜实现计数器上。

I have some of my methods written and have been testing as I go. When I test my code I either get it to place a mark but also print out invalid entry or it will continuously loop through asking for a move and then saying the space is occupied. I can't figure out how to fix that. I'm sure it has something to do with my do while loop and the boolean methods for isEmpty and notValid. Also I'm stuck on how to implement a counter for each player's win.

这是我的代码:

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

这是我的测试班:

public class TicTacToe
{

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


    }

}


推荐答案

我将让您处理多个游戏部分。

I will let you handle the multiple game part. This plays one game and exits.

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();
    }
}

这篇关于井字游戏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆