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
3 changes: 3 additions & 0 deletions src/resources/resources.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RedBlack=Red or Black,1
OddEven=Odd or Even,1
ThreeConsecutive=Three in a Row,11
20 changes: 6 additions & 14 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package roulette;

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


Expand All @@ -14,19 +12,15 @@
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 BetFactory myBetFactory;
private Wheel myWheel;

/**
* Construct the game.
*/
public Game () {
myWheel = new Wheel();
myBetFactory = new BetFactory();
}

/**
Expand Down Expand Up @@ -69,10 +63,8 @@ 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]));
}
int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length);
return myPossibleBets[response - 1];
System.out.println(myBetFactory.betChoicesToString());
int response = ConsoleReader.promptRange("Please make a choice", 1, myBetFactory.numBets());
return myBetFactory.makeBet(response);
}
}
27 changes: 0 additions & 27 deletions src/roulette/OddEven.java

This file was deleted.

26 changes: 0 additions & 26 deletions src/roulette/RedBlack.java

This file was deleted.

27 changes: 0 additions & 27 deletions src/roulette/ThreeConsecutive.java

This file was deleted.

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

import java.lang.reflect.Constructor;
import java.util.Enumeration;
import java.util.List;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;
import roulette.Bet;
import java.util.ResourceBundle;

public class BetFactory {

private ResourceBundle myResources;
private List<String> myPossibleBets;
private List<Entry<String, Integer>> myBetInfos;


public BetFactory () {
myResources = ResourceBundle.getBundle("resources/resources");
myBetInfos = new ArrayList<Entry<String, Integer>>();
myPossibleBets = new ArrayList<String>();
init();
}

private void init() {
Enumeration<String> iter = myResources.getKeys();
while (iter.hasMoreElements()) {
String key = iter.nextElement();
String value = myResources.getString(key);
String[] info = value.split(",");
String description = info[0];
System.out.println(info[1]);
int odds = Integer.parseInt(info[1]);

Entry<String, Integer> entry = new SimpleEntry<>(description, odds);
myPossibleBets.add(key);
myBetInfos.add(entry);

}

}

public Bet makeBet(int betIndex) {
String betClass = myPossibleBets.get(betIndex);
Entry<String, Integer> betInfo = myBetInfos.get(betIndex);
try {
Class<?> c = Class.forName(betClass);
Constructor<?> constructor = null;
constructor = c.getConstructor(String.class, Integer.class);
Bet bet = (Bet) constructor.newInstance(betInfo.getKey(), betInfo.getValue());
return bet;
} catch (ReflectiveOperationException e){
// Deal with error
return null;
}

}

public int numBets() {
return myPossibleBets.size();
}

public String betChoicesToString() {
String betChoices = "";
for (int k = 0; k < myPossibleBets.size(); k++) {
betChoices += (String.format("%d) %s \n", (k + 1), myPossibleBets.get(k)));
}
return betChoices;
}



}
1 change: 1 addition & 0 deletions src/roulette/bets/OddEven.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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


Expand Down
1 change: 1 addition & 0 deletions src/roulette/bets/RedBlack.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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


Expand Down
1 change: 1 addition & 0 deletions src/roulette/bets/ThreeConsecutive.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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


Expand Down