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>
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# example_roulette
Parit Burintrathikul (pb111)
Zdravko Paskalev (zap3)

Program to refactor that plays a game of roulette

Analysis of the code:

Currently, the way to add in an extra bet would be to add a new if statement in every single method and a new Bet object in the initial formation of the Bet[] in Game.java, which is very inefficient and ugly.

We refactored the code in a way which removes the redundancy of the multiple if statements by polymorphism and extending the bets class in a a variety of different subclasses for each bet. The refactored code is simpler in the sense that the if statements are removed and the new bets are set up in a way that you can make a new class for each new bet. This way it is a lot easy to look through the code since every bet is separated in its own class.
9 changes: 4 additions & 5 deletions src/roulette/Bet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@

/**
* Represents player's attempt to bet on outcome of the roulette wheel's spin.
*
*
* @author Robert C. Duvall
*/
public abstract class Bet {

private String myDescription;
private int myOdds;

/**
* Constructs a bet with the given name and odds.
*
*
* @param description name of this kind of bet
* @param odds odds given by the house for this kind of bet
*/
Expand All @@ -35,9 +36,6 @@ public String toString () {
return myDescription;
}

/**
* Place bet by prompting user for the specific information need to complete this bet.
*/
public abstract void place ();

/**
Expand All @@ -46,4 +44,5 @@ public String toString () {
* @param wheel information needed to check if bet won or lost
*/
public abstract boolean isMade (Wheel.SpinResult spinResult);

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

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

import roulette.bets.*;

public class BetFactory {

private Map<String,Class> myMap = new HashMap();

public void addBet(String Id, Class product){
myMap.put(Id,product);
}
public Bet getBet(String str,Integer in ){
// if(str.equals("Odd or Even")){
// return new OddEven(str, 1);
// }
// else if(str.equals("Red or Black")){
// return new RedBlack(str, 1);
// }
// else if(str.equals("Three Consecutive")){
// return new ThreeConsecutive(str, 1);
// }
// return null;
Class productClass = (Class) myMap.get(str);
try {
Constructor productCons = productClass.getDeclaredConstructor(new Class[]{String.class,Integer.class});
return (Bet) productCons.newInstance(str,in);
}
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
6 changes: 4 additions & 2 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@

/**
* Plays a game of roulette.
*
*
* @author Robert C. Duvall
*/
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 All @@ -39,7 +41,7 @@ public String getName () {
/**
* Play a round of roulette.
*
* Prompt player to make a bet, then spin the roulette wheel, and then verify
* Prompt player to make a bet, then spin the roulette wheel, and then verify
* that the bet is won or lost.
*
* @param player one that wants to play a round of the game
Expand Down
6 changes: 3 additions & 3 deletions src/roulette/Wheel.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Represents a roulette wheel that can be spun to get a color, either red, black, or green, and a
* number, between 0 and 37. The numbers 0 and 37 represent the roulette values 0 and 00,
* respectively.
*
*
* @author Robert C. Duvall
*/
public class Wheel {
Expand Down 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