aputamkon
aputamkon

Reputation: 11

having a problem with java for game simulation

User inputs integer value for gamblers starting bankroll User inputs integer value for gamblers desired bankroll – if the gambler’s bankroll reaches this value he quits the game User inputs integer value for the number of trials to perform – each trial will consist of enough games to either reduce the gamblers bankroll to zero or increase it to the desired bankroll Declare an integer variable (set to zero) to keep track of the number of wins The solution, obviously, will consist of nested looping structures and selection structures First (outer loop) will loop to perform the required number of trials Set cash equal to stake Second (inner loop) will simulate the results of one card game. This loop will repeat as long as cash is greater than zero and less than the desired bankroll Assume that the gambler has chance of winning the game of less than 50% Use a random number generator to determine if the gambler won the game If the gambler won, add $1.00 to his cash Otherwise subtract $1.00 from his cash At the end of the inner loop (one game is run) - If the value of cash equals the gamblers desired bankroll, then increment wins by one After the outer loop stops, print to the screen the number of wins out of the number of trials and the percent of games won.

trying to get a game simulator to count the number of games won after the total starting amount reaches the end amount. part of it works but not all of it then it should do that x number of times. https://github.com/samuelatlas/gamesimulation/tree/master ''' // demonstrating the Java for loop

import java.util.Scanner; // import a scanner c
import java.security.SecureRandom;  // imports a secure random class

