2016-12-06 145 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();