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

cmt57 mdf15 #29

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>
3 changes: 3 additions & 0 deletions src/BetMap.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
OddEven = Odd or Even , 1
RedBlackBet = Red or Black, 1
Consecutive = Three in a Row, 11
59 changes: 33 additions & 26 deletions src/roulette/Bet.java
Original file line number Diff line number Diff line change
@@ -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);
}
22 changes: 22 additions & 0 deletions src/roulette/Consecutive.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
40 changes: 40 additions & 0 deletions src/roulette/Factory.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
82 changes: 41 additions & 41 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 ***");
Expand All @@ -64,55 +64,55 @@ 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];
}

/**
* Place the given bet by prompting user for specific information need to complete that bet.
*
* @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.
*
* @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;
// }
// }
}
22 changes: 22 additions & 0 deletions src/roulette/OddEven.java
Original file line number Diff line number Diff line change
@@ -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"));
}

}
19 changes: 19 additions & 0 deletions src/roulette/RedBlackBet.java
Original file line number Diff line number Diff line change
@@ -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);
}

}