summaryrefslogtreecommitdiff
path: root/src/Q4/README.md
blob: bacf6836cfa8939e072c5a9681321e6cd1ce7e38 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
Learning Profile for Assignment #2, And Question #4

Name: Mo Khan
Student ID: 3431709

1. Problem Statement:

  Craps is a dice game where two dice are rolled.
  Each die has six faces representing values:

  1, 2, 3, 4, 5, or 6.

    1. If the sum is 2, 3, or 12 (called craps), you lose;
    1. If the sum is 7 or 11 (called natural), you win;
    1. If the sum is any other value (4, 5, 6, 8, 9, or 10), a value point is established, and you continue to roll until you either roll a sum of the value point or a 7.
      If the sum of the new roll is equal to the value point, then you win; if the sum of the new roll is equal to 7, then you lose.
      Remember, in option (III), you continue to roll until you get a 7 or the value point.

  Sample runs:

  * You rolled 5 + 6 = 11; you win
  * You rolled 1 + 2 = 3; you lose
  * You rolled 2 + 2 = 4; you establish the value point 4;
    – Roll again 2 + 3 = 5; roll
    – Roll again 2 + 1 = 3; roll
    – Roll again 2 + 2 = 4; you win
  * You rolled 2 + 6 = 8; you establish the value point 8;
    – Roll again 4 + 4 = 8; you win
  * You rolled 3 + 2 = 5; you establish the value point 5;
    – Roll again 1 + 1 = 2; roll
    – Roll again 2 + 2 = 4; roll
  * Roll again 1 + 1 = 2; roll
  * Roll again 3 + 4 = 7; you lose

  Develop a program that plays craps with a player three times.
  At the end, the program prints the number of times the player won and the number of times the player lost.

1. 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.

1. Errors and Warnings:
1. Sample Input and Output:

  1. 3 losses

    ```bash
    Welcome to Craps
    ================

    Game 1
    You rolled: 5
    Value point established: 5
    You rolled: 7
    You lose.

    Game 2
    You rolled: 6
    Value point established: 6
    You rolled: 5
    You rolled: 9
    You rolled: 7
    You lose.

    Game 3
    You rolled: 3
    Craps! You lose.

    ================
    Wins: 0
    Losses: 3
    ```


  1. 3 wins

    ```bash
    java -cp target/assignment2*.jar ca.mokhan.comp268.App 4
    Welcome to Craps
    ================

    Game 1
    You rolled: 7
    Natural! You win!

    Game 2
    You rolled: 7
    Natural! You win!

    Game 3
    You rolled: 5
    Value point established: 5
    You rolled: 6
    You rolled: 4
    You rolled: 8
    You rolled: 5
    You win!

    ================
    Wins: 3
    Losses: 0
    ```

  1. 2 wins, 1 loss

    ```bash
    Welcome to Craps
    ================

    Game 1
    You rolled: 4
    Value point established: 4
    You rolled: 3
    You rolled: 9
    You rolled: 7
    You lose.

    Game 2
    You rolled: 8
    Value point established: 8
    You rolled: 8
    You win!

    Game 3
    You rolled: 8
    Value point established: 8
    You rolled: 2
    You rolled: 8
    You win!

    ================
    Wins: 2
    Losses: 1
    ```

1. Discussion: