Skip to content

kjc37 and elj16 #23

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 4 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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# example_roulette
Program to refactor that plays a game of roulette
Kelly Cochran (kjc37) and Eric Jiang (elj16)

Refactoring questions
1. We added the abstract methods betIsMade() and setAndPlaceBet().
2. We took out the placeBet() and betIsMade() methods from Game to make it's
functionality make more sense.
3. getOdds() and getDescription() are the only methods implemented in Bet.
betIsMade() and setAndPlaceBet() are implemented in the subclass.
4. The myPossibleBets array now contains the different subclasses of Bet.
21 changes: 21 additions & 0 deletions src/Bets/ColorBet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Bets;

import roulette.Bet;
import roulette.Wheel;
import util.ConsoleReader;

public class ColorBet extends Bet {

public ColorBet(String description, int odds) {
super(description, odds);
}

public void setAndPlaceBet() {
myBetChoice = ConsoleReader.promptOneOf("Please bet", Wheel.BLACK, Wheel.RED);
}

public boolean betIsMade(Wheel wheel) {
return wheel.getColor().equals(myBetChoice);
}

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

import roulette.Bet;
import roulette.Wheel;
import util.ConsoleReader;

public class EvenOddBet extends Bet {

public EvenOddBet(String description, int odds) {
super(description, odds);
}

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

@Override
public void setAndPlaceBet() {
myBetChoice = ConsoleReader.promptOneOf("Please bet", "even", "odd");

}

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

import roulette.Bet;
import roulette.Wheel;
import util.ConsoleReader;

public class NumberBet extends Bet {

public NumberBet(String description, int odds) {
super(description, odds);
}

@Override
public boolean betIsMade(Wheel wheel) {
int start = Integer.parseInt(myBetChoice);
return (start <= wheel.getNumber() && wheel.getNumber() < start + 3);
}

@Override
public void setAndPlaceBet() {
myBetChoice = "" + ConsoleReader.promptRange("Enter first of three consecutive numbers",
1, Wheel.NUM_SPOTS - 3);

}

}
5 changes: 2 additions & 3 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
public class Main {
public static void main (String[] args) {
Game game = new Game();
Gambler player = new Gambler("Robert", 1000);
Gambler player = new Gambler("Kelleric", 1000);

System.out.println(String.format("Hello %s, let's play %s!\n", player.getName(), game.getName()));
while (player.isSolvent()) {
game.play(player);
}
System.out.println();
System.out.println(String.format("\nGoodbye %s, thanks for playing!", player.getName()));
System.out.println(String.format("\n\nGoodbye %s, thanks for playing!", player.getName()));
}
}
6 changes: 5 additions & 1 deletion src/roulette/Bet.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
*
* @author Robert C. Duvall
*/
public class Bet {
public abstract class Bet {
private String myDescription;
private int myOdds;
protected String myBetChoice;

/**
* Constructs a bet with the given name and odds.
Expand All @@ -34,4 +35,7 @@ public int getOdds () {
public String getDescription () {
return myDescription;
}

public abstract boolean betIsMade(Wheel wheel);
public abstract void setAndPlaceBet();
}
68 changes: 12 additions & 56 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package roulette;

import Bets.*;
import util.ConsoleReader;


Expand All @@ -13,9 +14,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 ColorBet("Red or Black", 1),
new EvenOddBet("Odd or Even", 1),
new NumberBet("Three in a Row", 11)
};
private Wheel myWheel;

Expand All @@ -42,17 +43,16 @@ public String getName () {
* @param player one that wants to play a round of the game
*/
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 amount = ConsoleReader.promptRange("How much do you want to bet", 0, player.getBankroll());
Bet currentBet = promptForBet();
currentBet.setAndPlaceBet();

System.out.print("Spinning ...");
System.out.print("Spinning ... ");
myWheel.spin();
System.out.println(String.format("Dropped into %s %d", myWheel.getColor(), myWheel.getNumber()));
if (betIsMade(whichBet, betChoice)) {
if (currentBet.betIsMade(myWheel)) {
System.out.println("*** Congratulations :) You win ***");
amount *= myPossibleBets[whichBet].getOdds();
amount *= currentBet.getOdds();
}
else {
System.out.println("*** Sorry :( You lose ***");
Expand All @@ -64,55 +64,11 @@ 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;
}

/**
* 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;
}
return myPossibleBets[ConsoleReader.promptRange("Please make a choice", 1, myPossibleBets.length) - 1];
}
}