问题尚未解决,但必须取消。
答案 0 :(得分: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
您正在检查玩家是否处于每个棋盘位置。
您想确定没有赢家。这意味着检查行,列和对角线都没有相同的播放器。
答案 1 :(得分:0)
您没有检查"计算机"赢了,如果计算机没赢,而且玩家也是,那么你可以将tieGame标志变为真
答案 2 :(得分:0)
将所有9个条件的GameBoard[0][0] == player
更改为GameBoard[0][0] != 0
。
答案 3 :(得分:0)
错误在函数的主代码/结构中。你的函数需要返回两个变量,finalwinner和tie game。现在你的函数在第一个return语句处停止,并在main中设置等于finalwinner。
尝试这样的事情:
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)
{
boolean finalwinner=true;
}
//diagonal checks
if (GameBoard[0][0]==player && GameBoard[1][1]==player && GameBoard[2][2]==player)
{
boolean finalwinner=true;
}
if (GameBoard[2][0]==player && GameBoard[1][1]==player && GameBoard[0][2]==player)
{
boolean finalwinner=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)
{
boolean tieGame=true;
}
return new boolean [] {finalwinner, tiegame};
}
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);
boolean result[] = CheckForWinner(currentplayer);
if(result[0]==true && result[1]==false)
{
System.out.println("Winner is Player" +currentplayer+ "Congrats Champion!");
System.out.println("Beginning New Game! Press Cancel then Enter to Exit");
Reset();
}
if(result[1]==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;
}
}
答案 4 :(得分:0)
这可能不是你喜欢的,但我强烈建议你去寻找不同的移动/董事会代表:
如何强>
首先,我建议你将你的单元格实现为枚举(因为它们只有9个)。
然后,将电路板实现为一组9个枚举,如下:
public enum Move {
A0, A1, A2, B0, B1, B2, C0, C1, C2; }
此外,将获胜模式(例如整行,整个对角线)实现为3个枚举的集合(即电路板集的子集)。
在这些变化之后,检查玩家(无论是你还是计算机)是否赢得了更容易。
我建议您添加子集以及检查玩家是否在Move类中获胜的方法。
可以像这样创建一个子集: e.g。
static private final EnumSet<Move> lineA = EnumSet.of(A0,A1,A2);
来自你赢得的方法的检查(注意你需要检查所有子集)可能是:
moves.containsAll(lineA)
其中,move是玩家进行的一系列动作(当玩家进行移动时,你会将枚举添加到其移动设置中)。
<强> WHY 强>
这种设计更简洁,直观和优雅。如果使用这种方法,则不应再遇到所描述的问题。
答案 5 :(得分:0)
首先,在您的主要方法中,您说finalwinner=CheckForWinner(currentplayer);
,因此每次检查行,列和对角线后,您都不必设置finalwinner=true;
。 return true;
就足够了。
你的问题是,你检查一个平局游戏,如果是这样,你也会返回true,但这是错误的,因为玩家和计算机都不会赢!因此,checkForWinner方法始终会返回 true 。
你真的不必为Tie Game检查额外费用。只需检查玩家的胜利,检查计算机的胜利,如果两者都是假的并且最后一步完成 - 这是一场平局游戏。