这是一个tic tac toe程序,您看到的部分是用户和计算机决策。我有两个while循环,当我运行它时,用户去,计算机响应两次,然后发生一些事情,计算机停止在用户之后移动。用户可以在不首先使用计算机的情况下进行移动。我不知道为什么,有人可以帮忙。
代码
while(!userDone)
{
if(input == 1 && !grid.previousInputs.contains(input))
{
grid.previousInputs.add(input);
g2.draw(X1s1);
g2.draw(X1s2);
userDone = true;
}
else if(input == 2 && !grid.previousInputs.contains(input))
{
grid.previousInputs.add(input);
g2.draw(X2s1);
g2.draw(X2s2);
userDone = true;
}
else if(input == 3 && !grid.previousInputs.contains(input))
{
grid.previousInputs.add(input);
g2.draw(X3s1);
g2.draw(X3s2);
userDone = true;
}
else if(input == 4 && !grid.previousInputs.contains(input))
{
grid.previousInputs.add(input);
g2.draw(X4s1);
g2.draw(X4s2);
userDone = true;
}
else if(input == 5 && !grid.previousInputs.contains(input))
{
grid.previousInputs.add(input);
g2.draw(X5s1);
g2.draw(X5s2);
userDone = true;
}
else if(input == 6 && !grid.previousInputs.contains(input))
{
grid.previousInputs.add(input);
g2.draw(X6s1);
g2.draw(X6s2);
userDone = true;
}
else if(input == 7 && !grid.previousInputs.contains(input))
{
grid.previousInputs.add(input);
g2.draw(X7s1);
g2.draw(X7s2);
userDone = true;
}
else if(input == 8 && !grid.previousInputs.contains(input))
{
grid.previousInputs.add(input);
g2.draw(X8s1);
g2.draw(X8s2);
userDone = true;
}
else if(input == 9 && !grid.previousInputs.contains(input))
{
grid.previousInputs.add(input);
g2.draw(X9s1);
g2.draw(X9s2);
userDone = true;
}
else
{
g2.drawString("Input Error: Please input a number 1 - 9", 200, 30);
userDone = false;
}
}
while(userDone)
{
if(!grid.previousInputs.contains(5))
{
g2.draw(O5);
grid.previousInputs.add(5);
userDone = false;
}
else if(!grid.previousInputs.contains(1))
{
g2.draw(O1);
grid.previousInputs.add(1);
userDone = false;
}
else if(!grid.previousInputs.contains(3))
{
g2.draw(O3);
grid.previousInputs.add(3);
userDone = false;
}
else if(!grid.previousInputs.contains(7))
{
g2.draw(O7);
grid.previousInputs.add(7);
userDone = false;
}
else if(!grid.previousInputs.contains(9))
{
g2.draw(O9);
grid.previousInputs.add(9);
userDone = false;
}
else if(!grid.previousInputs.contains(2))
{
g2.draw(O2);
grid.previousInputs.add(2);
userDone = false;
}
else if(!grid.previousInputs.contains(4))
{
g2.draw(O4);
grid.previousInputs.add(4);
userDone = false;
}
else if(!grid.previousInputs.contains(6))
{
g2.draw(O6);
grid.previousInputs.add(6);
userDone = false;
}
else
{
g2.draw(O8);
grid.previousInputs.add(8);
g2.drawString("Game Over!", 200, 230);
}
}