Skip to content
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>
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
# example_roulette
Program to refactor that plays a game of roulette

Students: Joy Patel (jhp21) and Tina Liang (tl150)

1. The refactored code gets rid of the "if" statements. It also allows one to make new Bet games by extending the Bet abstract class.

2. We had to extend the Bet class to create subclasses which made the code more complex. We also had to think about abstract methods that all Bet type classes should have versus methods unique to the specific types of Bets.

3. We made more classes which required more coding than just the original "if" statements. However, we split the code into more classes all extending from a single abstract class which improved the readability of the code.

4. We would prefer the refactored code because it hides the implementation of each Bet game from the Game class into it's own Bet class. This also allows us to easily make new types of Bet games which have their own unique gameplay (e.g., probabilities of winning, description, etc.).

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

abstract public String placeBet ();
abstract public boolean betIsMade (String betChoice, Wheel myWheel);

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

import util.ConsoleReader;

public class BlackRedBet extends Bet {

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

public BlackRedBet(int odds) {
super(DESCRIPTION, odds);
// TODO Auto-generated constructor stub
}

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

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

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

import util.ConsoleReader;

public class EvenOddBet extends Bet {

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

public EvenOddBet(int odds) {
super(DESCRIPTION, odds);
// TODO Auto-generated constructor stub
}

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

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

}
42 changes: 12 additions & 30 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ public class 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 BlackRedBet(1),
new EvenOddBet(1),
new NumberBet(11)
};
private Wheel myWheel;
private Bet myBetGame;

/**
* Construct the game.
Expand Down Expand Up @@ -45,6 +46,10 @@ public void play (Gambler player) {
int amount = ConsoleReader.promptRange("How much do you want to bet",
0, player.getBankroll());
int whichBet = promptForBet();

// Loop through to check which type of game
myBetGame = this.myPossibleBets[whichBet];

String betChoice = placeBet(whichBet);

System.out.print("Spinning ...");
Expand Down Expand Up @@ -78,19 +83,9 @@ private int promptForBet () {
* @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");
}
else if (whichBet == 2) {
result = "" + ConsoleReader.promptRange("Enter first of three consecutive numbers",
1, Wheel.NUM_SPOTS - 3);
}
System.out.println();
return result;
String result = myBetGame.placeBet();
System.out.println();
return result;
}

/**
Expand All @@ -100,19 +95,6 @@ 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;
}
return myBetGame.betIsMade(betChoice, myWheel);
}
}
26 changes: 26 additions & 0 deletions src/roulette/NumberBet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package roulette;

import util.ConsoleReader;

public class NumberBet extends Bet {

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

public NumberBet(int odds) {
super(DESCRIPTION, odds);
// TODO Auto-generated constructor stub
}

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

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

}