当前正在用tic-tac-toe的Java做一个学校项目,感觉代码本质上是正确的,但是当我尝试输出它时,它不会执行打印功能,并且会一直指示获胜者,打印功能本身也不会科技产出的空白,交叉或化为乌有时,它已经被分配到这些。任何帮助将不胜感激
public static void printBoard(char[][] board)
{
for(int i=0;i<NUMBER_OF_ROWS;i++)
{
System.out.println();
for(int j=0;j<NUMBER_OF_COLUMNS;j++)
{
if(j==0)
{
System.out.print("| ");
}
System.out.print(board[i][j] + " | ");
}
}
}
public static boolean isBoardFull(char[][] board)
{
for(int i=0;i>NUMBER_OF_ROWS;i++)
{
for(int j=0;j>NUMBER_OF_COLUMNS;j++)
{
if(board[i][j]==BLANK)
{
return false;
}
}
}
return true;
}
public static char winner( char[][] board)
{
char winner = BLANK;
for( int row = 0; row < board.length; row++)
{
if( board[row][0] == board[row][1] && board[row][1] == board[row][2])
{
winner = board[row][0];
}
}
for( int column = 0; column < board.length; column++)
{
if(board[0][column] == board[1][column] && board[1][column] == board[2][column])
{
winner = board[0][column];
}
}
if( board[0][0] == board[1][1] && board[1][1] == board[2][2])
{
winner = board[0][0];
}
if( board[0][2] == board[1][1] && board[1][1] == board[2][0])
{
winner = board[0][2];
}
return winner;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean finished = false;
char[][] board = new char[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
clearBoard(board);
int playerNumber = 1;
System.out.println("Player 1:Noughts\nPlayer 2: Crosses");
while(!finished)
{
if(isBoardFull(board))
{
finished = true;
if(winner(board)==BLANK)
{
System.out.print("The match is a draw ");
}
else
{
System.out.print("The winner is" + (winner(board)==CROSS?" Player 1":" Player 2"));
}
}
System.out.println("Enter place on board you wish to make a move for "
+ (playerNumber%2!=0?"Player 1":"Player 2") + "(row,column): ");
Scanner input = new Scanner(System.in);
input.useDelimiter(",");
int row = input.nextInt() - 1;
int column = input.nextInt() - 1;
if(!canMakeMove(board,row,column))
{
System.out.print("Cannot make move, please enter "
+ "coordinates within range and not previously entered");
}
else
{
makeMove(board, row, column, playerNumber);
printBoard(board);
playerNumber++;
if(winner(board) != BLANK)
{
System.out.println("The winner is" + (winner(board)==CROSS?" Player 1":" Player 2"));
finished = true;
}
}
}
}
答案 0 :(得分:1)
在您的isBoardFull方法i>NUMBER_OF_ROWS
和j>NUMBER_OF_COLUMNS
条件是错误的,它应该是其他方式。
因此它总是返回true
,即板是满