Skip to content
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

Brandon Ho, Daniel Pak Reflection #35

Open
wants to merge 4 commits 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>
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
# example_roulette
Refactored by Brandon Ho and Daniel Pak

Program to refactor that plays a game of roulette

Refactored 9/10/2015

#Adding a new feature
Add a new subclass of Bet and add that to the array of classes. Edit the subclass
to have different features compared to the other subclasses.

#In what ways is the refactored code simpler?
If statements were removed, resulting in certain methods that only returned
single statements.
#In what ways is the refactored code more complex?
Have to go through multiple subclasses to get all the information instead of it all being in one class (Game).
#What trade-offs did you make when refactoring your old code?
Made the new code slightly harder to navigate because we have to look through all the subclasses to get the details, but the new code is more flexible and does not have as many hard-coded statements.
#Which code do you prefer and why?
Prefer the refactored code because it has more concise classes and not as many extra if statements. The new code is also much more flexible so if you want to add a new feature you do not have to add to if-statements. Instead, you can just add to the class, which is much more organized.
3 changes: 3 additions & 0 deletions src/description.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
OddEven = Odd or Even,1
RedBlack = Red or Black,1
ThreeConsecutive = Three in a Row,11
8 changes: 8 additions & 0 deletions src/roulette/Bet.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public Bet (String description, int odds) {
myDescription = description;
myOdds = odds;
}

public boolean checkWin(Wheel myWheel, String betChoice){
return true;
}

public String makeBet(){
return "";
}

/**
* @return amount to pay out for winning this bet
Expand Down
62 changes: 62 additions & 0 deletions src/roulette/BetFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package roulette;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

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

public class BetFactory {

Properties descriptions = new Properties();
String propertiesName = "description.properties";

public BetFactory(){
InputStream input = getClass().getClassLoader().getResourceAsStream(propertiesName);
try {
descriptions.load(input);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public Bet getBet(String type){
Class<?> c = null;
try {
c = Class.forName("roulette.bets." + type);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bet b = null;
Constructor<?> cons[] = c.getConstructors();
String des[] = descriptions.getProperty(type).split(",");

try {
b = (Bet) cons[0].newInstance(des[0], Integer.parseInt(des[1]));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}


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

import roulette.Wheel.SpinResult;
import util.ConsoleReader;

public class ColorBet extends Bet{

public ColorBet(){
super("Red or Black", 1);
}

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

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

@Override
public void place() {
// TODO Auto-generated method stub

}

@Override
public boolean isMade(SpinResult spinResult) {
// TODO Auto-generated method stub
return false;
}

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

import roulette.Wheel.SpinResult;
import util.ConsoleReader;

public class ConsecutiveBet extends Bet{

public ConsecutiveBet(){
super("Three in a Row", 11);
}

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

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

@Override
public void place() {
// TODO Auto-generated method stub

}

@Override
public boolean isMade(SpinResult spinResult) {
// TODO Auto-generated method stub
return false;
}

}
31 changes: 25 additions & 6 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package roulette;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import roulette.bets.OddEven;
import roulette.bets.RedBlack;
import roulette.bets.ThreeConsecutive;
Expand All @@ -14,19 +18,33 @@
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 String[] myPossibleBets = {
"RedBlack",
"OddEven",
"ThreeConsecutive",
};

private String propertiesName = "description.properties";

protected Properties descriptions = new Properties();

private BetFactory myFactory = new BetFactory();
private Wheel myWheel;

/**
* Construct the game.
*/
public Game () {
myWheel = new Wheel();
InputStream input = getClass().getClassLoader().getResourceAsStream(propertiesName);
try {
descriptions.load(input);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
Expand Down Expand Up @@ -70,9 +88,10 @@ public void play (Gambler player) {
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]));

System.out.println(String.format("%d) %s", (k + 1), descriptions.getProperty(myPossibleBets[k]).split(",")[0]));
}
int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length);
return myPossibleBets[response - 1];
return myFactory.getBet(myPossibleBets[response-1]);
}
}
35 changes: 35 additions & 0 deletions src/roulette/ParityBet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package roulette;

import roulette.Wheel.SpinResult;
import util.ConsoleReader;

public class ParityBet extends Bet{

public ParityBet(){
super("Odd or Even", 1);
}

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

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

@Override
public void place() {
// TODO Auto-generated method stub

}

@Override
public boolean isMade(SpinResult spinResult) {
// TODO Auto-generated method stub
return false;
}

}
4 changes: 2 additions & 2 deletions src/roulette/Wheel.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public SpinResult spin () {
}

// @return color of the current spot on the wheel
private String getColor () {
protected String getColor () {
return OUR_SPOTS[myValue];
}

// @return number of the current spot on the wheel
private int getNumber () {
protected int getNumber () {
return myValue;
}

Expand Down