井字游戏显示无法正确显示

时间:2019-09-06 05:04:09

标签: java display tic-tac-toe

因此,我创建了一个简单的井字游戏控制台程序。但是由于某些原因,我的方法无法正确显示木板。

代码显示如下:

Tic-Tac-Toe
------------

Player 'X', enter move (row [1-3] column [1-3]): 2
2

|

|


-----------

|
 X 
|


-----------

|

|



Player 'O', enter move (row [1-3] column [1-3]): 

代码:

/**
 *  The grid represents the game board
 */
public class Grid {

    int ROWS = 3;                                                   // Defines the amount of rows
    int COLUMNS =3;                                                 // Defines the amount of columns

    Box[][] board;                                                  // Represents the game board as a grid
    int currentrow, currentcol;                                     // Row and Column that was played last

    public Grid()
    {
        board = new Box[ROWS][COLUMNS];                             // Constructor initializes the game board
        for(int row = 0; row < ROWS; row++) {
            for (int col = 0; col < COLUMNS; col++) {
                board[row][col] = new Box(row,col);
            }
        }
    }
    public void init()
    {
        for (int row = 0; row < ROWS; row++) {                      // Re-initializes the game board
            for (int col = 0; col < COLUMNS; col++) {
                board[row][col].clear();
            }
        }
    }
    public boolean isDraw()
    {
        for (int row = 0; row < ROWS; row++) {                      // Returns true if the game is a draw (no more empty boxes)
            for (int col = 0; col < COLUMNS; col++) {
                if (board[row][col].content == Player.EMPTY) {
                    return false;                                   // An empty box found, not a draw, exits
                }
            }
        }
        return true;                                                // No empty boxes return true is then a draw
    }
    public boolean hasWon(Player thePlayer) {
        return (board [currentrow] [0] .content == thePlayer && board [currentrow] [1].content == thePlayer && board [currentrow] [2].content == thePlayer // 3 in a row
                || board [0] [currentcol].content == thePlayer && board [1] [currentcol].content == thePlayer && board [2] [currentcol].content == thePlayer // 3 in a column
                || currentrow == currentcol
                && board[0] [0].content == thePlayer                // 3 in a diagonal
                && board[1] [1].content == thePlayer
                && board[2] [2].content == thePlayer
                || currentrow + currentcol == 2
                && board[0][2].content == thePlayer                 // 3 in the opposite diagonal
                && board[1][1].content == thePlayer
                && board[2][0].content == thePlayer);
    }
    public void paint()
    {
        for (int row = 0; row < ROWS; row++) {                      // Paints (displays) the full board
            for (int col = 0; col < COLUMNS; col++) {
                board[row][col].paint();
                if (col < COLUMNS - 1)
                    System.out.println("|");
            }
            System.out.println();
            if (row < ROWS - 1) {
                System.out.println("-----------");
            }
        }
    }
}

我希望代码以正确的方式打印出显示屏。我认为在paint()方法或初始化网格时肯定犯了一个简单的错误。请让别人看看我哪里出问题了。

2 个答案:

答案 0 :(得分:0)

您需要修改绘画方法。

for (int row = 0; row < ROWS; row++) {                      // Paints (displays) the full board
    for (int col = 0; col < COLUMNS; col++) {
        board[row][col].paint();
        if (col < COLUMNS - 1)
            System.out.print("|");
    }
    System.out.println();
    if (row < ROWS - 1) {
        System.out.println("-----------");
    }
}

首先,仅在需要时才需要使用println,其余时间只需使用print

Box.paint相同(此处不可见)。但它似乎正在使用System.out.println而不是System.out.print

另一件事,Box.paint应该返回String而不是在控制台中发送消息。木板负责绘画,而不是盒子。

public String paint(){
    return content; //return a `String` " ", "X" or "O"
}

经过测试:

public static void main(String[] args) throws ParseException {
    print(new String[][]{
            {"X", " ", "O"},
            {" ", " ", " "},
            {"O", " ", "X"}
        });
}

private static void print(String[][] board){
    int ROWS = board.length;
    int COLUMNS = board[0].length;
    for (int row = 0; row < ROWS; row++) {                      // Paints (displays) the full board
        for (int col = 0; col < COLUMNS; col++) {
            System.out.print(board[row][col]);
            if (col < COLUMNS - 1)
                System.out.print("|");
        }
        System.out.println();
        if (row < ROWS - 1) {
            System.out.println("-----");
        }
    }
}

给予:

X| |O
-----
 | | 
-----
O| |X

答案 1 :(得分:-1)

如果您不发布所有代码,很难100%确定地给出答案。您可以添加Boxmain方法的实现吗?

问题似乎是您在应该执行System.out.println("|");时正在执行System.out.print("|");System.out.println("|");还将换行,导致在下一行上打印出下一个内容。您已经正确处理了行尾的换行符(System.out.println();,所以System.out.print("|");应该是您所需要做的。

修复示例:https://repl.it/repls/DimgreyOutlandishFlashdrive