首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tic-Tac-Toe游戏(Java)

Tic-Tac-Toe游戏(Java)
EN

Code Review用户
提问于 2015-10-13 00:25:07
回答 1查看 2.6K关注 0票数 5

这个程序在控制台内运行游戏.玩家,X和O,轮流把他们的角色放在3x3网格上。当一个玩家成功地连续3次,无论是垂直的,水平的,还是对角线的,他们都会赢。如果没有胜利者决定所有的方块被填补,这是一个僵局。

我想知道这段代码有多高效。是否存在任何冗余、格式问题、可读性问题等?

TicTacToe.java

代码语言:javascript
复制
package tictactoe;

public class TicTacToe {

    public static void main(String[] args) {
        GameBoard myGame = new GameBoard();
        myGame.displayBoard();

        int counter = 1;

        while (myGame.gameActive() && counter < 10) {
            if (counter % 2 == 0)
                myGame.askPlayer('O');
            else
                myGame.askPlayer('X');
            counter++;

            System.out.println("\n");
            myGame.displayBoard();
            myGame.checkForWinner();

            if (counter == 10 && myGame.gameOnGoing() == true)
                System.out.print("Stalemate!\n");
        }

    }

}

GameBoard.java

代码语言:javascript
复制
package tictactoe;

import java.util.Arrays;
import java.util.Scanner;

public class GameBoard {
    private char[][] gameBoard;
    private boolean gameOnGoing = true;

    //This is the constructor for the GameBoard class.
    public GameBoard() {
        gameBoard = new char[3][3];

        for (int row=0; row < gameBoard.length; row++) {
            Arrays.fill(gameBoard[row], ' ');
        }
    }   //end of constructor


    //This method will display the gameBoard to the screen.
    public void displayBoard() {
        for (int row=0; row < gameBoard.length; row++) {
            for (int col=0; col < gameBoard[0].length; col++) {
                System.out.print("\t" + gameBoard[row][col]);
                if (col == 0 || col == 1)
                    System.out.print("|");
            }
            if (row == 0 || row == 1)
                System.out.print("\n----------------------------\n");
        }
        System.out.println();
    }   //end of method displayBoard

    //This method will return true if the game is still active.
    public boolean gameActive() {
        return gameOnGoing;
    } //end of method gameActive

    //This method will ask the user to pick a row and column, validate
    //the inputs, and call the method makeMove().
    public void askPlayer(char player) {
        Scanner keyboard = new Scanner(System.in);
        int row, col;
        do {
            System.out.printf("Player %s, please enter a row (1-3): ",player);
            row = keyboard.nextInt();

            System.out.printf("Player %s, please enter a column (1-3): ",player);
            col = keyboard.nextInt();
        }   
            while (notValid(row,col));

        makeMove(player,row-1,col-1);

    }   //end of askPlayer method

    //This method will check to see if there are 3 x's or o's in a row
    //and return true if there is a winner, false otherwise
    public void checkForWinner() {
        //loop over each row and check for a winner
        for (int row =0; row < gameBoard.length; row++) {
            if (gameBoard[row][0] == gameBoard[row][1] && gameBoard[row][1] == gameBoard[row][2]
                    && gameBoard[row][0] != ' ') {
                System.out.print("The winner is " + gameBoard[row][0] + "!");
                gameOnGoing = false;
            }

        }

        //loop over each column and check for a winner
        for (int col=0; col < gameBoard[0].length; col++) {
            if (gameBoard[0][col] == gameBoard[1][col] && gameBoard[1][col] == gameBoard[2][col]
                    && gameBoard[0][col] != ' ') {
                System.out.print("The winner is " + gameBoard[0][col] + "!");
                gameOnGoing = false;
            }

        }

        //check the diagonals
        if (gameBoard[0][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[2][2]
                && gameBoard[0][0] != ' ') {
            System.out.print("The winner is " + gameBoard[0][0] + "!");
            gameOnGoing = false;
        }

        if (gameBoard[2][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[0][2]
                && gameBoard[2][0] != ' ') {
            System.out.print("The winner is " + gameBoard[2][0] + "!");
            gameOnGoing = false;
        }
    }

    //Same as above, but does not print winner or change state of gameOnGoing.
    public boolean gameOnGoing() {
        //loop over each row and check for a winner
        for (int row =0; row < gameBoard.length; row++) {
            if (gameBoard[row][0] == gameBoard[row][1] && gameBoard[row][1] == gameBoard[row][2]
                    && gameBoard[row][0] != ' ') {
                return false;
            }

        }

        //loop over each column and check for a winner
        for (int col=0; col < gameBoard[0].length; col++) {
            if (gameBoard[0][col] == gameBoard[1][col] && gameBoard[1][col] == gameBoard[2][col]
                    && gameBoard[0][col] != ' ') {
                return false;
            }

        }

        //check the diagonals
        if (gameBoard[0][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[2][2]
                && gameBoard[0][0] != ' ') {
            return false;
        }

        if (gameBoard[2][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[0][2]
                && gameBoard[2][0] != ' ') {
            return false;
        }
        return true;
    }   //end of method gameOnGoing

    //This method will validate if the row and column are between 1-3
    //and if the position is currently empty.
    public boolean notValid(int row, int col) {
        if (row > 3 || row < 1 || col > 3 || col < 1 || !isEmpty(row, col))
            return true;
        else    
            return false;
    } //end of method notValid

    //This method will check if a position is empty and return true
    //if the position is empty, false otherwise
    public boolean isEmpty(int row, int col) {
        if (gameBoard[row-1][col-1] == ' ')
            return true;
        else {
            System.out.print("That position is taken.\n");
            return false;
        }
    }

    //This method will validate if a player's move is allowed and return true
    //if the move was completed
    public boolean makeMove(char player, int row, int col) {
        if (row >=0 && row <=2 && col >=0 && col <=2) {
            if (gameBoard[row][col] != ' ')
                return false;
            else {
                gameBoard[row][col] = player;
                return true;
            }
        }
        else
            return false;
    }   //end of method makeMove

}
EN

回答 1

Code Review用户

回答已采纳

发布于 2015-10-13 02:15:23

下面是一些简单的要点。我没有进入GameBoard班的课程。

TicTacToe.java

  • 您在这里使用幻数作为您的计数器。这应该作为一个常数来提取。
  • 僵局条件可以放在that块之外,正如您已经知道的那样,counter = 10
  • 我还将提取XO字符作为常量。当意外的char被传递给askPlayer时,我很好奇会发生什么。
  • \nprintprintln语句中很少有意义。在你的最终路线,你可以只拥有`System.out.println(“相持!”);
  • 所有这些游戏逻辑都在一个main方法中,当它应该正确地封装到它自己的单独方法中时。

GameBoard.java

  • 次要的一点,但默认情况下我不会将gameOnGoing初始化为true。相反,在构造函数中初始化所有内容之后,我会设置它。
  • 提取游戏板大小,3作为常量。
  • 关于注释,您应该为您的方法使用适当的JavaDoc注释,并且没有必要对每个方法的结尾进行注释。因此,//这是GameBoard类的构造函数。public GameBoard() { gameBoard = new 3.;for (int row=0;row < gameBoard.length;row++) { Arrays.fill( GameBoard 划,‘');} //end构造函数应为: /** *这是GameBoard类的构造函数。*/ public GameBoard() { gameBoard =新的char3.;for (int row=0;row < gameBoard.length;row++) { Arrays.fill(gameBoard划,‘');}}
票数 3
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/107328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档