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 a variable defined in
cstdlib.
The prototype for rand is:
int rand(); // needs #include <cstdlib>
So, if you need to print out 10 random
numbers in the range [0,1], you could do the following:
for(int i = 0; i < 10; i++)
cout << double(rand())/RAND_MAX << " ";
cout << endl;
If I run this, I get:
0.513871 0.175726 0.308634 0.534532 0.94763 0.171728 0.702231 0.226417 0.494766 0.124699
srand(), which seeds the
random number generator. Essentially, when you give srand a seed,
it uses the seed to pick a starting point in this sequence of "random" numbers.
With different starting points each time you run the program, you appear to be
getting different sequences of random numbers.
time() function from ctime to
seed your random number generator. Since the time changes each time you run
the program, you should get different sequences of "random" numbers. The
following program shows how this all works:
#include <cstdlib> //include cstdlib
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
// Seed the random number generator based on the time
srand(time(0));
// Print out 10 random numbers in the range [0,1]
for(int i = 0; i < 10; i++)
cout << double(rand())/RAND_MAX << " ";
cout << 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()%6 and adding 1.
Return that number
Player rolled 2 + 5 = 7 Player rolled 4 + 2 = 6 Player rolled 6 + 2 = 8 Player rolled 5 + 1 = 6 Player rolled 4 + 2 = 6
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. This code only
needs to execute once, and it should be 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 4 + 4 = 8 Player rolled 6 + 2 = 8 Player rolled 6 + 4 = 10 Player rolled 3 + 2 = 5 Player rolled 1 + 2 = 3 | ~/$ ./p1 Enter seed value: 20 Player rolled 2 + 1 = 3 Player rolled 6 + 4 = 10 Player rolled 3 + 2 = 5 Player rolled 5 + 6 = 11 Player rolled 5 + 2 = 7 |
~/bin/submit -c=IC210 -p=lab06 p1.cpp
int throwdice();
This function simulates a single throw of two dice and the
processing of that roll within the game.
Result of throwdice(): The function returns an integer according to the following rules:
| situation | return value |
| if the house wins (see above) | -1 |
| if the player wins (see above) | 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 2 + 5 = 7 Player wins! ~/$ ./p2 Enter seed value: 12 Player rolled 3 + 3 = 6 roll again Player rolled 3 + 5 = 8 roll again Player rolled 2 + 4 = 6 roll again Player rolled 6 + 4 = 10 roll again Player rolled 2 + 1 = 3 House wins! |
~/$ ./p2 Enter seed value: 10 Player rolled 2 + 5 = 7 Player wins! ~/$ ./p2 Enter seed value: 2018 Player rolled 2 + 6 = 8 roll again Player rolled 6 + 3 = 9 roll again Player rolled 6 + 1 = 7 Player wins! |
~/bin/submit -c=IC210 -p=lab06 p1.cpp p2.cpp
Below are some sample outputs.
~/$ ./p3 Enter seed value: 107 Player rolled 6 + 6 = 12 House wins! Play again? y Player rolled 3 + 6 = 9 setpoint is 9! Player rolled 3 + 2 = 5 roll again Player rolled 6 + 1 = 7 House wins! Play again? y Player rolled 1 + 1 = 2 House wins! Play again? y Player rolled 5 + 5 = 10 setpoint is 10! Player rolled 6 + 4 = 10 Player wins! Play again? y Player rolled 6 + 6 = 12 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