class newLoopTest1
{
    public static void main(String[] args)
    {
        // OUTER -------LOOP
        Scanner input = new Scanner(System.in);

        System.out.print("Enter the start bankroll  ");
        int startBankRoll = input.nextInt();
        //int desiredBankRoll = 5;

        System.out.print("Enter the desired bankroll  ");
        int desiredBankRoll = input.nextInt();

        System.out.print("Enter the number of trials  ");
        int numberTrials = input.nextInt();

        //int startBankRoll = 2;
        int i = 1;
        int current = startBankRoll;
        int wins = 0;

        //int numberTrials = 0;
        //OUTER----LOOP
        while(i <= numberTrials)
        //while(numberTrials <= 4)
        {


            i++;
            int innerloop = 0;
            System.out.println("printing from outer");

            //INNER----LOOP
            while((startBankRoll < desiredBankRoll) && (startBankRoll > 0))
            {
                SecureRandom randomNumber = new SecureRandom();
                int number = randomNumber.nextInt(101);
                System.out.println("Before hand start amount of " + 
startBankRoll + " end amount of " + desiredBankRoll);
                System.out.println("Rolled " + number);
                if( number <= 50)
                {
                    System.out.println("lost");
                    startBankRoll--;
                    System.out.println("After hand start amount of " + 
startBankRoll + " end amount of " + desiredBankRoll);
                }
                else
                {
                    System.out.println("won");
                    startBankRoll++;
                    System.out.println("After hand start amount of " + 
startBankRoll + " end amount of " + desiredBankRoll);
                }




                System.out.println(" Outerloop ran " + numberTrials + " 
Innerloop ran " + innerloop);
                innerloop++;


                //INNER----LOOP
            }

            //OUTER----LOOP
            numberTrials += 1;
            //wins++;
            System.out.println("Current" + current);
            if(startBankRoll == desiredBankRoll)
            {
                wins += 1;
                startBankRoll = current;
                System.out.println("wins" + wins);

            }
            else
            {
                startBankRoll = current;
                System.out.println(" lost all cash");
            }


            //OUTER----LOOP
        }
        int totalWins = (wins/(numberTrials-1));
        System.out.println("Won " + wins + " out of " + (numberTrials-1));
        //System.out.println("total percent" + wins/totalWins );
    }
}

Upvotes: 1

Views: 153

Answers (2)

aputamkon
aputamkon

Reputation: 11

well i figured it all out just took a while and lots of versions. here is the final code. most of the earlier code was to see where the numbers where going. {import java.util.Scanner; // import a scanner class. import java.security.SecureRandom; // imports a secure random class.

class TheGambler

    public static void main(String[] args)
    {
        // OUTER -------LOOP AREA
        // create scanner for object.
        Scanner input = new Scanner(System.in);

        //prompt users for the starting bankroll amount.
        System.out.print("Enter the start bankroll  ");
        int startBankRoll = input.nextInt();

        //prompt users for the desired bank roll amount.
        System.out.print("Enter the desired bankroll  ");
        int desiredBankRoll = input.nextInt();

        //prompt users for the number of tirals.
        System.out.print("Enter the number of trials  ");
        int aNumber = input.nextInt();

        //to reset the value after to inner loop has ran.
        int current = startBankRoll;

        // keep track of number of wins.
        int wins = 0;

        // keep track of numberTrials.
        int numberTrials = 1;

        //OUTER----LOOP AREA
        //condition for the outer while loop to continue.
        while(numberTrials <= aNumber)
        {

            // number of time inner loops executes.
            int innerloop = 0;

            //INNER----LOOP
            // condition for the inner while loop to continue.
            while((startBankRoll < desiredBankRoll) && (startBankRoll > 0))
            {
                //create a random number and assign it an integer named number.
                SecureRandom randomNumber = new SecureRandom();
                int number = randomNumber.nextInt(101);

                //condition to determine if player wins or a losses.
                if( number <= 50)
                {
                    // if losses subtract one from startamount.
                    startBankRoll--;

                }
                else
                {
                    // if wins adds one to startamount.
                    startBankRoll++;

                }
                // add one to the inner loop count.
                innerloop++;


                //INNER----LOOP AREA
            }

            //OUTER----LOOP AREA
            //add to the total number of trials ran
            numberTrials += 1;

            // condition to add one to wins if startamount is equal to desiredamount.
            if(startBankRoll == desiredBankRoll)
            {
                // adds one to the wins count and resets the startamount.
                wins += 1;
                startBankRoll = current;

            }
            else
            {
                //if startamount equals zero reset the startamount.
                startBankRoll = current;

            }

            //OUTER----LOOP AREA
        }

        // determine total number of games played.
        int total = (numberTrials-1);

        // converts the amount of wins to a percent.
        int percent = wins * 100 / total;

        //displays how many wins out of total amount of games played.
        System.out.println("Won " + wins + " out of " + total);

        //displayes the percent of games won.
        System.out.println(percent + "%");
    }
}

Upvotes: 0

MarsAtomic
MarsAtomic

Reputation: 10673

The main problem with your code seems to lie with understanding the problem. I took at look at the Github page you linked (I noticed your assignment is due tomorrow -- please do not wait until the last minute to ask for help in the future, and always ask the teacher first, rather than some stranger on Stack Overflow). Let's break down the assignment properly.

The player starts with cash (in your case, 2 units), so we know how to initialize startCash, which you've done properly

His goal is to get to 10 units or bust, so we know the upper and lower limits that define the parameters for his participation in the game. In other words, he only plays while he has > 0 and < 10 units. An outer loop checking to see if he has enough cash is pointless.

While those conditions are true, he plays a coin flipping game, where 50 or less is a loss of one unit and 51 or more is a win of one unit. Each time he flips, we increment a counter so we know how many coin flips he conducted to get to either 0 or 10.

Notice how I've rephrased the question: While cash > 0 and cash < 10, flip coin. If flip < 50, player loss, else win. Increment counter. That's all there is to it, all in one loop.

You confused yourself by adding an outer loop which you don't need at all -- maybe you put it there to keep flipping while the player has money, but it's redundant because your do...while is checking both the lower and upper limits for whether the game should be played. That outer loop is also running 5 times, but what if it takes more than 5 trials to bust or get 10?

I've simplified the code here by basically rearranging what you already had. Compare what you have to what I have and you'll see that I more or less just stripped away the useless outer loop. Run the code a few times and you'll see that you already had more or less the correct logic before you shot yourself in the foot.

import java.security.SecureRandom;

public class Homework
{
  public static void main(String[] args)
  {
    int startCash = 2;
    int endCash = 10;
    int currentCash = startCash;
    int counter = 0;

    while(currentCash > 0 && currentCash < endCash)
    {
      SecureRandom randomNumber = new SecureRandom();
      int number = randomNumber.nextInt(101);

      if(number <= 50)
      {
        // lost
        currentCash--;
      }
      else
      {
        // won
        currentCash++;
      }

      counter++;
    }

    System.out.println("Current Cash: " + currentCash);
    System.out.println("Trials: " + counter);

  }
}

The only "major" change other than removing the extra loop is changing your do...while into a while loop. The difference is that a do...while will always run at least once because the exit condition isn't checked until after the code block runs, which doesn't seem correct because what if startCash is already 0 or 10? The while loop checks the condition before running the code block, so if the player is ineligible to play (too much or too little cash), then he doesn't play.

Upvotes: 1

Related Questions