Monica and Chandler at the craps table.
In this lab, you will build a version of the dice game "craps"!
rand() Functioncstdlib contains a function rand() that
you can use.
#include <cstdlib>
It returns a random int in the range 0,....,RAND_MAX, where
RAND_MAX is usually 231 - 1, defined in cstdlib.
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
// Print out random numbers
for(int i = 0; i < 5; i++)
cout << rand() << " ";
cout << endl;
return 0;
}
The program will output 5 random numbers. If I run this, I get:
1804289383 846930886 1681692777 1714636915 1957747793
Answer: The output is always the same.
In a sense, there is a fixed sequence and rand() will pull up a
number in the sequence one by one. To reiterate, each individual number from
rand() looks random, but the random-looking sequence itself is fixed.
srand() Functionsrand(), which seeds
rand(). Essentially, when you give srand a seed, it
uses the seed to pick a sequence.
| Here are sample runs:
$ ./a.out seed: 1 1804289383 846930886 1681692777 1714636915 1957747793 $ ./a.out seed: 1 1804289383 846930886 1681692777 1714636915 1957747793 $ ./a.out seed: 3 1205554746 483147985 844158168 953350440 612121425 $ ./a.out seed: 3 1205554746 483147985 844158168 953350440 612121425 $ ./a.out seed: 100 677741240 611911301 516687479 1039653884 807009856 |
srand(): only one time in the beginning of main()srand() in the beginning of the program. You need to do
it only once.
rand().
p1.cpp that simulates the following:
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
int rolldie();
This function simulates a single roll of a single 6-sided die.
Warning: Do not change the prototype of this function!
rolldie() function as follows:
rand()%8.
rand()%6? It will give one of the six
numbers and we don't need to use a loop.
That will actually work. However, it turns out that what rand()
gives us a random number between 0 and 231 - 1, and rand()%6 is
slightly more likely to become 0 or 1 than other numbers.
On the other hand, rand()%8 wil give a random number from (0, 1,
..., 7) equaliy likely, but then to get a number between 1 and 6, we may need to
try again.
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): in the beginning of your program
srand() Add the following code snippet to your p1.cpp in the beginning
of the main() function.
int seed;
cout << "Enter seed value: ";
cin >> seed;
srand(seed);
~/$ ./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
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! |
int throwdice();
Function throwdice() simulates a single throw of two dice and the processing of that role within the game.
| situation | return value |
| if the house wins | -1 |
| if the player wins | 0 |
| if neither wins | the actual sum of the two dice rolls |
main(), you must not call rolldie().
throwdice(), call
rolldie() appropriately. Remember that rolldie()
simulates a throw of a die.
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!
~/bin/submit -c=IC210 -p=lab06 p1.cpp p2.cpp
The owner of the casino, runs analysis on your craps program and sees the odds
are stacked in favor of the player. He orders you to implement a version that
favors the house. After showing your game to a math professor, the following
rules are added to your game.
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. |
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 |
~/bin/submit -c=IC210 -p=lab06 p1.cpp p2.cpp p3.cpp