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
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
# example_roulette
Program to refactor that plays a game of roulette
####Group: cw272,wj43

### Refactor the Code


1. What methods would make sense as behaviors of a Bet hierarchy (i.e., make bets open to extension)? <br />
The method of outputing the information before the player plays this round.(function placeBet()).<br />
The method of judging whether the player wins or not in this round in different rules(function judgeWin(Wheel wheel)).<br />
Because all of these methods will vary from method to method.If we create new rule , output and input.The type of bet will be totally different.

2. What methods would help improve the code in the Game's methods (i.e., close it to modification)?<br />
We made some abstract methods in bet class,which can be implemented by its derived class.These methods can reduce the condition , switch sentences and for loop.

3. What methods can be completely implemented in the Bet super-class (i.e., are common across the hierarchy), and which completely in the Bet sub-classes (i.e., vary across the hierarchy)?<br />
The methods of getOdd and getDescription can be implemented in super-class . Moreover , we create two functions of input the choice the gambler makes this round and caculating the amount of money he wins or loses.These are in the based class.<br />
The judging rule and the function of recieving the choice should be in the sub class.
4. How should the Game class create the correct Bet subclass?<br />
By creating the bet list containing all kinds of bets at the very beginning of the Game initialization.


###Add New Features
1. In what ways is the refactored code simpler?<br />
If we add a new kind of bet ,we just need to modify very little part of game class.
2. In what ways is the refactored code more complex?<br />
Every new feature needs a new class.
3. What trade-offs did you make when refactoring your old code?<br />
No trade-off.
4. Which code do you prefer and why?<br />
I think the refactored one is better.Because there is less dependency. We doesn't need to modify the majority of code when we add a new feature.It is easier to do the extention.
36 changes: 34 additions & 2 deletions src/roulette/Bet.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
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 {
public abstract class Bet {
private String myDescription;
private int myOdds;

private int myAmount;
private String myBetChoice;

// public static final int RED_BLACK = 0;
// public static final int ODD_EVEN = 1;
// public static final int THREE_IN_ROW = 2;
/**
* Constructs a bet with the given name and odds.
*
Expand All @@ -20,6 +26,9 @@ public Bet (String description, int odds) {
myDescription = description;
myOdds = odds;
}

public abstract String placeBet();


/**
* @return odds given by the house for this kind of bet
Expand All @@ -34,4 +43,27 @@ public int getOdds () {
public String getDescription () {
return myDescription;
}

public void setAmount(int amount){
myAmount = amount;
}

public String getBetChoice() {
return myBetChoice;
}

public int getLoseAmount() {
return -myAmount;
}

public int getWinAmount() {
return myAmount*myOdds;
}

public void setBetChoice(String betChoice){
myBetChoice = betChoice;
}

public abstract boolean judgeWin(Wheel wheel);

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

import util.ConsoleReader;

public class EvenOdd extends Bet {
private final static int EVENODD_ODD = 1;
private final static String EVENODD_DIS = "Even or Odd";

private String myBetChoice;

public EvenOdd()
{
super(EVENODD_DIS, EVENODD_ODD);
}

// public String getBetChoice() {
// return myBetChoice;
// }

// public void setBetChoice(String betChoice) {
// this.myBetChoice = betChoice;
// }

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

@Override
public String placeBet(){
myBetChoice = ConsoleReader.promptOneOf("Please bet", "even", "odd");
return myBetChoice;
}
}
101 changes: 60 additions & 41 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ 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 Bet("Red or Black", 1),
// new Bet("Odd or Even", 1),
// new Bet("Three in a Row", 11)
new RedBlack(),
new EvenOdd(),
new HighLow(),
new ThreeInRow(),
new TwoInRow(),
new SingleNum()
};
private Wheel myWheel;

Expand Down Expand Up @@ -44,21 +50,24 @@ 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);
int whichBet = promptForBet();
Bet curBet = myPossibleBets[whichBet];
myPossibleBets[whichBet].setAmount(amount);

String betChoice = curBet.placeBet();
curBet.setBetChoice(betChoice);

System.out.print("Spinning ...");
myWheel.spin();
System.out.println(String.format("Dropped into %s %d", myWheel.getColor(), myWheel.getNumber()));
if (betIsMade(whichBet, betChoice)) {
System.out.println("*** Congratulations :) You win ***");
amount *= myPossibleBets[whichBet].getOdds();
if (curBet.judgeWin(myWheel)) {
System.out.println("*** Congratulations :) You win ***");
player.updateBankroll(curBet.getWinAmount());
}
else {
System.out.println("*** Sorry :( You lose ***");
amount *= -1;
player.updateBankroll(curBet.getLoseAmount());
}
player.updateBankroll(amount);
}

/**
Expand All @@ -77,42 +86,52 @@ 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 = "";
//// switch(whichBet) {
//// case 1:
//// break;
//// case 2:
//// break;
//// case 3:
//// break;
//// default:
//// }
//
// 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;
// }
// }
}
27 changes: 27 additions & 0 deletions src/roulette/HighLow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package roulette;

import util.ConsoleReader;

public class HighLow extends Bet {
private final static int HIGHLOW_ODD = 1;
private final static String HIGHLOW_DIS = "High or Low";

private String myBetChoice;

public HighLow()
{
super(HIGHLOW_DIS, HIGHLOW_ODD);
}

@Override
public boolean judgeWin(Wheel wheel){
return (wheel.getNumber() >= 0 && wheel.getNumber()<=18 && myBetChoice.equals("low")) ||
(wheel.getNumber() >= 19 && wheel.getNumber() <= 36 && myBetChoice.equals("high"));
}

@Override
public String placeBet(){
myBetChoice = "" + ConsoleReader.promptOneOf("Please bet", "high", "low");
return myBetChoice;
}
}
32 changes: 32 additions & 0 deletions src/roulette/RedBlack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package roulette;

import util.ConsoleReader;

public class RedBlack extends Bet {
private final static int REDBLACK_ODD = 1;
private final static String REDBLACK_DIS = "Red or Black";

private String myBetChoice;

public RedBlack()
{
super(REDBLACK_DIS, REDBLACK_ODD);
}

// public String getBetChoice() {
// return myBetChoice;
// }

@Override
public boolean judgeWin(Wheel wheel){
return wheel.getColor().equals(myBetChoice);
// return result;
}

@Override
public String placeBet(){
myBetChoice = ConsoleReader.promptOneOf("Please bet", Wheel.BLACK, Wheel.RED);

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

import util.ConsoleReader;

public class SingleNum extends XInRow {
private final static int SINGLENUM_ODD = 35;
private final static String SINGLENUM_DIS = "Single Number";

private String myBetChoice;

public SingleNum()
{
super(SINGLENUM_DIS, SINGLENUM_ODD);
super.setX(1);
}

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

import util.ConsoleReader;

public class ThreeInRow extends XInRow {
private final static int THREEINROW_ODD = 11;
private final static String THREEINROW_DIS = "Three in a Row";

private String myBetChoice;

public ThreeInRow()
{
super(THREEINROW_DIS, THREEINROW_ODD);
super.setX(3);
}

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

import util.ConsoleReader;

public class TwoInRow extends XInRow {
private final static int TWOINROW_ODD = 17;
private final static String TWOINROW_DIS = "Two in a Row";

private String myBetChoice;

public TwoInRow()
{
super(TWOINROW_DIS, TWOINROW_ODD);
super.setX(2);
}

}
Loading