gpt4 book ai didi

java - 检查获胜者 Tic Tac Toe 游戏 Java

转载 作者:行者123 更新时间:2023-12-01 09:56:08 25 4
gpt4 key购买 nike

我的获胜者检查方法没有检测到平局游戏。我不明白平局游戏的问题出在哪里,并且在专栏中进行搜索,所以如果有人可以提供帮助,那就太好了!代码如下

public static boolean CheckForWinner(int player)
{
//Row and Column Check
if (GameBoard[0][0] == player && GameBoard[0][1] == player && GameBoard[0][2] == player ||
GameBoard[1][0] == player && GameBoard[1][1] == player && GameBoard[1][2] == player ||
GameBoard[2][0] == player && GameBoard[2][1] == player && GameBoard[2][2] == player ||
GameBoard[0][0] == player && GameBoard[1][0] == player && GameBoard[2][0] == player ||
GameBoard[0][1] == player && GameBoard[1][1] == player && GameBoard[2][1] == player ||
GameBoard[0][2] == player && GameBoard[1][2] == player && GameBoard[2][2] == player)
{
finalwinner=true;
return true;
}

//diagonal checks
if (GameBoard[0][0]==player && GameBoard[1][1]==player && GameBoard[2][2]==player)
{
finalwinner=true;
return true;
}

if (GameBoard[2][0]==player && GameBoard[1][1]==player && GameBoard[0][2]==player)
{
finalwinner=true;
return true;
}

//Tie Game Check
if (GameBoard[0][0] == player && GameBoard[0][1] == player && GameBoard[0][2] == player &&
GameBoard[1][0] == player && GameBoard[1][1] == player && GameBoard[1][2] == player &&
GameBoard[2][0] == player && GameBoard[2][1] == player && GameBoard[2][2] == player)
{
tieGame=true;
return true;
}
return false;
}

这是主要方法:

       public static void main (String[] args)
{
System.out.println("Tic Tac Toe Game");
System.out.println("Player 1 is 1, Computer is 2");

Draw();
currentplayer=1;
while (winner!= true)
{
if(currentplayer==1)
{
System.out.println("Your Turn");
Player1Turn(currentplayer);
}
else
ComputerTurn(currentplayer);

finalwinner=CheckForWinner(currentplayer);

if(finalwinner==true && tieGame==false)
{
System.out.println("Winner is Player" +currentplayer+ "Congrats Champion!");
System.out.println("Beginning New Game! Press Cancel then Enter to Exit");
Reset();
}

if(tieGame==true)
{
System.out.println("It's a Tie! Play Again.");
System.out.println("Beginning New Game! Press Cancel then Enter to Exit");
Reset();
}

if(currentplayer==1)
currentplayer=2;
else if(currentplayer==2)
currentplayer=1;
}

}

最佳答案

问题是 if 语句中的这个表达式:

GameBoard[0][0] == player && GameBoard[0][1] == player && GameBoard[0][2] == player &&
GameBoard[1][0] == player && GameBoard[1][1] == player && GameBoard[1][2] == player &&
GameBoard[2][0] == player && GameBoard[2][1] == player && GameBoard[2][2] == player

您正在检查玩家是否处于每个棋盘位置。

您想要确定没有赢家。这意味着检查所有行、列和对角线都没有相同的玩家。

关于java - 检查获胜者 Tic Tac Toe 游戏 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37199056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com