## CS107 - Lab 8

Due Tuesday 11/16 at the beginning of class.

Overview: In this lab you will continue working with C++ functions to modularize your code. As you write your program please remember to pay attention to issues of style, including:

• descriptive naming of variables
• indentation
• separating logical blocks of code with with space

### Problems

Playing Craps

Write a program that allows the user to play as many games of Craps as they want and reports the total number of games they played and how many times they won. The uer should be able to say 'no', they don't want to play, right away and the program should terminate without playing a single game and print out the appropriate statistics.

A game of Craps is played as follows. The player rolls the dice once.

• If this first roll is a 7 or 11, the player wins immediately.
• If this first roll is a 2,3 or 12, the player loses immediately.
• Otherwise (first roll of 4,5,6,8,9 0r 10), the player keeps rolling until either:
• they win by repeating the first number rolled ("make their point"), or
• they lose by rolling a 7 (they "crap out").

So, in order to "play" a game of Craps you need to generate dice rolls repeatedly until the player wins or loses according to the above rules. Note that a dice rolls should be simulated by generating two random integers from 1 to 6 with equal probability and adding those two numbers. Generating a single random integer from 1 ro 12 with equal probabiliy does not give you the same probabilities as a real dice roll. To see this, consider there is only one way to roll a total of 2 with real dice, whereas there are multiple different ways of rolling a 7, so the probabilities of these two rolls are not equal.

You should design your program to look as below.

```

return-type playSingleCrapsGame(parameter-list) {

//call a function generateRoll that generates and returns the total value
//of a random dice roll

//call a function printRoll that prints out this initial value in an
//appropriate way  (see sample output)

loop (as long as the game has not been won or lost) {
//call generateRoll function to get another dice roll

//send this value to printRoll function to print it

//send the necessary values to a function checkGameStatus that
//checks to see if the player has won or lost on the current roll
//and returns a value indicating teh current game status

}//loop

//return appropriate value indicating whether the player won or lost
}

int main() {

//variable declaration

loop (as long as the user want to play) {

//call a function playSingleCrapsGame that plays a single game of craps
//and returns a value indicating the outcome (win/lose)

//use this value to update a counter variable (or variables)  so that
//you can print out the gane statistics when the user is done playing

//ask if the user wants to play again and call a function that validates
//and returns a legal response getUserResponse()

}//loop

}
```
Your output should look something like the following:
```
Game 1: You got 11 and won!

Do you want to play another game? (y/n) y

Game 2: You got 8, 11, 12, 4, 8 and made your point.

Do you want to play another game? (y/n) y

Game 3: You got 3 and lost!

Do you want to play another game? (y/n) y

Game 4: You got  5, 9, 8, 7 and crapped out.

Do you want to play another game? (y/n) n

You played 4 games and won 2 of them.

```

### What to turn in:

• The program (the .cpp file).

To submit the program first rename (Xcode->File->Rename) the file main.cpp, then drag it to the Desktop, then drag it into the csci107bf04->drop-box. The names that you choose should be your loginname followed by problem number. For instance if I were to submit the file for the first problem i would rename it as ltoma-lab8-1.cpp.

The drop-box has been set up such that you don't have read access (or you could copy the solutions of your colleagues), only write access. Therefore you will not be able to see whether your file has been submitted or not. Check with me in class to make sure your files got submitted.

If you work in a team submit only one file per team.

Make sure you include your name on the top line of all your .cpp programs!!