试图让我的赌场代码运行,直到所有的钱都消失了

时间:2014-10-21 04:26:38

标签: java

所以我不得不把这个赌场计划作为家庭作业,除了一小步之外我完成了所有这些。如果我以新的金额获胜,我需要我的比赛继续运行。 (如果这有意义的话)

public class CasinoSlot{
  public void play(){
    int coins=(int)(Math.random()*20+5);
    System.out.println("You have "+coins+" coins.");
    int bet=(int)(Math.random()*4+0);
    System.out.println("You bet "+bet+" coins.");
    if (bet==0){
      System.out.println("Please try again.");
    }
    else{
      int rem=coins-bet;
      if (bet>0){
        System.out.println("You have "+rem+" coins remaining.");
      }
      int d1=(int)(Math.random()*6+1);
      int d2=(int)(Math.random()*6+1);
      int d3=(int)(Math.random()*6+1);
      int same=(10*bet), rsame=(same+rem);
      int same2=(7*bet), rsame2=(same2+rem);
      int oddeven=(5*bet), roddeven=(oddeven+rem);
      int lose=(-1*bet), rlose=(lose+rem);
      System.out.println("You rolled: "+d1+", "+d2+", "+d3);
      if(d1==d2 && d2==d3){
        System.out.println("You win "+same+" coins. You now have "+rsame+" coins remaining.");
      }
      else if(d1==d2 || d2==d3 || d1==d3){
        System.out.println("You win "+same2+" coins. You now have "+rsame2+" coins remaining.");
      }
      else if((d1%2==0 && d2%2==0 && d3%2==0) || (d1%2!=0) && (d2%2!=0) && (d3%2!=0)){
        System.out.println("You win "+oddeven+" coins. You now have "+roddeven+" coins remaining.");
      }
      else{
        System.out.print("You lose "+lose+" coins. You now have "+rlose+" coins.");
      }
    }
  }
}

所以基本上如果我滚动所有相同并赢得40所以我现在有45我希望代码再次运行与45硬币相对于我以前的随机数量的硬币

1 个答案:

答案 0 :(得分:1)

基本上就像人们提到的那样你需要写一个循环。有几种不同的类型,check them out

虽然语法非常简单,但我会使用while循环:

while(someCondition) {
    // execute some stuff
}

我冒昧地看着你的代码,因为你说你的CS老师不好(我确定他确实有很多有价值的信息)因为我很无聊而且目前什么都没有要做(至少我想做)。

我的一个小小的烦恼就是人们在编写代码时没有考虑到可维护性,我知道你是初学者,所以很早就养成了这个习惯,我给你写了一段代码看看这个应用程序看起来像我写的那样(我认为自己是一个普通的Java程序员),希望能激励你看看你能用你的应用程序实现什么。

我的应用基于您的应用。

您会注意到我使用了许多旨在阻止我反复编写相同代码的方法,例如generateNumber(int max, int min)determineResult(int bet, int payout)play(game game) 。我实际上可以为许多不同的游戏定义规则并重复使用相同的代码,以便允许用户玩游戏,目前我只定义了2来证明这一点。

此外,通过在方法中封装程序的不同部分的功能,我可以轻松地将它们更改为新的东西,让不同的游戏指向不同的方法,甚至从一组其他游戏构建新的游戏'创建一些非常复杂的东西的方法,甚至不需要编写更多代码(或者至少是这个想法)。

/**
 * 
 * The Casino Slot game
 * 
 */
public class CasinoSlot {

    /** The payout multiplier for the THREE_DICE_SAME_3 result */
    private static final int PAYOUT_THREE_DICE_SAME_3 = 2;
    /** The payout multiplier for the THREE_DICE_SAME_2 result */
    private static final int PAYOUT_THREE_DICE_SAME_2 = 1;
    /** The payout multiplier for the THREE_DICE_ODD_EVEN result */
    private static final int PAYOUT_THREE_DICE_ODD_EVEN = 1;

    /** The payout multiplier for the FIVE_DICE_SAME_5 result */
    private static final int PAYOUT_FIVE_DICE_SAME_5 = 10;

