diff options
| author | mo khan <mo@mokhan.ca> | 2019-08-11 16:33:12 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2019-08-11 16:33:12 -0600 |
| commit | 39ae7885659d6d1c6397a900eb46898e9443b459 (patch) | |
| tree | 274f6602c7c61ccd5885f8c9a0d760804171e276 /src/Q4/README.md | |
| parent | c4eebacd3f18230c40d1c61b6570ef9fb4b646d6 (diff) | |
complete program profiles
Diffstat (limited to 'src/Q4/README.md')
| -rw-r--r-- | src/Q4/README.md | 237 |
1 files changed, 164 insertions, 73 deletions
diff --git a/src/Q4/README.md b/src/Q4/README.md index 468f654..bacf683 100644 --- a/src/Q4/README.md +++ b/src/Q4/README.md @@ -5,76 +5,167 @@ Student ID: 3431709 1. Problem Statement: -```text -Craps is a dice game where two dice are rolled. -Each die has six faces representing values: - -1, 2, 3, 4, 5, or 6. - -1. If the sum is 2, 3, or 12 (called craps), you lose; -2. If the sum is 7 or 11 (called natural), you win; -3. If the sum is any other value (4, 5, 6, 8, 9, or 10), a value point is established, and you continue to roll until you either roll a sum of the value point or a 7. -If the sum of the new roll is equal to the value point, then you win; if the sum of the new roll is equal to 7, then you lose. -Remember, in option (III), you continue to roll until you get a 7 or the value point. - -Sample runs: -* You rolled 5 + 6 = 11; you win -* You rolled 1 + 2 = 3; you lose -* You rolled 2 + 2 = 4; you establish the value point 4; - – Roll again 2 + 3 = 5; roll - – Roll again 2 + 1 = 3; roll - – Roll again 2 + 2 = 4; you win -* You rolled 2 + 6 = 8; you establish the value point 8; - – Roll again 4 + 4 = 8; you win -* You rolled 3 + 2 = 5; you establish the value point 5; - – Roll again 1 + 1 = 2; roll - – Roll again 2 + 2 = 4; roll -* Roll again 1 + 1 = 2; roll -* Roll again 3 + 4 = 7; you lose - -Develop a program that plays craps with a player three times. -At the end, the program prints the number of times the player won and the number of times the player lost. -``` - -2. Description of the Code: - -I solved this problem by creating a class called `RandomSumGame` as per -the class diagram. I added two instance variables named `wins` and -`losses` to keep track of how many times the player won or lost. - -To try to make the API of this class more testable, I chose to pass the -`PrintStream` in as a parameter to the constructor. This example of -dependency injection, made it possible to write unit tests to ensure the -proper output is printed to the stream. - -To simplify the problem, I split it up into two types of game play. The -rules for the initial roll is slightly different for the rules for -subsequent rolls. I created two methods named `firstPlay` and `subsequentPlay`. -This made it easy to focus on the rules for the initial roll in the -`firstPlay` method and the rules for the subsequent roles in the -`subsequentPlay` method. - -To roll the dice, I extracted a method called `roll` that returns a -random number between 1 - 6. - -To keep track of the wins/losses, I delegating to the `win` or `lose` -methods to print a message to the screen and increment a win/loss -counter. - -In the `main` method, I added a header for the game, then created an -instace of the game, ran 3 rounds of the game and printed the final -results afterwards. - -There were some unneccessary instance variables, but I kept them to -ensure that I satisfy the desired API described in the class diagram. -In a few cases, local variables and recursion was more than enough. - -I used `recursion` to handle subsequent rolls when a value point is -established. With any recursion it's important to have a solid base -case. In this case the base case was either rolling a 7 or the value -point. These values are randomly generated, so it is possible to produce -an infinte loop due to randomness. - -3. Errors and Warnings: -4. Sample Input and Output: -5. Discussion: + Craps is a dice game where two dice are rolled. + Each die has six faces representing values: + + 1, 2, 3, 4, 5, or 6. + + 1. If the sum is 2, 3, or 12 (called craps), you lose; + 1. If the sum is 7 or 11 (called natural), you win; + 1. If the sum is any other value (4, 5, 6, 8, 9, or 10), a value point is established, and you continue to roll until you either roll a sum of the value point or a 7. + If the sum of the new roll is equal to the value point, then you win; if the sum of the new roll is equal to 7, then you lose. + Remember, in option (III), you continue to roll until you get a 7 or the value point. + + Sample runs: + + * You rolled 5 + 6 = 11; you win + * You rolled 1 + 2 = 3; you lose + * You rolled 2 + 2 = 4; you establish the value point 4; + – Roll again 2 + 3 = 5; roll + – Roll again 2 + 1 = 3; roll + – Roll again 2 + 2 = 4; you win + * You rolled 2 + 6 = 8; you establish the value point 8; + – Roll again 4 + 4 = 8; you win + * You rolled 3 + 2 = 5; you establish the value point 5; + – Roll again 1 + 1 = 2; roll + – Roll again 2 + 2 = 4; roll + * Roll again 1 + 1 = 2; roll + * Roll again 3 + 4 = 7; you lose + + Develop a program that plays craps with a player three times. + At the end, the program prints the number of times the player won and the number of times the player lost. + +1. Description of the Code: + + I solved this problem by creating a class called `RandomSumGame` as per + the class diagram. I added two instance variables named `wins` and + `losses` to keep track of how many times the player won or lost. + + To try to make the API of this class more testable, I chose to pass the + `PrintStream` in as a parameter to the constructor. This example of + dependency injection, made it possible to write unit tests to ensure the + proper output is printed to the stream. + + To simplify the problem, I split it up into two types of game play. The + rules for the initial roll is slightly different for the rules for + subsequent rolls. I created two methods named `firstPlay` and `subsequentPlay`. + This made it easy to focus on the rules for the initial roll in the + `firstPlay` method and the rules for the subsequent roles in the + `subsequentPlay` method. + + To roll the dice, I extracted a method called `roll` that returns a + random number between 1 - 6. + + To keep track of the wins/losses, I delegating to the `win` or `lose` + methods to print a message to the screen and increment a win/loss + counter. + + In the `main` method, I added a header for the game, then created an + instace of the game, ran 3 rounds of the game and printed the final + results afterwards. + + There were some unneccessary instance variables, but I kept them to + ensure that I satisfy the desired API described in the class diagram. + In a few cases, local variables and recursion was more than enough. + + I used `recursion` to handle subsequent rolls when a value point is + established. With any recursion it's important to have a solid base + case. In this case the base case was either rolling a 7 or the value + point. These values are randomly generated, so it is possible to produce + an infinte loop due to randomness. + +1. Errors and Warnings: +1. Sample Input and Output: + + 1. 3 losses + + ```bash + Welcome to Craps + ================ + + Game 1 + You rolled: 5 + Value point established: 5 + You rolled: 7 + You lose. + + Game 2 + You rolled: 6 + Value point established: 6 + You rolled: 5 + You rolled: 9 + You rolled: 7 + You lose. + + Game 3 + You rolled: 3 + Craps! You lose. + + ================ + Wins: 0 + Losses: 3 + ``` + + + 1. 3 wins + + ```bash + java -cp target/assignment2*.jar ca.mokhan.comp268.App 4 + Welcome to Craps + ================ + + Game 1 + You rolled: 7 + Natural! You win! + + Game 2 + You rolled: 7 + Natural! You win! + + Game 3 + You rolled: 5 + Value point established: 5 + You rolled: 6 + You rolled: 4 + You rolled: 8 + You rolled: 5 + You win! + + ================ + Wins: 3 + Losses: 0 + ``` + + 1. 2 wins, 1 loss + + ```bash + Welcome to Craps + ================ + + Game 1 + You rolled: 4 + Value point established: 4 + You rolled: 3 + You rolled: 9 + You rolled: 7 + You lose. + + Game 2 + You rolled: 8 + Value point established: 8 + You rolled: 8 + You win! + + Game 3 + You rolled: 8 + Value point established: 8 + You rolled: 2 + You rolled: 8 + You win! + + ================ + Wins: 2 + Losses: 1 + ``` + +1. Discussion: |
