2016-12-06 146 views
-1

我試圖在學校製作一個nim遊戲,不幸的是我得到了不同於我所需輸出的輸出;這是我的代碼。Nim的遊戲邏輯錯誤

import java.util.Random; 
import java.util.Scanner; 

public class Nim2 { 
    private static int numStonesLeft; 

     /** 
     */ 
    /** 
     * The computerTurn method chooses a random number from 1 to 3 if 
     * numStonesLeft is greater than or equal to 3, otherwise chooses a random 
     * number from 1 to numStonesLeft. 
     * 
     * Then decrements numStonesLeft appropriately and prints the turn. 
     */ 
    public static void computerTurn() { 
     int stonesChosen = (int) (Math.random() * 16) + 15; 

     numStonesLeft -= stonesChosen; 
     System.out.println("\nI took " + stonesChosen + " stones."); 
     System.out.println("There are " + numStonesLeft + " stones left."); 
    } 

    /** 
     * The playerTurn method prompts the user for a valid number of stones to 
     * choose and reads an int value from the user and will repeat this action 
     * while the user input is invalid. (i.e. user must choose 1, 2 or 3 AND 
     * their choice must be less than or equal to numStonesLeft.) 
     * 
     * Also decrements numStonesLeft appropriately and prints the turn. 
     */ 
    public static void playerTurn() { 
     Scanner input = new Scanner(System.in); 
     System.out.println("Number of stones you take this turn:"); 
     int stonesChosen = 0; 
     stonesChosen = input.nextInt(); 

     while (stonesChosen > 3 || stonesChosen < 1) { 
      ; 
      System.out.println("That is an invalid number of stones."); 
      stonesChosen = input.nextInt(); 
     } 

     if (stonesChosen <= 3 || stonesChosen >= 1) 
      ; 
     { 
      System.out.println("\nYou took " + stonesChosen + " stones."); 
      System.out.println("There are " + (numStonesLeft - stonesChosen) 
        + " stones left."); 
     } 
     stonesChosen = input.nextInt(); 

    } 

    public void gameEnded() {   
      boolean over = false; 


      } 
    } 

不同的客戶端代碼

public class TestNim2 { 

    public static void main(String[] args) { 

     Nim2 nim = new Nim2(); 
     int numStonesLeft = (int) (Math.random() * 16) + 15; 
     System.out.println("This is the Game of nim."); 
     System.out.println("There is a pile of " + numStonesLeft 
       + " stones between us."); 
     System.out.println("We alternate taking either 1,2 or 3 stones."); 
     System.out.println("The person who takes the last stone loses"); 

     // Write a loop to alternate computerTurn() and playerTurn() 
     // checking after each turn see if there is a winner to print 
     // and to break the loop ... then output the winner 


      nim.playerTurn(); 
      nim.computerTurn(); 
     } 
} 

輸出我得到的是石塊

數你把這個回合:2

你花了2塊石頭。還剩下2塊石頭。 3

我拿了16塊石頭。還剩下16個石頭。

我的期望輸出應該是類似這樣的事情

你花了2塊石頭。有18個石頭留// 20

我花了3顆寶石隨機數有15個石頭留

+2

你是如何得到輸出的?你的代碼甚至沒有編譯 – developer

+0

我可以看到至少2個問題:1)在computerTurn()和playerTurn()中,你都沒有檢查'numStonesLeft> = stonesChosen',所以有負數出現 2)in playerTurn(),你沒有從stonsChosen中扣除numStonesLeft – diufanman

回答

0

有不少問題:

  • 你不初始化numStonesLeft
  • 您在玩家輪流中忘記了numStonesLeft -= stonesChosen;
  • 扣除後不檢查numStonesLeft >= 0
  • if之後加上分號可能會被解析爲無條件執行以下作用域。停止在行首輸入隨機分號。
  • 玩家轉過頭,多餘的stonesChosen = input.nextInt();