Java-如何让我的TictaToe游戏正确地检查赢家?
我没有得到任何编译错误,但必须有一个逻辑错误,因为我的checkWinner方法甚至没有遇到 这是我的checkWinner方法代码:Java-如何让我的TictaToe游戏正确地检查赢家?,java,arrays,methods,Java,Arrays,Methods,我没有得到任何编译错误,但必须有一个逻辑错误,因为我的checkWinner方法甚至没有遇到 这是我的checkWinner方法代码: public boolean checkWinner() { for (int i=0;i<3;i++){ if ((gameBoard[i][0] == gameBoard[i][1]) && (gameBoard[i][1] == gameBoard[i][2])) { //check every row to fi
public boolean checkWinner() {
for (int i=0;i<3;i++){
if ((gameBoard[i][0] == gameBoard[i][1]) && (gameBoard[i][1] == gameBoard[i][2])) { //check every row to find a match
System.out.println(currentMark + "wins!");
}
else if ((gameBoard[0][i] == gameBoard[1][i]) && (gameBoard[1][i] == gameBoard[2][i])) { //checks every column to find a match
System.out.println(currentMark + "wins!");
}
}
if ((gameBoard[0][0] == gameBoard[1][1]) && (gameBoard[1][1] == gameBoard[2][2])) { //checks first diagonal
System.out.println(currentMark + "wins!");
}
else if ((gameBoard[0][2] == gameBoard[1][1]) && (gameBoard[1][1] == gameBoard[2][0])) { //checks second diagonal
System.out.println(currentMark + "wins!");
}
else
System.out.println("Tie!");
return true;
我不确定我的checkWinners方法是否应该是addMove方法的一部分…这是addMove
public boolean addMove(int row, int column) {
boolean nonacceptable = true;
while (nonacceptable) {
System.out.println("Which row and column would you like to enter your mark? Enter the row and column between 0 and 2 separated by a space.");
row = input.nextInt();
column = input.nextInt();
if ((row >= 0 && row <=2) && (column >= 0 && column <=2)) { //make sure user entered a number between 0 and 2
if (gameBoard[row][column] != ' ') {
System.out.println("Sorry, this position is not open!");
}
else {
gameBoard[row][column] = currentMark;
nonacceptable = false;
}
}
else
System.out.println("That position is not between 0 and 2!");
}
return nonacceptable;
public boolean addMove(int行,int列){
布尔不可接受=真;
while(不可接受){
System.out.println(“要输入标记的行和列?输入0和2之间的行和列,用空格分隔。”);
row=input.nextInt();
column=input.nextInt();
if((row>=0&&row=0&&column看起来像用户输入的有效答案nonacceptable
被设置为false,这样while(nonacceptable)
循环将退出。但是,您返回nonacceptable
(这是false),意味着检查if(addMove())
将不会通过(因此不会调用checkWinner
)。您可以尝试将if
语句向下移动,如下所示:
addMove();
displayBoard();
if (checkWinner()) {
System.exit(0);
}
whoseTurn();
但是,只有在游戏获胜或平局的情况下,您才需要更改checkWinner
以返回true
,否则返回false
。看起来像是当用户输入有效答案时nonacceptable
设置为false,以便同时(nonacceptable)
循环将退出。但随后返回nonacceptable
(这是false),这意味着检查if(addMove())
将不会通过(因此不会调用checkWinner
)。您可以尝试向下移动if
语句,如下所示:
addMove();
displayBoard();
if (checkWinner()) {
System.exit(0);
}
whoseTurn();
但是,您需要更改checkWinner
以仅在游戏获胜或平局时返回true
,否则返回false
。在if(addMove(input.nextInt(),input.nextInt())
行中,您正在调用input.nextInt()
两次,因此在调用addMove
之前,用户必须输入两个整数。这是您期望的行为吗?@neuronaut这就是导致所有问题的原因吗?我可以将其更改为什么?我只需删除这些参数:public boolean addMove()
和if(addMove())
。由于该功能已经提示用户并请求输入,因此您不需要同时请求输入(无提示)调用函数时,您需要检查每行的每一列,检查右边和左边diagonals@neuronaut好的,这样就解决了我的问题,它两次接受输入…但它仍然无法识别赢家…你知道我如何解决这个问题吗?在if(addMove(input.nextInt(),input.nextInt())行中
您正在调用input.nextInt()
两次,因此在调用addMove
之前,用户必须输入两个整数。这是您所期望的行为吗?@neuronaut就是导致所有问题的原因吗?我可以将其更改为什么?我只需删除这些参数:公共布尔addMove()
和如果(addMove())
。由于该函数已经提示用户并请求输入,因此您不需要同时请求输入(没有提示)调用函数时,您需要检查每行的每一列,检查右边和左边diagonals@neuronaut好的,这样就解决了我的问题,它两次接受输入…但它仍然无法识别赢家…你知道我如何解决这个问题吗?