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

Net IDs: at201 hpb3 - Lab Roulette #25

Open
wants to merge 1 commit 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/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class Main {
public static void main (String[] args) {
Game game = new Game();
Gambler player = new Gambler("Robert", 1000);
Gambler player = new Gambler("Adam", 1000);

System.out.println(String.format("Hello %s, let's play %s!\n", player.getName(), game.getName()));
while (player.isSolvent()) {
Expand Down
3 changes: 3 additions & 0 deletions src/betResources/Bets.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
8 changes: 8 additions & 0 deletions src/roulette/Bet.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ public String toString () {
* @param wheel information needed to check if bet won or lost
*/
public abstract boolean isMade (Wheel.SpinResult spinResult);

public String getDescription(){
return myDescription;
}

public int getOdds(){
return myOdds;
}
}
43 changes: 43 additions & 0 deletions src/roulette/Factory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package roulette;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.ResourceBundle;

import util.ConsoleReader;

public class Factory {
private List<Bet> myPossibleBets;

public Factory() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException{
myPossibleBets = new ArrayList<Bet>();

ResourceBundle myResources = ResourceBundle.getBundle("betResources/Bets.properties");
Enumeration<String> enumeration = myResources.getKeys();
while(enumeration.hasMoreElements()){
String myClassKey = enumeration.nextElement();
Class betClass = Class.forName(myClassKey);

Constructor betCtor = betClass.getConstructor(String.class, int.class);
String classValue = myResources.getString(myClassKey);
String description = classValue.substring(0, classValue.indexOf(","));
classValue = classValue.substring(classValue.indexOf(",")+1);
int odds = Integer.parseInt(classValue);
Object betObj = betCtor.newInstance(new Object[] { description, odds });
myPossibleBets.add((Bet)betObj);
}
}

public Bet printMenuChoices(){
System.out.println("You can make one of the following types of bets:");
for (int k = 0; k < myPossibleBets.size(); k++) {
System.out.println(String.format("%d) %s", (k + 1), myPossibleBets.get(k).getDescription()));
}
int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.size());
return myPossibleBets.get(response - 1);
}

}
15 changes: 2 additions & 13 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Game {
new ThreeConsecutive("Three in a Row", 11),
};
private Wheel myWheel;
private Factory myFactory;

/**
* Construct the game.
Expand All @@ -47,7 +48,7 @@ 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();
Bet b = myFactory.printMenuChoices();
b.place();

System.out.print("Spinning ...");
Expand All @@ -63,16 +64,4 @@ 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];
}
}
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.