    /** The payout multiplier for the LOSS result */
    private static final int PAYOUT_LOSS = 0;

    /** The player's coins */
    private int coins;

    private enum game {
        THREE_DICE, FIVE_DICE
    }

    /**
     * The program entry point
     * 
     * @param args
     *            The program args
     */
    public static void main(String[] args) {
        new CasinoSlot();
    }

    /**
     * The CasinoSlot constructor
     */
    public CasinoSlot() {
        coins = generateNumber(20, 5); // generate initial coins
        while (coins > 0) { // game loop
            chooseGame();
        }
    }

    /**
     * Chooses which game to play
     */
    private void chooseGame() {
        if (coins < 10) {
            // desperate times...
            play(game.FIVE_DICE);
        } else {
            play(game.THREE_DICE);
        }

    }

    /**
     * Generates a random number between max and min or zero if max-min <= 0
     * 
     * @param max
     *            The max number
     * @param min
     *            The min number
     * @return the random number
     */
    private int generateNumber(int max, int min) {
        if (max - min <= 0)
            return 0;
        return (int) (Math.random() * (max - min) + min);
    }

    /**
     * Determines the payout
     * 
     * @param bet
     *            The bet
     * @param payout
     *            The payout multiplier
     * @return The payout
     */
    private int determineResult(int bet, int payout) {
        int win = bet * payout;
        System.out.println("You win " + win + " coins.");
        return win;
    }

    /**
     * Called per play cycle of the application
     */
    public void play(game game) {
        System.out.println("You have " + coins + " coins.");
        int bet = generateNumber(4, 0); // determine bet
        System.out.println("You bet " + bet + " coins.");

        // analyse bet
        if (bet == 0) { // no bet
            System.out.println("Please try again.");
        } else if (coins - bet < 0) { // can't bet
            System.out.println("You don't have that much!");
        } else { // bet placed
            coins -= bet; // update balance

            // determine game
            switch (game) {
            case THREE_DICE:
                System.out.println("Play 3 dice...");
                coins += playThreeDice(bet);
                break;
            case FIVE_DICE:
                System.out.println("Play 5 dice...");
                coins += playFiveDice(bet);
                break;
            }
        }
        System.out.println("");
    }

    /**
     * The Three Dice rules
     * 
     * @param The
     *            bet amount
     * @return The result
     */
    private int playThreeDice(int bet) {
        int result;

        int d1 = generateNumber(6, 1);
        int d2 = generateNumber(6, 1);
        int d3 = generateNumber(6, 1);

        System.out.println("You rolled: " + d1 + ", " + d2 + ", " + d3);

        if (d1 == d2 && d2 == d3) {
            result = determineResult(bet, PAYOUT_THREE_DICE_SAME_3);
        } else if (d1 == d2 || d2 == d3 || d1 == d3) {
            result = determineResult(bet, PAYOUT_THREE_DICE_SAME_2);
        } else if ((d1 % 2 == 0 && d2 % 2 == 0 && d3 % 2 == 0) || (d1 % 2 != 0) && (d2 % 2 != 0) && (d3 % 2 != 0)) {
            result = determineResult(bet, PAYOUT_THREE_DICE_ODD_EVEN);
        } else {
            result = determineResult(bet, PAYOUT_LOSS);
        }
        return result;
    }

    /**
     * The Five Dice rules
     * 
     * @param The
     *            bet amount
     * @return The result
     */
    private int playFiveDice(int bet) {
        int result;

        int d1 = generateNumber(6, 1);
        int d2 = generateNumber(6, 1);
        int d3 = generateNumber(6, 1);
        int d4 = generateNumber(6, 1);
        int d5 = generateNumber(6, 1);

        System.out.println("You rolled: " + d1 + ", " + d2 + ", " + d3 + ", " + d4 + ", " + d5);

        if (d1 == d2 && d2 == d3 && d3 == d4 && d4 == d5) {
            result = determineResult(bet, PAYOUT_FIVE_DICE_SAME_5);
        } else {
            result = determineResult(bet, PAYOUT_LOSS);
        }
        return result;
    }

}