summaryrefslogtreecommitdiff
path: root/src/Q4/RandomSumGame.java
blob: 183498be2be5ac463658b77371dac825778633cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
 * 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.*;
import java.util.*;

public class RandomSumGame {
  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 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. 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
   */
  public void play(int d1, int d2) {
    int total = d1 + d2;
    this.puts("You rolled: %d", total);
    if (hasValuePoint()) this.subsequentPlay(total);
    else this.firstPlay(total);
  }

  /** This method rolls each of the dice. */
  public void rollDice() {
    this.d1 = this.roll();
    this.d2 = this.roll();
    this.sum = this.d1 + this.d2;
  }

  private int roll() {
    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.losses += 1;
    this.puts(message);
    this.reset();
  }

  private void puts(String format, Object... args) {
    this.out.println(String.format(format, args));
  }

  private boolean hasValuePoint() {
    return this.valuePoint > 0;
  }

  private void reset() {
    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);

    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();
  }
}