The goal of this assignment is to get you started with the basics of Java and to get you to think about how to translate a relatively simple algorithm into code.
In this assignment, you will write a sequence of programs designed to evaluate three betting strategies for a basic coin toss game. In this game, a player is given a starting amount of cash (perhaps $50) and repeatedly bets an amount of money (perhaps $1 or $2) on the outcome of a fair coin toss. For each coin toss, the player decides how much money to bet - this decision is determined using one of the three strategies, which are described later. The simplest strategy, for example, is to always bet exactly $1. The player will continue to play the game until one of three events happens
We can run one trial of this game, observe its outcome and perhaps find it interesting. Given a computer, we can also run this game for thousands of trials to understand more about the game itself. It becomes easy to answer questions like "How likely am I to win $500 if I only start off with $50?" and "Which betting strategy is best in this situation?" Your code will have two nested loops, how do these loops correspond to the game and the many trials of the game?
Some of the strategies that you will simulate, such as the "one dollar" strategy, can be solved using pen, paper, and some knowledge of probability and statistics. Our method of repeated simulation is another approach to understanding this sort of problem that will allow us to solve much more complex strategies ("Fibonacci", for example) which might not be solved so easily using analytic methods. In general, simulation is an excellent tool for dealing with the increasingly complex problems that we face as a society.
A reminder about seeking help: if after carefully reading the details
of any part of the assignment, you are still confused about how to get
started or make progress, send mail to the course staff to ask for
help or come to office hours.
We will be using a software version control system named svn and an svn server named PhoenixForge. We have already set up a PhoenixForge accounts for most of the students in CS121. Unless you hear from me by email, you can assume that you already have an account.
Each student will have a code repository on PhoenixForge. You will work with the code in this repository by checking it out, modifying it, and then reporting your changes back to the repository. To check out your repository, use the following commands:
replacing <your CNET ID> with your CNET ID. You will be prompted for your CNET password.cd workspace svn co https://phoenixforge.cs.uchicago.edu/svn/<your CNET ID>-cs121
If this command is successful, you will see a new sub-directory called <your CNET ID>-cs121 in your workspace directory. Do an ls of this new directory and you will see that we have seeded your repository with a directory named hw1, which contains a file named Dollar.java and sample output for this assignment.
Once you change to the hw1 directory, you will be able to edit Dollar.java with a text editor (I like emacs and gedit is particularly simple) and compile it using javac.
When you are done working for the day, check your changes back into PhoenixForge using thef following command:
Check your code in when you are done working even if you have not finished the assignment.svn ci -m"<short descriptive comment>"
All three of your programs will take the same type of input and produce the same type of output. Each program will take an optional flag (-f) followed by four command line arguments:
For example, to simulate 1000 trials of a game where the player begins with $50, aims to reach $250 and will play no more than 20000 rounds, you would type the following the console:
which would yield:java Dollar -f 50 250 1000 20000
Dollar -f 50 250 1000 20000
number of successful trials: 119
average number of bets for successful trials: 11794
average number of bets for unsuccessful trials: 7211
Your program will produce different output if you omit the -f
flag.
Note: Your program should omit the output line for the average
number of bets for successful (unsuccessful) trials, if the number of
successful trials (unsuccessful) trials is zero.
Put your name in Dollar.java before you start this task!.
The file Dollar.java contains code that parses the command-line arguments and includes the code from your textbook that simulates the basic dollar betting system (see Sedgwick and Wayne, Program 1.3.8). Modify this code to obtain the intended output. In order to do this, you need to:
The first part requires you to keep a tally of bets made in each trial (as opposed to the total number of bets across all the trials). Once you have modified the code to keep track of this number, add a condition in the while-loop such that it stops when the number of bets reaches a stipulated cap.
For the second part, we do not know if a trial is successful until it is over. Using the tally of bets made in each trial and depending on whether the trial was successful, update the
Introduce new variables to store these values.
For the third part, you will need to add print statements for the desired output. Remember to check for divide-by-zero cases.
We added fewer than 15 lines of code to Dollar.java for this
task.
After you copy the file, replace all occurrences of Dollar in the code with DAlembert in the new file. You will also need to change the comment at the top of the file to describe the new betting system.cp Dollar.java DAlembert.java svn add DAlembert.java
In this task, you will implement the D'Alembert betting system. In this system, the player chooses a starting bet amount and then plays according to the following rules:
In the Dollar strategy, the player declares victory when he reaches his target. Using the D'Alembert strategy, the player can exceed his goal. Why? How do you need to change your code to account for this problem?
Use an initial bet amount of $4 for all experiments.
Here is an example where the player starts with $20 in cash and an initial bet of $4:
| Round | Outcome | Cash on hand After | Bet amount After | Rule |
|---|---|---|---|---|
| 0 | _ | 20 | 4 | start |
| 1 | L | 16 | 5 | 3 |
| 2 | W | 21 | 4 | 1 |
| 3 | W | 25 | 3 | 1 |
| 4 | W | 28 | 2 | 1 |
| 5 | W | 30 | 4 | 2 |
This example does not use the fourth rule, but it is important to make sure that the player never bets more than he has.
You will need to adapt your code to keep track of the current bet amount. Also, you will need to modify the consequences of winning and losing a round accordingly and add code for dealing with the special cases introduced by rules 2 and 4.
We added fewer than ten lines of code to our version of Dollar.java
from Task 1 for this task.
In this task, you will implement the Fibonacci betting strategy. Use cp and svn add to copy your code from Dollar.java to Fibonacci.java and modify it accordingly.
The Fibonacci strategy uses an approach similar to DAlembert, but the bet amounts follow the Fibonacci sequence (1, 1, 2, 3, 5, etc). In this strategy a steady sequence of unsucessful bets can quickly result in very large bet amounts. In this approach, the player
Here's a sample game where the player begins with 20 dollars and initially bets one dollar:
| Round | Outcome | Cash on hand After | Bet amount After | Rule |
|---|---|---|---|---|
| 0 | _ | 20 | 1 | start |
| 1 | L | 19 | 1 | 1 |
| 2 | L | 18 | 2 | 1 |
| 3 | L | 16 | 3 | 1 |
| 4 | L | 13 | 5 | 1 |
| 5 | W | 18 | 2 | 2 |
| 6 | W | 20 | 1 | 2 |
| 7 | W | 21 | 1 | 3 |
| 8 | L | 20 | 1 | 2 |
up: Fi+1 = Fi-1 + Fi
down: Fi-2 = Fi - Fi-1
Your program should use one variable to track the current bet amount
(Fi) and another to track the previous bet
(Fi-1). Also, you will need to use a temporary variable to
perform the above computations. Whenever the player is already at the
first term in the sequence, he cannot go back any further in the
sequence. In this case, simply set the bet amount to be $1 and the
previous bet amount to $0.
If the bet amount is more than the player's cash on hand, your program
will need to cap the bet amount to the amount of cash he has on
hand. If the player loses this bet, he loses the trial. However, if he
wins this bet, his betting pattern should continue to resemble a
Fibonacci series. This may be difficult if, by capping his bet, we used a number that wasn't in the series. You can fix this by changing the variable
that tracks the previous bet amount to be approximately 0.61803 of his
cash on hand at the time (rounded down to the nearest integer). For
example, if the player has $10 and the next bet in the sequence should
be $13, set the bet amount to $10 and the previous bet to $6.
The reason for this approach is as follows. The ratio of consecutive terms in the
Fibonacci sequence tends towards the Golden Ratio as the index of the
term increases: limn→∞(Fn+1/Fn)
≈ 1.61803, limn→∞(Fn/Fn+1)
≈ 0.61803. This provides a suitable way to generate a series that is
similar to the Fibonacci sequence. Again, you need to make sure that
this
amount is not less than $1.
We added fewer than ten lines of code to our version of Dollar.java
from Task 1 to complete this task.
To submit your assignment, check your code into PhoenixForge:
We will grade the last version you check in. We recommend checking in your code early and often.svn ci -m"final version ready for submission"
This assignment was developed originally by Cong Han Lim and has
since been modified by Anne Rogers