patternjavaMinor
Playing “craps” for the win v0.3
Viewed 0 times
playingwincrapsthefor
Problem
Continud from Playing “craps” for the win v0.2
You roll two dice. Each die has six faces, which contain one, two,
three, four, five and six spots, respectively. After the dice have
come to rest, the sum of the spots on the two upward faces is
calculated. If the sum is 7 or 11 on the first throw, you win. If the
sum is 2, 3 or 12 on the first throw (called “craps”), you lose (i.e.,
the “house” wins). If the sum is 4, 5, 6, 8, 9 or 10 on the first
throw, that sum becomes your “point.” To win, you must continue
rolling the dice until you “make your point” (i.e., roll that same
point value). You lose by rolling a 7 before making your point.
```
// Craps.java
package co.vu.xxx.craps;
import java.security.SecureRandom;
public class Craps {
public static enum Outcome {UNDEFINED, WIN_FIRST, WIN_POINT, LOSE_FIRST, LOSE_POINT};
private int currentNumber;
private int point;
private Outcome gameResult = Outcome.UNDEFINED;
private static SecureRandom randomNumbers = new SecureRandom();
public static int rollDice() {
int randN = 2 + randomNumbers.nextInt(6) + randomNumbers.nextInt(6);
return randN;
}
public void firstRoll() {
currentNumber = rollDice();
switch (currentNumber) {
case 7:case 11:
gameResult = Outcome.WIN_FIRST;
break;
case 2:case 3:case 12:
gameResult = Outcome.LOSE_FIRST;
break;
default:
point = currentNumber;
}
}
public void moreRolls() {
currentNumber = rollDice();
if(currentNumber == point) {
gameResult = Outcome.WIN_POINT;
} else if(currentNumber == 7) {
gameResult = Outcome.LOSE_POINT;
}
}
public Outcome getGameResult() {
return gameResult;
}
public int getPoint() {
return point;
}
public int getCurrentNumber() {
return currentNumber;
You roll two dice. Each die has six faces, which contain one, two,
three, four, five and six spots, respectively. After the dice have
come to rest, the sum of the spots on the two upward faces is
calculated. If the sum is 7 or 11 on the first throw, you win. If the
sum is 2, 3 or 12 on the first throw (called “craps”), you lose (i.e.,
the “house” wins). If the sum is 4, 5, 6, 8, 9 or 10 on the first
throw, that sum becomes your “point.” To win, you must continue
rolling the dice until you “make your point” (i.e., roll that same
point value). You lose by rolling a 7 before making your point.
```
// Craps.java
package co.vu.xxx.craps;
import java.security.SecureRandom;
public class Craps {
public static enum Outcome {UNDEFINED, WIN_FIRST, WIN_POINT, LOSE_FIRST, LOSE_POINT};
private int currentNumber;
private int point;
private Outcome gameResult = Outcome.UNDEFINED;
private static SecureRandom randomNumbers = new SecureRandom();
public static int rollDice() {
int randN = 2 + randomNumbers.nextInt(6) + randomNumbers.nextInt(6);
return randN;
}
public void firstRoll() {
currentNumber = rollDice();
switch (currentNumber) {
case 7:case 11:
gameResult = Outcome.WIN_FIRST;
break;
case 2:case 3:case 12:
gameResult = Outcome.LOSE_FIRST;
break;
default:
point = currentNumber;
}
}
public void moreRolls() {
currentNumber = rollDice();
if(currentNumber == point) {
gameResult = Outcome.WIN_POINT;
} else if(currentNumber == 7) {
gameResult = Outcome.LOSE_POINT;
}
}
public Outcome getGameResult() {
return gameResult;
}
public int getPoint() {
return point;
}
public int getCurrentNumber() {
return currentNumber;
Solution
Much of this is nitpicking, but since you keep posting revisions, you seem to want a really thorough review.
- The parameter name
pCraptoUseCraps.declareResult()isn't explanatory. Try renaming it to something likegameinstead.
- I suggest doing the same for
crapObjinmain().
getGameResult()should probably be namedgetState()or something similar, since result implies that it has finished, and there is anUNDEFINEDstate.
- Speaking of which, the state isn't really undefined, it's just unfinished. It should be renamed to something that doesn't imply an error condition.
- To be super finicky, you could be declaring your local variables
randN,currentNumber,crapObj,point, andinputScanasfinal.
- In
Craps.rollDice()you could justreturnthe value instead of storing it in a variable and then immediately returning. The method should also beprivate.
UseCraps.declareResult()should be private.
- In
UseCraps.waitUser()you don't need to store the line ininputsince you don't use it.
- Your
switchinUseCraps.declareResult()doesn't have adefault.
- The
Crapsclass should only expose onerollmethod. It may make sense to keep the two as separateprivateones, but you should simplify what your callers need to do to use your class. Consider what happens if someone callsfirstRolland never callsmoreRolls, or if they callmoreRollsfirst, or if they go back and forth.
- Consider adding a
resetmethod toCrapsso you can start a new game without having to create a new instance.
- You should check if you are already in a win state when someone calls
roll.
Context
StackExchange Code Review Q#68921, answer score: 7
Revisions (0)
No revisions yet.