diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..fb50116 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/BetMap.properties b/src/BetMap.properties new file mode 100644 index 0000000..19bb2ee --- /dev/null +++ b/src/BetMap.properties @@ -0,0 +1,3 @@ +OddEven = Odd or Even , 1 +RedBlackBet = Red or Black, 1 +Consecutive = Three in a Row, 11 \ No newline at end of file diff --git a/src/roulette/Bet.java b/src/roulette/Bet.java index 66888a3..18d690e 100755 --- a/src/roulette/Bet.java +++ b/src/roulette/Bet.java @@ -1,37 +1,44 @@ package roulette; +import util.ConsoleReader; /** * Represents player's attempt to bet on outcome of the roulette wheel's spin. * * @author Robert C. Duvall */ -public class Bet { - private String myDescription; - private int myOdds; +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 - */ - public Bet (String description, int odds) { - myDescription = description; - myOdds = odds; - } + /** + * 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 + */ + public Bet(String description, int odds) { + myDescription = description; + myOdds = odds; + } - /** - * @return odds given by the house for this kind of bet - */ - public int getOdds () { - return myOdds; - } + /** + * @return odds given by the house for this kind of bet + */ + public int getOdds() { + return myOdds; + } - /** - * @return name of this kind of bet - */ - public String getDescription () { - return myDescription; - } -} + /** + * @return name of this kind of bet + */ + public String getDescription() { + return myDescription; + } + + public abstract String placeBet(); + + public abstract boolean isMade(String betChoice, Wheel wheel); +} \ No newline at end of file diff --git a/src/roulette/Consecutive.java b/src/roulette/Consecutive.java new file mode 100644 index 0000000..c57c343 --- /dev/null +++ b/src/roulette/Consecutive.java @@ -0,0 +1,22 @@ +package roulette; + +import util.ConsoleReader; + +public class Consecutive extends Bet { + + public Consecutive(String description, int odds) { + super(description, odds); + // TODO Auto-generated constructor stub + } + + public String placeBet() { + return "" + ConsoleReader.promptRange("Enter first of three consecutive numbers", 1, Wheel.NUM_SPOTS - 3); + + } + + public boolean isMade(String betChoice, Wheel wheel) { + int start = Integer.parseInt(betChoice); + return (start <= wheel.getNumber() && wheel.getNumber() < start + 3); + } + +} \ No newline at end of file diff --git a/src/roulette/Factory.java b/src/roulette/Factory.java new file mode 100644 index 0000000..9e2e6a5 --- /dev/null +++ b/src/roulette/Factory.java @@ -0,0 +1,40 @@ +package roulette; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.ResourceBundle; + +import util.ConsoleReader; + +public class Factory { + private ResourceBundle myBundle = ResourceBundle.getBundle("BetMap.properties"); + private String[] myPossibleBets = {"Red or Black", "Odd or Even", "Three in a row"}; + + private Bet promptForBet () throws NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + 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])); + } + try { + String myName = myPossibleBets[ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length) - 1]; + String[] myList = myBundle.getString(myName).split(","); + Class myClass = Class.forName(myName); + Constructor myConstructor = myClass.getDeclaredConstructor(new Class[] { String.class , int.class}); + return (Bet) myConstructor.newInstance(new Object[] {myList[0], myList[1]}); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + +} diff --git a/src/roulette/Game.java b/src/roulette/Game.java index 0e60ba9..7330b60 100755 --- a/src/roulette/Game.java +++ b/src/roulette/Game.java @@ -13,9 +13,9 @@ public class Game { private static final String DEFAULT_NAME = "Roulette"; // bets player can make private Bet[] myPossibleBets = { - new Bet("Red or Black", 1), - new Bet("Odd or Even", 1), - new Bet("Three in a Row", 11) + new RedBlackBet("Red or Black", 1), + new OddEven("Odd or Even", 1), + new Consecutive("Three in a Row", 11), }; private Wheel myWheel; @@ -44,15 +44,15 @@ public String getName () { public void play (Gambler player) { int amount = ConsoleReader.promptRange("How much do you want to bet", 0, player.getBankroll()); - int whichBet = promptForBet(); - String betChoice = placeBet(whichBet); + Bet b = promptForBet(); + String betChoice = b.placeBet(); System.out.print("Spinning ..."); myWheel.spin(); System.out.println(String.format("Dropped into %s %d", myWheel.getColor(), myWheel.getNumber())); - if (betIsMade(whichBet, betChoice)) { + if (b.isMade(betChoice, myWheel)) { System.out.println("*** Congratulations :) You win ***"); - amount *= myPossibleBets[whichBet].getOdds(); + amount *= b.getOdds(); } else { System.out.println("*** Sorry :( You lose ***"); @@ -64,12 +64,12 @@ public void play (Gambler player) { /** * Prompt the user to make a bet from a menu of choices. */ - private int promptForBet () { + 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].getDescription())); } - return ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length) - 1; + return myPossibleBets[ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length) - 1]; } /** @@ -77,21 +77,21 @@ private int promptForBet () { * * @param whichBet specific bet chosen by the user */ - private String placeBet (int whichBet) { - String result = ""; - if (whichBet == 0) { - result = ConsoleReader.promptOneOf("Please bet", Wheel.BLACK, Wheel.RED); - } - else if (whichBet == 1) { - result = ConsoleReader.promptOneOf("Please bet", "even", "odd"); - } - else if (whichBet == 2) { - result = "" + ConsoleReader.promptRange("Enter first of three consecutive numbers", - 1, Wheel.NUM_SPOTS - 3); - } - System.out.println(); - return result; - } +// private String placeBet (int whichBet) { +// String result = ""; +// if (whichBet == 0) { +// result = ConsoleReader.promptOneOf("Please bet", Wheel.BLACK, Wheel.RED); +// } +// else if (whichBet == 1) { +// result = ConsoleReader.promptOneOf("Please bet", "even", "odd"); +// } +// else if (whichBet == 2) { +// result = "" + ConsoleReader.promptRange("Enter first of three consecutive numbers", +// 1, Wheel.NUM_SPOTS - 3); +// } +// System.out.println(); +// return result; +// } /** * Checks if the given bet is won or lost given user's choice and result of spinning the wheel. @@ -99,20 +99,20 @@ else if (whichBet == 2) { * @param whichBet specific bet chosen by the user * @param betChoice specific value user chose to try to win the bet */ - private boolean betIsMade (int whichBet, String betChoice) { - if (whichBet == 0) { - return myWheel.getColor().equals(betChoice); - } - else if (whichBet == 1) { - return (myWheel.getNumber() % 2 == 0 && betChoice.equals("even")) || - (myWheel.getNumber() % 2 == 1 && betChoice.equals("odd")); - } - else if (whichBet == 2) { - int start = Integer.parseInt(betChoice); - return (start <= myWheel.getNumber() && myWheel.getNumber() < start + 3); - } - else { - return false; - } - } -} +// private boolean betIsMade (int whichBet, String betChoice) { +// if (whichBet == 0) { +// return myWheel.getColor().equals(betChoice); +// } +// else if (whichBet == 1) { +// return (myWheel.getNumber() % 2 == 0 && betChoice.equals("even")) || +// (myWheel.getNumber() % 2 == 1 && betChoice.equals("odd")); +// } +// else if (whichBet == 2) { +// int start = Integer.parseInt(betChoice); +// return (start <= myWheel.getNumber() && myWheel.getNumber() < start + 3); +// } +// else { +// return false; +// } +// } +} \ No newline at end of file diff --git a/src/roulette/OddEven.java b/src/roulette/OddEven.java new file mode 100644 index 0000000..2581a3a --- /dev/null +++ b/src/roulette/OddEven.java @@ -0,0 +1,22 @@ +package roulette; + +import util.ConsoleReader; + +public class OddEven extends Bet { + + public OddEven(String description, int odds) { + super(description, odds); + // TODO Auto-generated constructor stub + } + + public String placeBet() { + return ConsoleReader.promptOneOf("Please bet", "even", "odd"); + + } + + public boolean isMade(String betChoice, Wheel wheel) { + return (wheel.getNumber() % 2 == 0 && betChoice.equals("even")) + || (wheel.getNumber() % 2 == 1 && betChoice.equals("odd")); + } + +} \ No newline at end of file diff --git a/src/roulette/RedBlackBet.java b/src/roulette/RedBlackBet.java new file mode 100644 index 0000000..35477d4 --- /dev/null +++ b/src/roulette/RedBlackBet.java @@ -0,0 +1,19 @@ +package roulette; + +import util.ConsoleReader; + +public class RedBlackBet extends Bet { + + public RedBlackBet(String description, int odds) { + super(description, odds); + // TODO Auto-generated constructor stub + } + public String placeBet(){ + return ConsoleReader.promptOneOf("Please bet", Wheel.BLACK, Wheel.RED); + + } + public boolean isMade(String betChoice, Wheel wheel){ + return wheel.getColor().equals(betChoice); + } + +} \ No newline at end of file