我如何在TicTacToe中正确打印我的板子

因此,我在用井字游戏打印板时遇到了一些麻烦,而不是并排打印,而是向下打印行。

它打印如下:

  • |
  • |
  • |
  • |
  • |
  • |
  • |
  • |
  • |

我希望代码能像普通的单板一样打印。 TicTacToe.java

package TicTacToe;
import Interfaces.IGame; 
public class TicTacToe implements IGame{
    private char[][] board;

    private char currentPlayerSign;



    /**

     * Constructor. 

     * Properly initialized board, sets which player will play first.

     * @param x

     * @param y

     */

    public TicTacToe() {

        board = new char[3][3];

        currentPlayerSign = 'x';

        clearBoard();

    }   

    /**

     * Gives access to the CurrentPlayerSign

     */

    public char getCurrentPlayerSign() {

        return currentPlayerSign;

    }

    @Override

    /**

     * Return the board as strings.

     */

    public void printBoard() {

        System.out.println("--------");

        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board.length; j++) {

                System.out.println(board[i][j] + " | ");

            }

            System.out.println();

        }

    }

    @Override

    /**

     * Empty board/values. Looping through rows and columns.

     * Set each space to "-".

     */

    public void clearBoard() {

        for (int i = 0; i < 3; i++) {

            for (int j = 0; j < 3; j++) {

                board[i][j] = '-';

            }

        }

    }



    @Override

    /**

     * If there is none "-" then the board is full and return true.

     */

    public boolean fullBoard() {

        boolean fullBoard = true;

        for (int i = 0; i < 3; i++) {

            for (int j = 0; j < 3; j++) {

                if (board[i][j] == '-') {

                    fullBoard = false;

                }

            }

        }

        return fullBoard;

    }

    @Override

    /**

     * Main method to test rows, columns and diagonals for win

     * since these have different conditions. 

     * return the three methods for rows, columns or diagonal win.

     */

    public boolean ifWinner() {

        return rowsForWin() || colForWin() || diagForWin();

    }


    @Override

    /**

     * Checks if there is three identical in a row

     */

    public boolean rowsForWin() {

        for (int i = 0; i < 3; i++) {

            if (checkRowColumns(board[i][0], board[i][1], board[i][2]) == true) {

                return true;

            }

        } 

        return false;

    }



    @Override

    /**

     * Checks if there is three identical in the columns

     */

    public boolean colForWin() {

        for (int i = 0; i < 3; i++) {

            if (checkRowColumns(board[0][i], board[1][i], board[2][i]) == true) {

                return true;

            }

        } 

        return false;

    }



    @Override

    /**

     * Checks if there is three identical diagonal from each corner of the board

     */

    public boolean diagForWin() {

        return ((checkRowColumns(board[0][0], board[1][1], board[2][2]) == true) || (checkRowColumns(board[0][2], board[1][1], board[2][0]) == true));

    }



    /**

     * Will check the specified characters if three X or O and compare them 

     * @return

     */

    boolean checkRowColumns(char c1, char c2, char c3) {

        return ((c1 != '-') && (c1 == c2) && (c2 == c3));

    }



    @Override

    /**

     * Change turns from player 1 to 2 and back

     */

    public void turn() {

        if(currentPlayerSign == 'x') {

            currentPlayerSign = 'o';

        }

        else {

            currentPlayerSign = 'x';

        }

    }



    @Override

    /**

     * Places the sign on the board

     */

    public boolean placeSign(int row, int column) {

        if ((row >= 0) && (row < 3)) {

            if ((column >= 0) && (column < 3)) {

                if (board[row][column] == '-') {

                    board[row][column] = currentPlayerSign;

                    return true;

                }

            }

        }

        return false;

    }


}

这是我的Main.java

包主;

导入java.util.Scanner;

导入TicTacToe.TicTacToe;

公共班级主要{     / **

 * 

 * @param args

 */

public static void main(String[] args) {



    Scanner s = new Scanner(System.in);

    //ConnectFour CF = new ConnectFour();

    TicTacToe TTT = new TicTacToe();



    TTT.clearBoard();

    System.out.println("Tic Tac Toe");

    do 

    {

        System.out.println("Board layout:");

        TTT.printBoard();

        int row;

        int column;

        do 

        {

            System.out.println("Player " + TTT.getCurrentPlayerSign() + " enter empty row and column to play");

            row = s.nextInt()-1;

            column = s.nextInt()-1;

        }

        while (!TTT.placeSign(row, column));

        TTT.turn(); 

    }

    while (!TTT.ifWinner() && !TTT.fullBoard());

    if (TTT.fullBoard() && !TTT.ifWinner()) {

        System.out.println("Game result: Draw");

    } 

    else {

        System.out.println("Board layout:");

        TTT.printBoard();

        TTT.turn();

        System.out.println(Character.toUpperCase(TTT.getCurrentPlayerSign()) + "Wins!");

    }   

}

}

评论
  • 小草
    小草 回复

    尝试使用:

    System.out.print("...");
    

    The println("...") method prints the string "..." and moves the cursor to a new line.

    The print("...") method instead prints just the string "...", but does not move the cursor to a new line. Hence, subsequent printing instructions will print on the same line.

    Check out the Java Docs for more information.