2014-11-14 90 views
-2

我正在研究一個輪盤遊戲,並且我的輪盤類中的makeDecision方法出現錯誤。原因是因爲我在Player類中的updatePot方法遇到問題,不知道該怎麼做。我使用的4類,這是我對每個代碼:麻煩製作Java輪盤遊戲

public class Assign3 { 

    public static void main(String[] args) { 
     Roulette roulette = new Roulette(); 
     roulette.run(); 
    } 

} 

public class Roulette { 

    private String decision; 

    Player player = new Player(); 
    Wheel wheel = new Wheel(); 

    private void makeDecision(){ 
     String bet = player.getBet(); 
     try { 
      int value = Integer.parseInt(bet); 
      if (value == wheel.getValue()){ 
       decision = " you win 40 times your bet"; 
       player.updatePot(40); 
      } else { 
       decision = "you lose the game"; 
       player.updatePot(0); 
      } 
     } catch (NumberFormatException e){ 
      if (bet.equals("odd")){ 
       if (wheel.getValue() %2 == 0){ 
        decision = "you lose the game"; 
        player.updatePot(0); 
       } else { 
        decision = "you win double your bet"; 
        player.updatePot(2); 
       } 
      } else if (bet.equals("equals")){ 
       if (wheel.getValue() %2 == 0){ 
        decision = "you win double your bet"; 
        player.updatePot(2); 
       } 
      } 
     } 
    } 

    private void displayDecision(){ 

    } 

    public void run(){ 
     System.out.println("Welcome to Cache Creek style Roulette..bet an amount and type\n" 
          + " if you select the correct colour, you win double your bet\n" 
          + " if you select the correct odd/even, you win double your bet\n" 
          + " if you select the right number, you win 40 times your bet\n" 
          + " otherwise you lose your bet\n" 
          + " If you lose all your money, the game is over"); 

     do { 
      player.setBetAmount(); 
      player.setBet(); 
      wheel.spin(); 
      wheel.display(); 
      makeDecision(); 
      displayDecision(); 
     } while (true); 
    } 
} 

package Assignment3; 

import java.util.Scanner; 

public class Player { 

    private int pot = 0; 
    private int amount = 0; 

    Scanner input = new Scanner(System.in); 

    public void setBetAmount() { 
     System.out.print("\nHow much do you want to bet? "); 
     amount = input.nextInt(); 
     int pot =100; 
     if (amount > pot || amount < 1){ 
      System.out.print("Invalid bet amount..How much do you want to bet? "); 
      amount = input.nextInt(); 
     } 
    } 

    public void setBet() { 
     System.out.print("What is your bet? (odd/even/red/black/exact number)"); 

    } 

    public String getBet() { 
     return null; 
    } 

    public void updatePot(int i) { 

    } 

} 

package Assignment3; 

import java.util.Random; 

public class Wheel { 
    private int value; 

    private String colour; 
    Random random; 

    public Wheel(){ 
     value = 0; 
     colour = null; 
     random = new Random(); 
    } 

    public int getValue(){ 
     return value; 
    } 

    public void setValue(int value){ 
     this.value = value; 
    } 

    public String getColour(){ 
     return colour; 
    } 

    public void spin(){ 
     value = random.nextInt(39)+1; 
     colour = (random.nextInt(1)==0)?"red":"black"; 
    } 

    public void display(){ 
     System.out.print(value+" "+colour); 
    } 

} 
+1

你能描述'updatePot'應該做什麼嗎? – 2014-11-14 21:53:51

+0

如果玩家做出正確的選擇,updatePot應該增加他們贏得的獎金到他們現有的底池。 – James 2014-11-14 21:56:48

+0

'鍋+ =我;'..... – 2014-11-14 21:57:59

回答

0

輸入你的程序有成爲一個字符串,因爲你的玩家可以輸入機器人h文本或特定的數字。然後您必須檢查該字符串是否出現(奇數/偶數/紅色/黑色)或有效數字。如果兩者都不成立,則輸入無效。

在查看代碼時,您需要考慮如何讓updatePot方法起作用。

  • 應將其設置鍋給定值(這是方法名會建議我)
  • 它應該給定值添加到鍋或
  • 它應該乘以當前鍋值給定值

當你決定如何工作時,你必須更新你的Roulette.makeDecision()方法。 祝你的作業順利! Markus

+0

謝謝,這非常有幫助 – James 2014-11-15 00:00:25

0

只要將下注的數據類型從String更改爲int,然後添加updatePot方法即可。你還需要調整你的NumberFormatException。