Skip to content
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>
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
# example_roulette
Program to refactor that plays a game of roulette

Daniel McKee - dbm33
Karen Li - kjl32

1. The placeBet and betIsMade methods in the Bet class are much shorter. We also reduced the number of parameters to pass to Bet constructor.
2. We now have to pass a Wheel object as a parameter when winBet is called inside of BetIsMade().
3. We traded more classes for better readability and shorter functions.
4. We prefer our code because it is more readable and concise.
16 changes: 7 additions & 9 deletions src/roulette/Bet.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@
*
* @author Robert C. Duvall
*/
public class Bet {
private String myDescription;
public abstract class Bet {
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;
public Bet (int odds) {
myOdds = odds;
}

Expand All @@ -31,7 +27,9 @@ public int getOdds () {
/**
* @return name of this kind of bet
*/
public String getDescription () {
return myDescription;
}
public abstract String getDescription ();

public abstract String makeBet();

public abstract boolean winBet(String betChoice, Wheel wheel);
}
37 changes: 7 additions & 30 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
public class Game {
// name of the 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 RedBlack(1),
new OddEven(1),
new Three(11)
};
private Wheel myWheel;

/**
Expand Down Expand Up @@ -78,19 +79,8 @@ 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;
return myPossibleBets[whichBet].makeBet();
}

/**
Expand All @@ -100,19 +90,6 @@ else if (whichBet == 2) {
* @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;
}
return myPossibleBets[whichBet].winBet(betChoice, myWheel);
}
}
29 changes: 29 additions & 0 deletions src/roulette/OddEven.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package roulette;

import util.ConsoleReader;

public class OddEven extends Bet {
private static final String DESCRIPTION = "Odd or Even";

public OddEven(int odds) {
super(odds);
}

@Override
public String makeBet() {
return ConsoleReader.promptOneOf("Please bet", "even", "odd");
}

@Override
public boolean winBet(String betChoice, Wheel wheel) {
return (wheel.getNumber() % 2 == 0 && betChoice.equals("even")) ||
(wheel.getNumber() % 2 == 1 && betChoice.equals("odd"));
}

@Override
public String getDescription() {
return DESCRIPTION;
}


}
33 changes: 33 additions & 0 deletions src/roulette/RedBlack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package roulette;

import util.ConsoleReader;

public class RedBlack extends Bet{
private static final String DESCRIPTION = "Red or Black";
public RedBlack(int odds) {
super(odds);
}



@Override
public String makeBet() {
return ConsoleReader.promptOneOf("Please bet", Wheel.BLACK, Wheel.RED);
}

@Override
public boolean winBet(String betChoice, Wheel wheel) {
return wheel.getColor().equals(betChoice);
}



@Override
public String getDescription() {
return DESCRIPTION;
}




}
31 changes: 31 additions & 0 deletions src/roulette/Three.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package roulette;

import util.ConsoleReader;

public class Three extends Bet {
private static final String DESCRIPTION = "Three in a Row";

public Three(int odds) {
super(odds);
}

@Override
public String makeBet() {
// TODO Auto-generated method stub
return "" + ConsoleReader.promptRange("Enter first of three consecutive numbers",
1, Wheel.NUM_SPOTS - 3);
}

@Override
public boolean winBet(String betChoice, Wheel wheel) {
int start = Integer.parseInt(betChoice);
return (start <= wheel.getNumber() && wheel.getNumber() < start + 3);
}

@Override
public String getDescription() {
return DESCRIPTION;
}


}