我有Java的功课,我需要稔的游戏程序。从本质上讲,这是一个双人游戏,玩家可以从3个堆中移除石块。去除石头的最后一个人是赢家。我写了一段时间循环,这样当所有的石头都没有石头时,程序将停止要求玩家进行移动。但是,它并没有停止。请帮忙。While循环稔的游戏不停止
这里是我的代码:
import java.util.Scanner;
public class nim {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Welcome!");
System.out.println("This is the game of Nim.");
System.out.println("[1] Pile 1: 9");
System.out.println("[2] Pile 2: 9");
System.out.println("[3] Pile 3: 9");
int stones1 = 9;// These variables will be used
int stones2 = 9;// in order to subtract a number
int stones3 = 9;// of stones from the pile.
int tStones = stones1 + stones2 + stones3;
while (tStones > 0){
System.out.println("From which pile would you like to take?");
String aMove1 = input.nextLine();
System.out.println("How many stones would you like to take?");
String bMove1 = input.nextLine();
int naMove1= Integer.parseInt(aMove1);
int nbMove1= Integer.parseInt(bMove1);
if (naMove1 == 1){ //This stack of code will
stones1 = stones1 - nbMove1;
}
if (naMove1 == 2){ // subtract stones based on the
stones2 = stones2 - nbMove1;
}
if (naMove1 == 3){ // pile input and stone input.
stones3 = stones3 - nbMove1;
}
if (stones1 < 0){
if (naMove1 == 1){
System.out.println("That move is invalid.");
System.out.println("But nothing happened!");
stones1 = 0;
naMove1 = 0;
nbMove1 = 0;
}
}
if (stones2 < 0){
if (naMove1 == 2){
System.out.println("That move is invalid.");
System.out.println("But nothing happened!");
stones2 = 0;
naMove1 = 0;
nbMove1 = 0;
}
}
if (stones3 < 0){
if (naMove1 == 3){
System.out.println("That move is invalid.");
System.out.println("But nothing happened!"); //splash
stones3 = 0;
naMove1 = 0;
nbMove1 = 0;
}
}
System.out.println("Taking " + nbMove1 + " stones from stack " + naMove1);
System.out.println("[1] Pile 1: " + stones1); //This stack will display
System.out.println("[2] Pile 2: " + stones2); //the stone count for
System.out.println("[3] Pile 3: " + stones3); //each pile after the first move.
}
System.out.println("You lose!");
}
}
你不更新tStones你的while循环,但那是休息条件。 – Gnietschow
所以,你有什么建议,Gnietschow? –