Commit 85d5921
Changed files (2)
src
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
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: