2014-12-24 154 views
0

我有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!"); 
    } 

} 
+2

你不更新tStones你的while循環,但那是休息條件。 – Gnietschow

+0

所以,你有什麼建議,Gnietschow? –

回答

0

你是不是在while循環體更新tStones

嘗試更新它:

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; 
     } 
    } 

// Updating here 
    tStones = stones1 + stones2 + stones3; 

    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!"); 
} 
} 
+0

它不工作 –

+0

@ noah.chun它的工作,現在只要你的樁都是零,如果你的目標是不是這個然後再檢查程序的邏輯循環將結束 –