summaryrefslogtreecommitdiff
path: root/src/Q4
diff options
context:
space:
mode:
authormo <mo.khan@gmail.com>2019-06-18 17:30:10 -0600
committermo <mo.khan@gmail.com>2019-06-18 17:30:10 -0600
commitbf82cc693a1a6cdfabf49e4883fb7c90c6fcf266 (patch)
treee7ca53dc089b7d8d90e632fdcc57b61d692e3000 /src/Q4
parentae4c0a788d276d4793ab4f91276bdb7793ae8f95 (diff)
extract methods to make it easier to read
Diffstat (limited to 'src/Q4')
-rw-r--r--src/Q4/RandomSumGame.java66
1 files changed, 39 insertions, 27 deletions
diff --git a/src/Q4/RandomSumGame.java b/src/Q4/RandomSumGame.java
index aa0aa00..30080c2 100644
--- a/src/Q4/RandomSumGame.java
+++ b/src/Q4/RandomSumGame.java
@@ -9,6 +9,7 @@ public class RandomSumGame {
private int d2 = 0;
private int sum = 0;
private int valuePoint = 0;
+ private int wins = 0;
private String status;
private PrintStream out;
@@ -25,34 +26,9 @@ public class RandomSumGame {
int total = d1 + d2;
this.puts("You rolled: %d", total);
if (!hasValuePoint()) {
- switch (total) {
- case 2:
- case 3:
- case 12:
- this.puts("Craps! You lose.");
- break;
- case 7:
- case 11:
- this.puts("Natural! You win!");
- break;
- default:
- this.puts("Value point established: %d", total);
- this.valuePoint = total;
- play();
- break;
- }
+ this.firstPlay(total);
} else {
- if (total == this.valuePoint) {
- this.puts("You win!");
- this.reset();
- return;
- } else if (total == 7) {
- this.puts("You lose.");
- this.reset();
- return;
- } else {
- play();
- }
+ this.subsequentPlay(total);
}
}
@@ -66,6 +42,42 @@ public class RandomSumGame {
return new Random().nextInt(5) + 1;
}
+ private void firstPlay(int total) {
+ switch (total) {
+ case 2:
+ case 3:
+ case 12:
+ this.lose("Craps! You lose.");
+ break;
+ case 7:
+ case 11:
+ this.win("Natural! You win!");
+ break;
+ default:
+ this.puts("Value point established: %d", total);
+ this.valuePoint = total;
+ play();
+ break;
+ }
+ }
+
+ private void subsequentPlay(int total) {
+ if (total == this.valuePoint) this.win("You win!");
+ else if (total == 7) this.lose("You lose.");
+ else play();
+ }
+
+ private void win(String message) {
+ this.wins += 1;
+ this.puts(message);
+ this.reset();
+ }
+
+ private void lose(String message) {
+ this.puts(message);
+ this.reset();
+ }
+
private void puts(String format, Object... args) {
this.out.println(String.format(format, args));
}