麻烦制作Java轮盘游戏

时间:2014-11-14 21:51:45

标签: java

我正在进行轮盘赌游戏,我的轮盘赌课程中的makeDecision方法出错了。原因是因为我在我的Player类中遇到了updatePot方法的问题而且不知道该怎么做。我正在使用4个类,这是我的每个代码:

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

}

2 个答案:

答案 0 :(得分:0)

程序的输入必须是String,因为您的播放器可以输入文本或特定数字。然后,您必须检查此字符串是否出现(奇数/偶数/红/黑)或有效数字。如果两者都不为真,那么输入无效。

查看代码时,您需要考虑如何使用updatePot方法。

  • 是否应该将底池设置为给定值(这是方法名称对我的建议)
  • 是否应该将给定值添加到底池或
  • 是否应将当前底值乘以给定值

当你决定它应该如何工作后,你必须更新你的Roulette.makeDecision()方法。 祝你好运! 马库斯

答案 1 :(得分:0)

只需将下注的数据类型从String更改为int,然后添加updatePot方法。您还需要调整NumberFormatException。

相关问题