3
\$\begingroup\$

I am new to the coding world as well as this site. I have been learning Java by myself for a few days now and wanted to make a gambling program just for fun and practice.

I am going to ask two questions, one short, and one long. I'll ask the long one first! How does my code look for a beginner? I know I haven't dipped into using multiple classes yet or anything, so how does this very basic program look?

package com.company;

import java.util.Scanner;
import java.util.Random;

public class StakingGame {

    static int cashStack = 500000;
    static int amountStaked;

    public static void main(String[] args) {
        System.out.println("Welcome to my staking game.");

        while(cashStack > 0) {
            amountStaked();
            System.out.println("Rolling...");

            whoWon();
        }
        System.out.println("You have no more money.");

    }

    public static void amountStaked() {
        Scanner scan = new Scanner(System.in);
        System.out.println("How much would you like to stake?");

        amountStaked = scan.nextInt();

        System.out.println("You have chosen to stake, " + amountStaked + "$, goodluck.");

    }

    public static int roll() {
        Random r = new Random();

        int min = 0;
        int max = 100;
        int rolled = r.nextInt(max-min) + min;

        return rolled;
    }

    public static void whoWon() {

        int playerRoll = roll();
        int computerRoll = roll();

        System.out.println("You have rolled a " + playerRoll);
        System.out.println("Your oppent has rolled a " + computerRoll);

        if(playerRoll > computerRoll) {
            stakeWon(0);
        } else if (playerRoll < computerRoll) {
            stakeLost(0);
        } else {
            System.out.println("You guys tied.");
        }
    }

    public static int stakeWon(int newAmount) {

        newAmount = cashStack + amountStaked;
        cashStack = newAmount;

        System.out.println("You have won the stake! You now have " + cashStack);
        return cashStack;
    }

    public static int stakeLost(int newAmount) {

        newAmount = cashStack - amountStaked;
        cashStack = newAmount;

        System.out.println("You have lost the stake.. You now have " + cashStack);
        return cashStack;
    }
}

My second question is, well, more of a question. I want this program to run until I either terminate it or the player runs out of money. So this is how I decided to go about that. I created this loop in my main method:

public static void main(String[] args) {
    System.out.println("Welcome to my staking game.");

    while(cashStack > 0) {
        amountStaked();
        System.out.println("Rolling...");

        whoWon();
    }
    System.out.println("You have no more money.");

}

Is that the best way to make the program run until you either lose or I terminate it?

\$\endgroup\$
0

2 Answers 2

2
\$\begingroup\$
  1. I am not a beginner but I have the cockiness to try to think like a beginner for this moment. For a beginner this is a good approach. As you already mentioned a professional is missing a little bit of object-orientation.

  2. Yes. The only improvement I see is to

    1. Print out Exit the game by losing all your money (or more) or hit ctrl+x.
    2. Extract the 0 to a constant.
    3. Convert the head-controlled-loop into a foot-controlled-loop. You wrote I want this program to run until I either terminate it or the player runs out of money, what is indicating that the player is playing at least one round, but that head-controlled-loop terminates if the Player has no money at start. Having a do{...}while(cachStack > 0) might be better.
\$\endgroup\$
3
\$\begingroup\$
    static int cashStack = 500000;
    static int amountStaked;

When you make a static class field, you are saying that every object of the class should share it. E.g.

    public static Scanner scan = new Scanner(System.in);
    public static Random r = new Random();

Now you can use this one Scanner and one Random throughout the program.

But that's not necessary for cashStack or amountStaked.

    private int cashStack;

    public StakingGame(int amount) {
        cashStack = amount;
    }

We can set the visibility to private, as we only want this class to be able to access the cashStack.

Then you'd use it like

    public static void main(String[] args) {
        System.out.println("Welcome to my staking game.");

        StakingGame game = new StakingGame(500000);
        while (game.hasMoneyLeft()) {
            int amount = inputStake();
            System.out.println("Rolling...");

            game.play(amount);
        }

        System.out.println("You have no more money.");
    }

Now we can set the value of the bank when we create the game.

You shouldn't directly access class variables in the main method. So I added the hasMoneyLeft method.

    public boolean hasMoneyLeft() {
        return cashStack > 0;
    }

As a general rule, classes and objects should have noun names while methods have verb names. This is because classes and objects represent things while methods are actions.

This way we get the staked amount via user input and then pass that to the play method. This is better because the amount staked isn't a property of the game. It is entered every round. It should not persist between rounds.

    public static int amountStaked() {
        System.out.println("How much would you like to stake?");

        int amountStaked = scan.nextInt();

        System.out.println("You have chosen to stake, " + amountStaked + "$, good luck.");

        return amountStaked;
    }

I moved the Scanner to a class field. So we don't have to create a new one each time this method is called.

I made this method static because it doesn't use object state. That is to say, it doesn't use cashStack. You might consider modifying it so that it does use cashStack to limit the wager. You could enforce a rule that the wager is less than or equal to the remaining money.

You could enforce a rule that wagers have to be greater than zero.

    public static int roll() {
        Random r = new Random();

        int min = 0;
        int max = 100;
        int rolled = r.nextInt(max-min) + min;

        return rolled;
    }

We moved the Random to a class field, so we don't need to create it here.

You have two numbers that are effectively constants. Consider making them constants.

    private static final MAXIMUM = 100;

Or leaving them out altogether. This could just be

    public static int roll() {
        return r.nextInt(MAXIMUM);
    }

Your logic is correct if there is a minimum, but you don't need one. The 0 will give the same results as this.

    public static int stakeWon(int newAmount) {

        newAmount = cashStack + amountStaked;
        cashStack = newAmount;

This looks rather weird. Consider

    public static void stakeWon(int amountStaked) {
        cashStack += amountStaked;

You don't use the parameter (newAmount) except to immediately set it to something else. You could just say

        int newAmount = cashStack + amountStaked;

But that's not really necessary. Using the += operator does exactly what we want here, increasing the bank by the amount won.

You don't use the return value, so it makes more sense for this to be void.

I moved the amountStaked out of being a class field, so we have to pass it into the method.

    public static void play(int wager) {
        int playerRoll = roll();
        int computerRoll = roll();

        System.out.println("You have rolled a " + playerRoll);
        System.out.println("Your opponent has rolled a " + computerRoll);

        if (playerRoll > computerRoll) {
            stakeWon(wager);
        } else if (playerRoll < computerRoll) {
            stakeLost(wager);
        } else {
            System.out.println("You guys tied.");
        }
    }

Added a wager variable. It would be more consistent to call it amountStaked, but I prefer the name wager. It's shorter and in my opinion, clearer.

Fixed the spelling of "opponent."

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.