diff options
| author | mo <mo.khan@gmail.com> | 2019-06-18 17:56:27 -0600 |
|---|---|---|
| committer | mo <mo.khan@gmail.com> | 2019-06-18 17:56:27 -0600 |
| commit | 85d59210513ae96412e06e0b56842b7e19232927 (patch) | |
| tree | 2c1acc9ba43ec1f6024679dba6e5d18fe66542dd /src | |
| parent | 52a4b2209a2a83323091214360258ed224db72ed (diff) | |
add description of the code
Diffstat (limited to 'src')
| -rw-r--r-- | src/Q4/README.md | 38 | ||||
| -rw-r--r-- | src/Q4/RandomSumGame.java | 7 |
2 files changed, 42 insertions, 3 deletions
diff --git a/src/Q4/README.md b/src/Q4/README.md index f2a0455..468f654 100644 --- a/src/Q4/README.md +++ b/src/Q4/README.md @@ -37,6 +37,44 @@ At the end, the program prints the number of times the player won and the number ``` 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: diff --git a/src/Q4/RandomSumGame.java b/src/Q4/RandomSumGame.java index 06204ac..183498b 100644 --- a/src/Q4/RandomSumGame.java +++ b/src/Q4/RandomSumGame.java @@ -12,14 +12,14 @@ import java.io.*; import java.util.*; public class RandomSumGame { - private boolean start; + private boolean start = true; private int d1 = 0; private int d2 = 0; private int sum = 0; private int valuePoint = 0; private int wins = 0; private int losses = 0; - private String status; + private String status = ""; private PrintStream out; /** @@ -56,7 +56,8 @@ public class RandomSumGame { } /** - * Plays a roll of the given dice. + * Plays a roll of the given dice. This is a method overload of the + * previous play method. * * @param d1 the value of the roll for the first dice * @param d2 the value of the roll for the second dice |
