Modify Gambler.java to take an extra command line parameter that specifies the number of bets the gambler is willing to make so that there are three possible ways for the game to end:
* the gambler wins, loses, or runs “out of time” meaning the gambler gives up.
* Add to the output to give the expected amount of money the gambler will have when the game ends.
Think of the different outcomes and how would report on them. That means what your output would look like and how you would display it. Look back to the format of the previous gambler’s ruin activity.
A random(get it?) example:
stake: $5 goal: $10 trials: 10 games and run out of time is 7 bets
1 game – lost all your money
2 game – won
3 game – end by the 7th bet and you had $6
4 game – lost all your money
5 game – end by the 7th bet and you had $4
6 game – lost all your money
7 game – won
8 game – end by the 7th bet and you had $2
9 game – won
10 game – lost all your money
6 + 4 + 2 = 12 total accumulated from “run out of time”
the expected amount of money from 10 games is 12/3 = $4
Lesson slides on page 28
public class Gambler
{
public static void main(String[] args)
{
int stake = Integer.parseInt(args[0]);
int goal = Integer.parseInt(args[1]);
int trials = Integer.parseInt(args[2]);
int wins = 0;
for (int i = 0; i < trials; i++)
{
int t = stake;
while (t > 0 && t < goal)
{
if (Math.random() < 0.5) t++;
else t--;
}
if (t == goal) wins++;
}
StdOut.println(wins + " wins of " + trials);
}
}