Monica and Chandler at the craps table.
In this lab, you will build a version of the dice game "craps"!
cstdlib
has a function
called rand
which generates "random" numbers. The
rand
function returns an integer between 0 and RAND_MAX (a number
defined in cstdlib
). The number returned will at least appear to
have been randomly chosen in the range.
More specifically, to use the rand function, you must include the
cstdlib
library. The prototype for rand
is:
int rand(); // needs #include <cstdlib>
For example, the following code will output a random number:
#include <iostream>
#include <cstdlib> // include cstdlib!!
using namespace std;
int main()
{
int num = rand(); // call rand()!!
cout << "Random number: " << num << endl;
return 0;
}
p1.cpp
that simulates the following:
int rolldie();
This function simulates a single roll of a single 6-sided die.
Do not change the prototype of this function!
rolldie()
function as follows:
rand()%8
.
rand()%6
. This would certainly give you a number in the range
0,1,2,3,4,5 - and in fact you can do it this way and it will work (you may need
to add 1 to get the range to be 1 up to 6).
However, it turns out that what rand()
gives us is 31 random bits,
which means a random(ish) number between 0 and 231 - 1. In other
words, there are 231 possibilities. Since 231 is
not a multiple of 6, I am slightly more likely to get 0 or 1 out of
this than other numbers.
On the other hand, if you do rand()%8
, there's no bias; this
returns a number in the range 0, 1,2,3,4,5,6,7 with equal probability (since
231 is a multiple of 8). But you simulate a 6-sided die
instead of 8! So you keep doing this until rand()%8
is actually
in the desired range of 1 up to 6.
Player rolled 6 + 1 = 7 Player rolled 3 + 1 = 4 Player rolled 2 + 4 = 6 Player rolled 1 + 5 = 6 Player rolled 2 + 3 = 5
Answer: The output is always the same.
Nothing random about it!
The reason for this repeating sequence is not accidental. In order to debug a
program that uses a random number generator like rand()
, it may be
important to repeat the same sequence.
rand()
srand
to "seed" rand
. The idea
is that the particular sequence of seemingly random numbers rand
produces will actually depend on the seed value - different seed values will
produce wildly different sequences.
srand()
in your program?
Answer (drag a mouse): only once
rand()
in your program?
Answer (drag a mouse): many times (i.e., every time you need to choose a random number)
srand()
in your program?
Answer (drag a mouse): before the the first function call of rand()
Add the following code snippet to your p1.cpp
before using the
rand()
function:
int seed;
cout << "Enter seed value: ";
cin >> seed;
srand(seed);
Your program should work exactly as follows:
~/$ ./p1 Enter seed value: 7 Player rolled 5 + 3 = 8 Player rolled 3 + 5 = 8 Player rolled 3 + 2 = 5 Player rolled 1 + 1 = 2 Player rolled 3 + 1 = 4 | ~/$ ./p1 Enter seed value: 20 Player rolled 2 + 1 = 3 Player rolled 5 + 6 = 11 Player rolled 1 + 1 = 2 Player rolled 6 + 4 = 10 Player rolled 1 + 1 = 2 |
~/bin/submit -c=IC210 -p=lab06 p1.cpp
int throwdice();
This function simulates a single throw of two dice and the
processing of that role within the game.
Output of throwdice(): The function returns an integer according to the following rule.
situation | return value |
if the house wins | -1 |
if the player wins | 0 |
if neither wins | the actual sum of the two dice rolls |
Using the function throwdice()
, your program will simulate craps
up until either the player or the house wins.
Note 1: You're going
to have to give some thought to which function is responsible for printing
what.
Note 2: Your output format must match the examples
exactly!
Sample runs are shown below:
~/$ ./p2 Enter seed value: 1 Player rolled 6 + 1 = 7 Player wins! ~/$ ./p2 Enter seed value: 12 Player rolled 2 + 2 = 4 roll again Player rolled 3 + 5 = 8 roll again Player rolled 1 + 1 = 2 House wins! |
~/$ ./p2 Enter seed value: 10 Player rolled 6 + 5 = 11 Player wins! ~/$ ./p2 Enter seed value: 2018 Player rolled 1 + 5 = 6 roll again Player rolled 4 + 2 = 6 roll again Player rolled 6 + 5 = 11 Player wins! |
~/bin/submit -c=IC210 -p=lab06 p1.cpp p2.cpp
Below are some sample outputs.
~/$ ./p3 Enter seed value: 107 Player rolled 5 + 5 = 10 setpoint is 10! Player rolled 4 + 4 = 8 roll again Player rolled 5 + 3 = 8 roll again Player rolled 6 + 2 = 8 roll again Player rolled 6 + 1 = 7 House wins! Play again? y Player rolled 5 + 4 = 9 setpoint is 9! Player rolled 3 + 1 = 4 roll again Player rolled 2 + 2 = 4 roll again Player rolled 2 + 3 = 5 roll again Player rolled 2 + 4 = 6 roll again Player rolled 1 + 2 = 3 roll again Player rolled 1 + 1 = 2 roll again Player rolled 5 + 6 = 11 roll again Player rolled 3 + 2 = 5 roll again Player rolled 2 + 3 = 5 roll again Player rolled 1 + 2 = 3 roll again Player rolled 5 + 1 = 6 roll again Player rolled 6 + 2 = 8 roll again Player rolled 2 + 5 = 7 House wins! Play again? y Player rolled 5 + 3 = 8 setpoint is 8! Player rolled 1 + 1 = 2 roll again Player rolled 2 + 1 = 3 roll again Player rolled 1 + 5 = 6 roll again Player rolled 2 + 3 = 5 roll again Player rolled 2 + 2 = 4 roll again Player rolled 4 + 1 = 5 roll again Player rolled 1 + 3 = 4 roll again Player rolled 3 + 3 = 6 roll again Player rolled 4 + 6 = 10 roll again Player rolled 6 + 1 = 7 House wins! Play again? y Player rolled 2 + 1 = 3 House wins! Play again? y Player rolled 1 + 2 = 3 House wins! Play again? n |
Hint: think about adding a parameter setpoint to your throwdice() function. This would allow you to deal with throws after the first. Of course you still have to deal with the first throw. You might make different functions to distinguish the first from the following throws. Or you might use arguments to throwdice() to determine which case you're in. Or you might do something altogether different.
~/bin/submit -c=IC210 -p=lab06 p1.cpp p2.cpp p3.cpp
~/bin/submit -c=IC210 -p=lab06 p1.cpp p2.cpp p3.cpp p4.cpp