gpt4 book ai didi

java - 用 while 循环结束我的游戏 -- java

转载 作者:行者123 更新时间:2023-12-01 12:20:36 24 4
gpt4 key购买 nike

我在尝试制作 TicTacToe 游戏时添加了此代码。最初游戏只玩了一个回合,但我需要一种方法让它在玩家玩完游戏后结束。但是当我运行代码时,它甚至没有进行回合播放。我之前用一个简单的 while(a>3){/playturn/a++;} 对其进行了测试,它在大部分情况下都有效,但循环未能正确结束,只是让它逐渐变细并强制我手动关闭我的终端。

http://hastebin.com/iziselusex.tex

这是我的轮流方法,因为你要求它

http://hastebin.com/isoxifufeq.axapta

其中包含Initialize方法,以及turns函数中提到的方法

    int[][] anArray = new int[3][3];
aBoard.initialize();

int end = 0;

int t = 0;

while(end != 1){ //so long as end isn't reached, the game will still play
while(t < 4){ //there are a total of 4 turns that can take place before the board is filled up. 5 if you include the last piece, but i wasn't sure about including it.
anArray = this.turns(anArray);//this starts the turn

if((anArray[0][0] == anArray[1][1]) && (anArray[1][1] == anArray[2][2])){
end = 1;
}else if((anArray[0][2] == anArray[1][1]) && (anArray[1][1] == anArray[2][0])){
end = 1;
}else if((anArray[0][0] == anArray[0][1]) && (anArray[0][1] == anArray[0][2])){
end = 1;
}else if((anArray[1][0] == anArray[1][1]) && (anArray[1][1] == anArray[1][2])){
end = 1;
}else if((anArray[2][0] == anArray[2][1]) && (anArray[2][1] == anArray[2][2])){
end = 1;
}else if((anArray[0][0] == anArray[1][0]) && (anArray[1][0] == anArray[2][0])){
end = 1;
}else if((anArray[0][1] == anArray[1][1]) && (anArray[1][1] == anArray[2][1])){
end = 1;
}else if((anArray[0][2] == anArray[1][2]) && (anArray[1][2] == anArray[2][2])){
end = 1;
}else{
end = 0;
}//this checks for possible victories that could have been made during the last turn

t++; //starts the next cycle over again
}
end = 1; //once all the turns are exhausted, the game ends

if(end == 1){
aView.println("The game is finished!");
System.exit(0);
}
}

编辑:我在末尾添加了退出函数并修复了第二个 while 循环问题(不再有 t > 4)

最佳答案

首先,第二个 while 必须是 while (t < 4),因为 t 从 0 开始,t > 4 将立即计算为 false,甚至不会运行循环体。

关于java - 用 while 循环结束我的游戏 -- java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26686089/

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