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/Q4/README.md | |
| parent | 52a4b2209a2a83323091214360258ed224db72ed (diff) | |
add description of the code
Diffstat (limited to 'src/Q4/README.md')
| -rw-r--r-- | src/Q4/README.md | 38 |
1 files changed, 38 insertions, 0 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: |
