master
  1/**
  2 * Assignment 2, COMP268 Class: RandomSumGame.java
  3 *
  4 * @description Provides an implementation of a light weight version of the dice game, Craps.
  5 * @author: mo khan Student ID: 3431709
  6 * @date Jun 18, 2019
  7 * @version 1.0
  8 */
  9package Q4;
 10
 11import java.io.*;
 12import java.util.*;
 13
 14public class RandomSumGame {
 15  private boolean start = true;
 16  private int d1 = 0;
 17  private int d2 = 0;
 18  private int sum = 0;
 19  private int valuePoint = 0;
 20  private int wins = 0;
 21  private int losses = 0;
 22  private String status = "";
 23  private PrintStream out;
 24
 25  /**
 26   * Creates an instance of the craps game.
 27   *
 28   * @param out the output stream to write messages to.
 29   */
 30  public RandomSumGame(PrintStream out) {
 31    this.out = out;
 32  }
 33
 34  /**
 35   * Returns the total # of wins.
 36   *
 37   * @return the total # of wins
 38   */
 39  public int getWins() {
 40    return this.wins;
 41  }
 42
 43  /**
 44   * Returns the total # of losses
 45   *
 46   * @return the total # of losses
 47   */
 48  public int getLosses() {
 49    return this.losses;
 50  }
 51
 52  /** Plays a roll of the dice. This method will also roll the dice. */
 53  public void play() {
 54    this.rollDice();
 55    this.play(this.d1, this.d2);
 56  }
 57
 58  /**
 59   * Plays a roll of the given dice. This is a method overload of the previous play method.
 60   *
 61   * @param d1 the value of the roll for the first dice
 62   * @param d2 the value of the roll for the second dice
 63   */
 64  public void play(int d1, int d2) {
 65    int total = d1 + d2;
 66    this.puts("You rolled: %d", total);
 67    if (hasValuePoint()) this.subsequentPlay(total);
 68    else this.firstPlay(total);
 69  }
 70
 71  /** This method rolls each of the dice. */
 72  public void rollDice() {
 73    this.d1 = this.roll();
 74    this.d2 = this.roll();
 75    this.sum = this.d1 + this.d2;
 76  }
 77
 78  private int roll() {
 79    return new Random().nextInt(5) + 1;
 80  }
 81
 82  private void firstPlay(int total) {
 83    switch (total) {
 84      case 2:
 85      case 3:
 86      case 12:
 87        this.lose("Craps! You lose.");
 88        break;
 89      case 7:
 90      case 11:
 91        this.win("Natural! You win!");
 92        break;
 93      default:
 94        this.puts("Value point established: %d", total);
 95        this.valuePoint = total;
 96        play();
 97        break;
 98    }
 99  }
100
101  private void subsequentPlay(int total) {
102    if (total == this.valuePoint) this.win("You win!");
103    else if (total == 7) this.lose("You lose.");
104    else play();
105  }
106
107  private void win(String message) {
108    this.wins += 1;
109    this.puts(message);
110    this.reset();
111  }
112
113  private void lose(String message) {
114    this.losses += 1;
115    this.puts(message);
116    this.reset();
117  }
118
119  private void puts(String format, Object... args) {
120    this.out.println(String.format(format, args));
121  }
122
123  private boolean hasValuePoint() {
124    return this.valuePoint > 0;
125  }
126
127  private void reset() {
128    this.valuePoint = this.d1 = this.d2 = this.sum = 0;
129  }
130
131  /**
132   * The entry point to the console application.
133   *
134   * @param args the command line arguments passed to the program
135   */
136  public static void main(String[] args) {
137    Scanner in = new Scanner(System.in);
138    System.out.println("Welcome to Craps");
139    System.out.println("================");
140    RandomSumGame game = new RandomSumGame(System.out);
141
142    for (int i = 0; i < 3; i++) {
143      System.out.println();
144      System.out.println(String.format("Game %d", i + 1));
145      game.play();
146    }
147
148    System.out.println();
149    System.out.println("================");
150    System.out.println(String.format("Wins: %d", game.getWins()));
151    System.out.println(String.format("Losses: %d", game.getLosses()));
152    System.out.println();
153  }
154}