Commit 52a4b22
Changed files (1)
src
src/Q4/RandomSumGame.java
@@ -1,3 +1,11 @@
+/**
+ * Assignment 2, COMP268 Class: RandomSumGame.java
+ *
+ * @description Provides an implementation of a light weight version of the dice game, Craps.
+ * @author: mo khan Student ID: 3431709
+ * @date Jun 18, 2019
+ * @version 1.0
+ */
package Q4;
import java.io.*;
@@ -10,18 +18,49 @@ public class RandomSumGame {
private int sum = 0;
private int valuePoint = 0;
private int wins = 0;
+ private int losses = 0;
private String status;
private PrintStream out;
+ /**
+ * Creates an instance of the craps game.
+ *
+ * @param out the output stream to write messages to.
+ */
public RandomSumGame(PrintStream out) {
this.out = out;
}
+ /**
+ * Returns the total # of wins.
+ *
+ * @return the total # of wins
+ */
+ public int getWins() {
+ return this.wins;
+ }
+
+ /**
+ * Returns the total # of losses
+ *
+ * @return the total # of losses
+ */
+ public int getLosses() {
+ return this.losses;
+ }
+
+ /** Plays a roll of the dice. This method will also roll the dice. */
public void play() {
this.rollDice();
this.play(this.d1, this.d2);
}
+ /**
+ * Plays a roll of the given dice.
+ *
+ * @param d1 the value of the roll for the first dice
+ * @param d2 the value of the roll for the second dice
+ */
public void play(int d1, int d2) {
int total = d1 + d2;
this.puts("You rolled: %d", total);
@@ -29,6 +68,7 @@ public class RandomSumGame {
else this.firstPlay(total);
}
+ /** This method rolls each of the dice. */
public void rollDice() {
this.d1 = this.roll();
this.d2 = this.roll();
@@ -71,6 +111,7 @@ public class RandomSumGame {
}
private void lose(String message) {
+ this.losses += 1;
this.puts(message);
this.reset();
}
@@ -87,10 +128,27 @@ public class RandomSumGame {
this.valuePoint = this.d1 = this.d2 = this.sum = 0;
}
+ /**
+ * The entry point to the console application.
+ *
+ * @params args the command line arguments passed to the program
+ */
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Welcome to Craps");
+ System.out.println("================");
RandomSumGame game = new RandomSumGame(System.out);
- game.play();
+
+ for (int i = 0; i < 3; i++) {
+ System.out.println();
+ System.out.println(String.format("Game %d", i + 1));
+ game.play();
+ }
+
+ System.out.println();
+ System.out.println("================");
+ System.out.println(String.format("Wins: %d", game.getWins()));
+ System.out.println(String.format("Losses: %d", game.getLosses()));
+ System.out.println();
}
}