Skip to content

roulette recitation modifications (bew21 and cdg26) #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
# example_roulette
Program to refactor that plays a game of roulette

Cameron Givler
Bailey Wall

##Questions

###1. In what ways is the refactored code simpler?
The code is simpler because we now have eliminated many if statements that switched
between different types of bets. Instead of if statements, we subclassed `Bet` with
the three different types (plus the ones we added). We then implemented abstract
methods inside `Bet`, `getPrompt()` and `wonBet()`. These methods are no longer implemented
in game, and simply have functionality defined in their respective subclass. We
also broke up `Play()` into multiple helper methods to improve readability.

###2. In what ways is the refactored code more complex?
There are now more classes that you have to look through to figure out the functionality
of each type of bet.

###3. What trade-offs did you make when refactoring your old code?
Our implementation requires instantiating a new class for each `Bet` subclass, even if that
specific instance isn't currently being used. We had to make this trade-off because we have to
be able to access myDescription from within each class.


###4. Which code do you prefer and why?
I prefer the new code because I think it is easier to read. It is also more easily
extended. The only modifications that need to be made to extend the number of
possible bets is to create a new subclass of bet and add it to the array of possible bets.
6 changes: 5 additions & 1 deletion src/roulette/Bet.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* @author Robert C. Duvall
*/
public class Bet {
public abstract class Bet {
private String myDescription;
private int myOdds;

Expand Down Expand Up @@ -34,4 +34,8 @@ public int getOdds () {
public String getDescription () {
return myDescription;
}

public abstract String getPrompt();

public abstract boolean wonBet(Wheel wheel, String betChoice);
}
22 changes: 22 additions & 0 deletions src/roulette/ColorBet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package roulette;

import util.ConsoleReader;

public class ColorBet extends Bet {

private static final String DESCRIPTION = "Red or Black";

public ColorBet(int odds) {
super(DESCRIPTION, odds);
}

public String getPrompt(){
return ConsoleReader.promptOneOf("Please bet", Wheel.BLACK, Wheel.RED);
}

@Override
public boolean wonBet(Wheel wheel, String betChoice) {
return wheel.getColor().equals(betChoice);
}

}
81 changes: 33 additions & 48 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
* @author Robert C. Duvall
*/
public class Game {

// name of the game
private static final String DEFAULT_NAME = "Roulette";

// bets player can make
private Bet[] myPossibleBets = {
new Bet("Red or Black", 1),
new Bet("Odd or Even", 1),
new Bet("Three in a Row", 11)
new ColorBet(1),
new OddEvenBet(1),
new HighLowBet(1),
new SingleNumberBet(35),
new TwoInARowBet(17),
new ThreeInRowBet(11)
};
private Wheel myWheel;

Expand Down Expand Up @@ -45,19 +50,11 @@ public void play (Gambler player) {
int amount = ConsoleReader.promptRange("How much do you want to bet",
0, player.getBankroll());
int whichBet = promptForBet();
String betChoice = placeBet(whichBet);

System.out.print("Spinning ...");
myWheel.spin();
System.out.println(String.format("Dropped into %s %d", myWheel.getColor(), myWheel.getNumber()));
if (betIsMade(whichBet, betChoice)) {
System.out.println("*** Congratulations :) You win ***");
amount *= myPossibleBets[whichBet].getOdds();
}
else {
System.out.println("*** Sorry :( You lose ***");
amount *= -1;
}
String betChoice = myPossibleBets[whichBet].getPrompt();

spinWheel();
amount = calculateResult(amount, whichBet, betChoice);

player.updateBankroll(amount);
}

Expand All @@ -71,26 +68,26 @@ private int promptForBet () {
}
return ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length) - 1;
}

/**
* Place the given bet by prompting user for specific information need to complete that bet.
*
* @param whichBet specific bet chosen by the user
*/
private String placeBet (int whichBet) {
String result = "";
if (whichBet == 0) {
result = ConsoleReader.promptOneOf("Please bet", Wheel.BLACK, Wheel.RED);
}
else if (whichBet == 1) {
result = ConsoleReader.promptOneOf("Please bet", "even", "odd");
private void spinWheel(){
System.out.println();
System.out.print("Spinning ...");
myWheel.spin();
System.out.println(String.format("Dropped into %s %d", myWheel.getColor(), myWheel.getNumber()));
}

private int calculateResult(int amount, int whichBet, String betChoice){
if (betIsMade(whichBet, betChoice)) {
System.out.println("*** Congratulations :) You win ***");
amount *= myPossibleBets[whichBet].getOdds();
}
else if (whichBet == 2) {
result = "" + ConsoleReader.promptRange("Enter first of three consecutive numbers",
1, Wheel.NUM_SPOTS - 3);

else {
System.out.println("*** Sorry :( You lose ***");
amount *= -1;
}
System.out.println();
return result;
return amount;
}

/**
Expand All @@ -100,19 +97,7 @@ else if (whichBet == 2) {
* @param betChoice specific value user chose to try to win the bet
*/
private boolean betIsMade (int whichBet, String betChoice) {
if (whichBet == 0) {
return myWheel.getColor().equals(betChoice);
}
else if (whichBet == 1) {
return (myWheel.getNumber() % 2 == 0 && betChoice.equals("even")) ||
(myWheel.getNumber() % 2 == 1 && betChoice.equals("odd"));
}
else if (whichBet == 2) {
int start = Integer.parseInt(betChoice);
return (start <= myWheel.getNumber() && myWheel.getNumber() < start + 3);
}
else {
return false;
}
Bet selectedBet = myPossibleBets[whichBet];
return selectedBet.wonBet(myWheel, betChoice);
}
}
28 changes: 28 additions & 0 deletions src/roulette/HighLowBet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package roulette;

import util.ConsoleReader;

public class HighLowBet extends Bet {

private static final String DESCRIPTION = "High or Low";

public HighLowBet(int odds) {
super(DESCRIPTION, odds);
}

@Override
public String getPrompt() {
return ConsoleReader.promptOneOf("Please bet", "low", "high");
}

@Override
public boolean wonBet(Wheel wheel, String betChoice) {
if(wheel.getNumber() <= 19){
return (betChoice.equals("low"));
}
else{
return (betChoice.equals("high"));
}
}

}
23 changes: 23 additions & 0 deletions src/roulette/OddEvenBet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package roulette;

import util.ConsoleReader;

public class OddEvenBet extends Bet {

private static final String DESCRIPTION = "Odd or Even";

public OddEvenBet(int odds) {
super(DESCRIPTION, odds);
}

public String getPrompt(){
return ConsoleReader.promptOneOf("Please bet", "even", "odd");
}

@Override
public boolean wonBet(Wheel wheel, String betChoice) {
return (wheel.getNumber() % 2 == 0 && betChoice.equals("even")) ||
(wheel.getNumber() % 2 == 1 && betChoice.equals("odd"));
}

}
24 changes: 24 additions & 0 deletions src/roulette/SingleNumberBet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package roulette;

import util.ConsoleReader;

public class SingleNumberBet extends Bet {

private static final String DESCRIPTION = "Single Number";

public SingleNumberBet(int odds) {
super(DESCRIPTION, odds);
}

@Override
public String getPrompt() {
return "" + ConsoleReader.promptRange("Enter a number", 1, Wheel.NUM_SPOTS);
}

@Override
public boolean wonBet(Wheel wheel, String betChoice) {
int choice = Integer.parseInt(betChoice);
return (wheel.getNumber() == choice);
}

}
24 changes: 24 additions & 0 deletions src/roulette/ThreeInRowBet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package roulette;

import util.ConsoleReader;

public class ThreeInRowBet extends Bet {

private static final String DESCRIPTION = "Three in a Row";

public ThreeInRowBet(int odds) {
super(DESCRIPTION, odds);
}

public String getPrompt(){
return "" + ConsoleReader.promptRange("Enter first of three consecutive numbers",
1, Wheel.NUM_SPOTS - 3);
}

@Override
public boolean wonBet(Wheel wheel, String betChoice) {
int start = Integer.parseInt(betChoice);
return (start <= wheel.getNumber() && wheel.getNumber() < start + 3);
}

}
24 changes: 24 additions & 0 deletions src/roulette/TwoInARowBet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package roulette;

import util.ConsoleReader;

public class TwoInARowBet extends Bet {

private static final String DESCRIPTION = "Two in a Row";

public TwoInARowBet(int odds) {
super(DESCRIPTION, odds);
}

@Override
public String getPrompt() {
return "" + ConsoleReader.promptRange("Enter first of two consecutive numbers", 1, Wheel.NUM_SPOTS - 2);
}

@Override
public boolean wonBet(Wheel wheel, String betChoice) {
int start = Integer.parseInt(betChoice);
return (start <= wheel.getNumber() && wheel.getNumber() < start + 2);
}

}