Java 如何打印Tictaoe板之间的线条
我制作了一个2人游戏机的tic-tac-toe程序,但是在玩游戏时显示的棋盘是空白的,除了侧面和底部的符号。我想在行和列之间打印行,但我不知道如何打印。以下是涉及我的董事会的代码:Java 如何打印Tictaoe板之间的线条,java,if-statement,for-loop,Java,If Statement,For Loop,我制作了一个2人游戏机的tic-tac-toe程序,但是在玩游戏时显示的棋盘是空白的,除了侧面和底部的符号。我想在行和列之间打印行,但我不知道如何打印。以下是涉及我的董事会的代码: public void createBoard() { //This method creates the 3x3 board and fills the board with 0s for (int row = 0; row < board.le
public void createBoard()
{
//This method creates the 3x3 board and fills the board with 0s
for (int row = 0; row < board.length; row++) {
if(row > 0) System.out.println("-+-+-");
for (int column = 0; column < board[0].length; column++) {
if(column > 0) System.out.println("|");
board[row][column] = 0;
}
}
}
public void displayBoard(){
//creates right amount of cells for 3 rows
for (int row=0; row < board.length; row++)
{
//spaces between row notation and board + prints row notation
System.out.print("\t" +(char)('a'+ row) + " ");
//creates right amount of cells for the 3 columns
for (int column=0; column < board.length; column++)
{
if(board[row][column]==BLANK){
System.out.print("\t");
}
else if(board[row][column]==X){
System.out.print("\tX");
}
else{
System.out.print("\tO");
}
}
//blanks on right side of board
System.out.println();
}
//spacing between columns
System.out.println("\t \t1 \t2 \t3");
checkTie();
}
public void createBoard()
{
//此方法创建3x3板并用0填充板
对于(int row=0;row0)System.out.println(“-+-+-”);
对于(int column=0;column0)System.out.println(“|”);
线路板[行][列]=0;
}
}
}
公共显示板(){
//为3行创建适当数量的单元格
对于(int row=0;row
这取决于控制台的功能。几乎在任何地方都可以使用的最简单的方法是使用两个字符
(条形)和-
(减号)。您可能还希望在十字路口使用+
(plus)
有些终端具有特定的字符来绘制线条和方框,但这意味着您的代码必须确定终端的类型、功能,有时还需要确定字体。这里有一些库可以帮助您(例如)。是一个Java库,它试图为您提供类似的功能
编辑:查看以下代码示例:
for (int row=0; row < board.length; row++)
{
if(row > 0) System.out.println("-+-+-");
for (int column=0; column < board.length; column++)
{
if(column > 0) System.out.println("|");
... print cell value ...
}
}
for(int row=0;row0)System.out.println(“-+-+-”);
for(int column=0;column0)System.out.println(“|”);
…打印单元格值。。。
}
}
只需在字段之间打印出|(或符号)和u或-即可。@Speedo有许多tic-tac-toe示例可供后续使用,因此和其他web资源:我如何修改代码以显示您提到的最常用的条形和减号字符?当我试图实现它们时,我把电路板弄乱了。你需要使用这些字符而不是\t
。我仍然有问题。你能看看我更新的代码吗?我在那里尝试实现你所做的事情?@Speedo:拿一张网格纸。在网格纸上画一个tic-tac趾板。仔细计算网格方格,并将这些计数转移到您的print和println语句中。@Speedo:为什么您希望我的代码在createBoard()方法中工作?