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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
### William Yang and Susan Lang

# example_roulette
Program to refactor that plays a game of roulette

####1. In what ways is the refactored code simpler?

The Game class is shorter and contains fewer lengthy if/else-if/else statements. We created Bet subclasses that held their unique interactions.

####2. In what ways is the refactored code more complex?

The program is split up into several classes, so making a new Bet involves creating a whole new subclass and implementing some methods.

####3. What trade-offs did you make when refactoring your old code?

We chose to have more classes, which keeps the code organized, but now there are more classes to keep track of when you extend the game.

####4. Which code do you prefer and why?

We prefer the new code because it is easier to define behaviors for new bets. The Bet class is now abstract, which makes more sense because there is no generic bet. The methods for each type of bet are defined in the Bet subclasses, instead of being separate cases in an if-statement, which also depended on the bets being in a fixed position in an array.
2 changes: 1 addition & 1 deletion src/roulette/Bet.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ public String toString () {
* @param wheel information needed to check if bet won or lost
*/
public abstract boolean isMade (Wheel.SpinResult spinResult);
}
}
30 changes: 30 additions & 0 deletions src/roulette/Factory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package roulette;

import roulette.bets.OddEven;
import roulette.bets.RedBlack;
import roulette.bets.ThreeConsecutive;
import util.ConsoleReader;

public class Factory {
private String[] betClassNames = {"RedBlack", "OddEven", "ThreeConsecutive"};
private String[] betInputNames = {"Red or Black", "Odd or Even", "Three in a Row"};
private int[] betInputNum = {1, 1, 11};

private Bet[] myPossibleBets = {
(Bet) Class.forName(betClassNames[0]).getConstructor(String.class, Integer.TYPE).newInstance(betInputNames[0], betInputNum[0]),
(Bet) Class.forName(betClassNames[0]).getConstructor(String.class, Integer.TYPE).newInstance(betInputNames[1], betInputNum[1]),
(Bet) Class.forName(betClassNames[0]).getConstructor(String.class, Integer.TYPE).newInstance(betInputNames[2], betInputNum[2])
};

/**
* Prompt the user to make a bet from a menu of choices.
*/
public Bet promptForBet () {
System.out.println("You can make one of the following types of bets:");
for (int k = 0; k < myPossibleBets.length; k++) {
System.out.println(String.format("%d) %s", (k + 1), myPossibleBets[k]));
}
int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length);
return myPossibleBets[response - 1];
}
}
2 changes: 1 addition & 1 deletion src/roulette/Gambler.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ public boolean isSolvent () {
public void updateBankroll (int amount) {
myMoney += amount;
}
}
}
23 changes: 5 additions & 18 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ public class Game {
// name of the game
private static final String DEFAULT_NAME = "Roulette";
// add new bet subclasses here
private Bet[] myPossibleBets = {
new RedBlack("Red or Black", 1),
new OddEven("Odd or Even", 1),
new ThreeConsecutive("Three in a Row", 11),
};

private Wheel myWheel;

/**
Expand Down Expand Up @@ -47,7 +43,8 @@ public String getName () {
public void play (Gambler player) {
int amount = ConsoleReader.promptRange("How much do you want to bet",
0, player.getBankroll());
Bet b = promptForBet();
Factory betFactory = new Factory();
Bet b = betFactory.promptForBet();
b.place();

System.out.print("Spinning ...");
Expand All @@ -64,15 +61,5 @@ public void play (Gambler player) {
player.updateBankroll(amount);
}

/**
* Prompt the user to make a bet from a menu of choices.
*/
private Bet promptForBet () {
System.out.println("You can make one of the following types of bets:");
for (int k = 0; k < myPossibleBets.length; k++) {
System.out.println(String.format("%d) %s", (k + 1), myPossibleBets[k]));
}
int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length);
return myPossibleBets[response - 1];
}
}

}