Skip to content

made factory (nrg12 and med61) #34

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

Open
wants to merge 2 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>
113 changes: 55 additions & 58 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package roulette;

import roulette.bets.BetFactory;
import roulette.bets.OddEven;
import roulette.bets.RedBlack;
import roulette.bets.ThreeConsecutive;
Expand All @@ -12,67 +13,63 @@
* @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;
// 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;

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

/**
* @return name of the game
*/
public String getName () {
return DEFAULT_NAME;
}
/**
* @return name of the game
*/
public String getName () {
return DEFAULT_NAME;
}

/**
* Play a round of roulette.
*
* 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
*/
public void play (Gambler player) {
int amount = ConsoleReader.promptRange("How much do you want to bet",
0, player.getBankroll());
Bet b = promptForBet();
b.place();
/**
* Play a round of roulette.
*
* 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
*/
public void play (Gambler player) {
int amount = ConsoleReader.promptRange("How much do you want to bet",
0, player.getBankroll());
Bet b = promptForBet();
b.place();

System.out.print("Spinning ...");
Wheel.SpinResult spinResult = myWheel.spin();
System.out.println(String.format("Dropped into %s", spinResult));
if (b.isMade(spinResult)) {
System.out.println("*** Congratulations :) You win ***");
amount = b.payout(amount);
}
else {
System.out.println("*** Sorry :( You lose ***");
amount *= -1;
}
player.updateBankroll(amount);
}
System.out.print("Spinning ...");
Wheel.SpinResult spinResult = myWheel.spin();
System.out.println(String.format("Dropped into %s", spinResult));
if (b.isMade(spinResult)) {
System.out.println("*** Congratulations :) You win ***");
amount = b.payout(amount);
}
else {
System.out.println("*** Sorry :( You lose ***");
amount *= -1;
}
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];
}
/**
* Prompt the user to make a bet from a menu of choices.
*/
private Bet promptForBet () {
BetFactory f = new BetFactory();
return f.prompt();
}
}
86 changes: 86 additions & 0 deletions src/roulette/bets/BetFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package roulette.bets;

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


import roulette.Bet;
import util.ConsoleReader;

public class BetFactory {
private Bet[] myPossibleBets = {
new RedBlack("RedBlack", 1),
new OddEven("OddEven", 1),
new ThreeConsecutive("ThreeConsecutive", 11),
};
private Map paramMap; // = new HashMap<String, Integer>;

public Bet prompt() {
setUpMap();
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]));
}
String response = ConsoleReader.promptString("Please type in your type of bet: ");
return generateBet(response);
//int response = ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length);
//return myPossibleBets[response - 1];
}

private void setUpMap() {
paramMap = new HashMap<String, Integer>();
paramMap.put("RedBlack", 1);
paramMap.put("OddEven", 1);
paramMap.put("ThreeConsecutive", 11);
}

private Bet generateBet(String s) {

Class c;
try {
c = Class.forName("roulette.bets." + s);
Class[] cArg = new Class[2]; //Our constructor has 3 arguments
cArg[0] = String.class; //First argument is of *object* type Long
cArg[1] = int.class; //Second argument is of *object* type String
try {
return (Bet) c.getDeclaredConstructor(cArg).newInstance(s, paramMap.get(s));
} 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;


//
// switch (s.trim()) {
// case ("RedBlack"):
// return new RedBlack("Red or Black", 1);
// case ("OddEven"):
// return new OddEven("Odd or Even", 1);
// case ("ThreeConsecutive"):
// return new ThreeConsecutive("Three in a Row", 11);
// default:
// return null;
// }
}}