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

csv5, reb36 #5

Open
wants to merge 3 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>
2 changes: 1 addition & 1 deletion src/roulette/Bet.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public String toString () {
/**
* Place bet by prompting user for the specific information need to complete this bet.
*/
public abstract void place ();
public abstract String place ();

/**
* Checks if bet is won or lost given result of spinning the wheel.
Expand Down
44 changes: 44 additions & 0 deletions src/roulette/BetFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package roulette;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ResourceBundle;

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

public class BetFactory {
private String[] myPossibleBets = {
"Red or Black",
"Odd or Even",
"Three in a Row"
};

private String RedBlack = "roulette.RedBlack";
private String OddEven = "roulette.OddEven";
private String ThreeConsecutive = "roulette.ThreeConsecutive";

private ResourceBundle propertiesBundle = ResourceBundle.getBundle("util/factory");

public BetFactory() {

}

public Bet getBet() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {

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

Class bet = Class.forName(propertiesBundle.getString(myPossibleBets[response]));
Constructor c = bet.getConstructor(Integer.class, String.class);
return (Bet) c.newInstance(myPossibleBets[response], 1);


}
}
40 changes: 29 additions & 11 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package roulette;

import java.lang.reflect.InvocationTargetException;

import roulette.bets.OddEven;
import roulette.bets.RedBlack;
import roulette.bets.ThreeConsecutive;
Expand All @@ -15,11 +17,8 @@ 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 betFactory;

private Wheel myWheel;

/**
Expand Down Expand Up @@ -68,11 +67,30 @@ public void play (Gambler player) {
* 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];
try {
return betFactory.getBet();
} 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();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
8 changes: 7 additions & 1 deletion src/roulette/OddEven.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package roulette;

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


Expand All @@ -19,9 +20,14 @@ public String place () {
/**
* @see Bet#isMade(String, Wheel)
*/
@Override
public boolean isMade (String betChoice, Wheel wheel) {
return (wheel.getNumber() % 2 == 0 && betChoice.equals("even")) ||
(wheel.getNumber() % 2 == 1 && betChoice.equals("odd"));
}

@Override
public boolean isMade(SpinResult spinResult) {
// TODO Auto-generated method stub
return false;
}
}
2 changes: 1 addition & 1 deletion src/roulette/Wheel.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private String getColor () {
}

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

Expand Down
3 changes: 3 additions & 0 deletions src/util/factory.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Red Or Black = roulette.RedBlack
Odd Or Even = roulette.OddEven
Three in a Row = roulette.ThreeConsecutive