2014-03-13 29 views
0

嗨,我正在編程一個Nim遊戲,Nim Game Java錯誤?

我已經完成了大部分代碼,但我遇到了兩個主要問題。

  1. 我找不到我的錯誤

  2. 我似乎無法讓電腦纔會輪到

在此先感謝,這裏是我的代碼。

import java.util.*; 
import java.util.Random; 
public class Nim { 


public static void main(String[] args) { 
int banana = 0; 
Random r = new Random(); 
    intro(); 
    banana = r.nextInt(16); 
    int numStones = (15 + banana); 
yorner(numStones); 


//kbinput.nextInt(); 



     } 
public static void intro() { 
    System.out.println("Welcome to the game of Nim!"); 
    System.out.println("The Rules of the game are as follows: \n"); 
System.out.println("1. There are two players in this game; you and the computer."); 
    System.out.println("2. The game starts with a random number stones ranging from 15 to 30 stones."); 
System.out.println("3. Every turn each player takes anywhere between 1 to 3 stones"); 
    System.out.println("4. The player who takes the last stone loses. \n"); 
System.out.println("Would you like to start the game now? \nPlease enter 'Y' for yes and 'N' for no:"); 

} 
public static void yorner (int numStones){ 
System.out.println("This game of nim will start with " + numStones + " stones.\n"); 
    Scanner kbinput = new Scanner (System.in); 
    boolean vInput = false; 

    do 
     { 
     String yorn = kbinput.nextLine(); 
     char input = yorn.charAt(0); 
     switch(input) { 

    case 'Y': 
    case 'y': 
     vInput = true; 
     yes(numStones); 
     break; 
    case 'N': 
    case 'n': 
     System.out.println("Thank you for your time."); 
     vInput = true; 
     break; 
    default: 
System.out.println("Please only enter 'Y' for yes and 'N' for no, other entries will not be tolerated."); 
     } 
     } 
     while((vInput == false)); 

} 
public static void yes (int numStones){ 
System.out.println("You selected 'Yes', thank you for choosing to play the game of Nim.\n"); 

    System.out.println("It is your turn first."); 
    System.out.println("How many stones would you like to take? \n"); 
    System.out.println("Enter a number from 1 to 3"); 
    player(numStones); 
} 

public static int player(int numStones){ 


    Scanner kbinput = new Scanner (System.in); 
    int numTake = kbinput.nextInt(); 
    int numStone = 0; 
    boolean apple = false; 
    loop: while (apple == false){ 
    switch(numTake){ 
    case 1: 
     apple = true; 
     numStone = numStones - numTake; 
     System.out.println("There are " + numStone + " stones left"); 
     break; 
    case 2: 
     apple = true; 
     numStone = numStones - numTake; 
     System.out.println("There are " + numStone + " stones left"); 
     break; 
    case 3: 
     apple = true; 
     numStone = numStones - numTake; 
     System.out.println("There are " + numStone + " stones left"); 

     break; 
    default: 
System.out.println("You can only takes anywhere between 1 and 3 stones from the pile"); 
    } 
     } 

    return numStone; 

} 
public static boolean compWin (int numStone){ 
return false; 

} 
public static void computerTurn(int numStone1, int numStone) { 
Random rn = null; 
int compTake = rn.nextInt(3); 
switch(compTake){ 
case 1: 
    System.out.println("Computer takes 1 stone."); 
    numStone1 = numStone - compTake; 
    System.out.println("There are " + numStone + " stones left"); 
    break; 
case 2: 
    System.out.println("Computer takes 2 stones"); 
    numStone1 = numStone - compTake; 
    System.out.println("There are " + numStone + " stones left"); 
    break; 
case 3: 
    System.out.println("Computer takes 3 stones"); 
    numStone1 = numStone - compTake; 
    System.out.println("There are " + numStone + " stones left"); 
    break; 
} 
} 






} 

,我得到的錯誤是在線程「主要」 java.lang.Error的這些

異常:未解決的編譯問題: 語法錯誤,插入「;」完成LocalVariableDeclarationStatement 語法錯誤,插入「;」完成聲明

+0

'隨機RN = NULL; int compTake = rn.nextInt(3);' 我敢肯定,這總是會導致'NullPointerException'? – tredontho

+0

我該如何解決這個問題? – user3417442

+0

類似於你在'main'方法中使用隨機變量。初始化它而不是'null'。隨機rn =新隨機();' – tredontho

回答

1

我無法再現您的編譯錯誤。

但是,你沒有一個遊戲循環(即一個循環,保持遊戲的打算,而有仍然有效移動),你永遠不調用computerTurn

+0

你能幫助我如何做到這一點? – user3417442

+0

在你的main中,類似:'while(numStones> 0){player(); computerTurn(); }'。你不需要'computerTurn()'中的第一個參數。由於您無論如何都會覆蓋它,只需在方法的開頭聲明它即可。 – blueygh2