Skip to content

refactored roulette #31

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 1 commit 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>
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# example_roulette
Program to refactor that plays a game of roulette
It's easier to create a new option for betting. You just need to add a new subclass
to the abstract Bet class. You only need to edit the place bet function in order
to decide what the game actually entails of. This removed the if tree in the Game class which would have been more frustrating down the line to add more options. We did this by noticing that the user had three options to choose from, and the game class already has an array of each possible option, so we could bypass the conditional by just accessing the index in the myPossibleBets array. It is definitely more complex since you have to add a class and make sure it has a proper super constructor and implemented functions, which is very easy for most java users but not for beginners as much. It's not quite as readable, since it's calling other classes behind the scenes and not all in one class as it did before. The code is now more spread out across the project. Personally my partner and I currently prefer the first one; it just felt more readable and natural.

Done by sam98 and nrg12
4 changes: 3 additions & 1 deletion src/roulette/Bet.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* @author Robert C. Duvall
*/
public class Bet {
public abstract class Bet {
private String myDescription;
private int myOdds;

Expand Down Expand Up @@ -34,4 +34,6 @@ public int getOdds () {
public String getDescription () {
return myDescription;
}
public abstract String placeBet();
public abstract boolean betIsMade(String betChoice, Wheel myWheel);
}
51 changes: 11 additions & 40 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 OddEvenBet("Odd or Even", 1),
new ThreeInARowBet("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 whichBet = promptForBet();
String betChoice = whichBet.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 (whichBet.betIsMade(betChoice, myWheel)) {
System.out.println("*** Congratulations :) You win ***");
amount *= myPossibleBets[whichBet].getOdds();
amount *= whichBet.getOdds();
}
else {
System.out.println("*** Sorry :( You lose ***");
Expand All @@ -64,55 +64,26 @@ 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;
}


/**
* 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;
}
}

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

import util.ConsoleReader;

public class OddEvenBet extends Bet{

public OddEvenBet(String description, int odds) {
super(description, odds);
// TODO Auto-generated constructor stub
}

@Override
public String placeBet() {
// TODO Auto-generated method stub
return ConsoleReader.promptOneOf("Please bet", "even", "odd");
}

@Override
public boolean betIsMade(String betChoice, Wheel myWheel) {
// TODO Auto-generated method stub
return (myWheel.getNumber() % 2 == 0 && betChoice.equals("even")) ||
(myWheel.getNumber() % 2 == 1 && betChoice.equals("odd"));
}

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

import util.ConsoleReader;

public class RedBlackBet extends Bet {
public RedBlackBet(String description, int odds){
super(description, odds);
}

@Override
public String placeBet() {
// TODO Auto-generated method stub
return ConsoleReader.promptOneOf("Please bet", Wheel.BLACK, Wheel.RED);
}

@Override
public boolean betIsMade(String betChoice, Wheel myWheel) {
// TODO Auto-generated method stub
return myWheel.getColor().equals(betChoice);
}

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

import util.ConsoleReader;

public class ThreeInARowBet extends Bet {

public ThreeInARowBet(String description, int odds) {
super(description, odds);
// TODO Auto-generated constructor stub
}

@Override
public String placeBet() {
// TODO Auto-generated method stub
return Integer.toString(ConsoleReader.promptRange("enter 3 consectutive numbers", 1, Wheel.NUM_SPOTS-1));
}

@Override
public boolean betIsMade(String betChoice, Wheel myWheel) {
// TODO Auto-generated method stub
int start = Integer.parseInt(betChoice);
return (start <= myWheel.getNumber() && myWheel.getNumber() < start + 3);
}

}