java我的TictaToe代码出了什么问题?如何检查已采取的措施?
我已经编辑了这个问题。我觉得我很接近了,但出于某种原因,该代码的输出会在电路板上方额外打印一个“X”或“O”。我无法精确指出它为什么这样做,以及如何检查是否使用了一个点,并且不允许覆盖该点。我尝试过放置一个if(board[I][j]!=''{S.O.P(“无效”)},但它不能正常工作。也许我写错地方了
最后一件事是,当我想重新开始游戏时,已经宣布了赢家。如何让游戏重置棋盘
我已经做了几个小时了,非常感谢你的帮助
import java.util.Scanner;
public class TicTacToe
{
public static char[][] board = new char[3][3];
public static char player;
public static void newGame()
{
System.out.println("New Game: X goes first.");
player = 'X';
}
public static void writeBoard()
{
//drawing the game board
String line = "______";
System.out.println("");
//making seperate columns and rows
for(int i= 0;i<3; i++)
{
for(int j=0;j<3;j++)
System.out.print(board[i][j]+"|");
System.out.println("");
System.out.println(line);
}
}
public static void getMove()
{
Scanner keyboard = new Scanner(System.in);
int i, j;
if(player == 'X')
{
System.out.println("X's Turn");
System.out.println("Where do you want your X placed?");
System.out.println("Please enter row number and column number seperated by a space.");
i = keyboard.nextInt();
j = keyboard.nextInt();
if(board[i][j] != ' ')
System.out.println("That position is taken");
System.out.println("You have entered row" + " " + i + " "+ "and column" + " " + j);
System.out.println("Thank you for your selection.");
System.out.print(board[i][j]='X');
player = 'O';
}
else if(player == 'O')
{
System.out.println("O's Turn");
System.out.println("Where do you want your O placed?");
System.out.println("Please enter row number and column number seperated by a space.");
i = keyboard.nextInt();
j = keyboard.nextInt();
if(board[i][j] != ' ')
{
System.out.println("That position is taken");
}
System.out.println("You have entered row"+" " + i +" "+ "and column" +" " + j);
System.out.println("Thank you for your selection.");
System.out.println(board[i][j] = 'O');
player = 'X';
}
}
public static boolean winner()
{
//check the row
for (int i = 0; i < 3; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && (board[i][0] == 'X'))
{
System.out.println("X IS THE WINNER");
return true;
}
else if (board[i][0] == board[i][1] && board[i][1] == board[i][2] &&(board[i][0]=='O'))
{
System.out.println("O IS THE WINNER");
return true;
}
//check the column
else if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && (board[0][i] =='X'))
{
System.out.println("X IS THE WINNER");
return true;
}
else if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && (board[0][i]=='O'))
{
System.out.println("O IS THE WINNER");
return true;
}
}
//check diagnols from the left
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && (board[0][0]=='O'))
{
System.out.println("O IS THE WINNER!");
return true;
}
else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && (board[0][0] == 'X'))
{
System.out.println("X IS THE WINNER!");
return true;
}
//checks diagnols from the right
else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && (board[0][2]=='O'))
{
System.out.println("O IS THE WINNER!");
return true;
}
else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && (board[0][2]=='X'))
{
System.out.println("X IS THE WINNER!");
return true;
}
else
{
return false;
}
}
public static boolean tie()
{
//If there is no winner(a tie)
for(int i=0;i<3;i++)
{
for(int j=0; j<3;j++)
{
if(board[i][j]==' ')
{
System.out.println("It's A TIE!");
return false;
}
}
}
return true;
}
}
import java.util.Scanner;
public class TicTacToeDemo
{
public static char[][] board = new char[3][3];
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int i, j;
String answer;
do{
TicTacToe.newGame();
while(!TicTacToe.winner())
{
TicTacToe.writeBoard();
TicTacToe.getMove();
TicTacToe.tie();
}
System.out.println("Would you like a rematch?");
System.out.println("Enter yes or no");
answer = keyboard.next();
} while(answer.equalsIgnoreCase("yes"));
//do-while loop to keep game going if wanted to
}
}
# 1 楼答案
在进行第一步时,player变量是未定义的,因为它是getMove()和newGame()的局部变量。让它全球化。 此外,你要检查是否有赢家,但不检查哪一个。您需要检查它是否是“X”或“O”行,然后返回获胜的玩家,例如,作为字符,如果没有赢家,则返回其他字符
所以,应该是这样的:
另外,在getMove()方法中:
# 2 楼答案
TicTacToe类
newGame()缺少返回语句,添加void
播放器未初始化,请使用以下命令将其设置为全局变量:
对winner方法进行重构,太复杂了。为了更容易阅读,可以将条件分成不同的方法。你可以找到很多关于如何做的例子
修复错误处理(我输入了“22 33”,得到了ArrayIndexOutOfBoundsException,还有“a b”,我得到了InputMismatchException),你需要防止这种情况
TicTacToeDemo类
我和j也从不使用黑板
你为什么这么做?这可以用while loop来完成
这几个补丁使它运行起来,但您需要解决所有其他问题
您使用什么IDE进行开发?它应该报告这个错误并给你解释