Skip to content

Commit

Permalink
Added initial code files
Browse files Browse the repository at this point in the history
  • Loading branch information
robertcduvall committed Jan 28, 2016
1 parent d826b16 commit d5b41bf
Show file tree
Hide file tree
Showing 8 changed files with 586 additions and 5 deletions.
24 changes: 24 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Auto detect text files and perform LF normalization
* text=auto

# JAR Files And Dependencies
*.zip binary
*.jar binary
*.dll binary
*.so binary
*.jnilib binary
*.dylib binary

# Standard to msysgit
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain

*.obj diff=astextplain
124 changes: 119 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,126 @@
*.class
# IDEA Ignores #
################
*.iml
*.ipr
*.iws
.idea/
out/
local.properties

# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Generic Android ignores #
###########################
bin/
target
gen/

# Built Application Files
###########################
*.apk
*.ap_

# Files for the dex VM
###########################
*.dex

# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so

# Package Files #
*.jar
# Packages #
############
*.7z
*.dmg
*.gz
*.iso
*.rar
*.tar
*.zip
*.war
*.ear

# Logs and databases #
######################
log/
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store
.DS_Store?
ehthumbs.db
Icon?
Thumbs.db

# VCS #
#######
.svn
.svn/
CVS

# XCode 5 #
###########
*.xcuserstate
*.pbxuser
*.mode1v3
*.mode2v3
*.perspectivev3
xcuserdata/
*.xccheckout
*.moved-aside
DerivedData
!default.pbxuser
!default.mode1v3
!default.mode2v3
!default.perspectivev3


#Eclipse gitignore
*.pydevproject
.metadata
.gradle
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath

# Eclipse Core
.project

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# CDT-specific
.cproject

# Java annotation processor (APT)
.factorypath

# PDT-specific
.buildpath

# sbteclipse plugin
.target

# TeXlipse plugin
.texlipse

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
22 changes: 22 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import roulette.Gambler;
import roulette.Game;


/**
* Plays as many games of roulette until the player runs out of money.
*
* @author Robert C. Duvall
*/
public class Main {
public static void main (String[] args) {
Game game = new Game();
Gambler player = new Gambler("Robert", 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()));
}
}
37 changes: 37 additions & 0 deletions src/roulette/Bet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package roulette;


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

/**
* 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 name of this kind of bet
*/
public String getDescription () {
return myDescription;
}
}
53 changes: 53 additions & 0 deletions src/roulette/Gambler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package roulette;


/**
* Represents a Gambler simply as an amount to money to be spent.
*
* @author Robert C. Duvall
*/
public class Gambler {
private String myName;
private int myMoney;

/**
* Constructs a gambler with the given name and an initial bankroll.
*
* @param name used to refer to the gambler
* @param money initial amount of the money the gambler has to spend
*/
public Gambler (String name, int money) {
myName = name;
myMoney = money;
}

/**
* @return name of the gambler
*/
public String getName () {
return myName;
}

/**
* @return amount of money the gambler has left
*/
public int getBankroll () {
return myMoney;
}

/**
* @return true if the gambler has money left to spend, false otherwise
*/
public boolean isSolvent () {
return (myMoney > 0);
}

/**
* Changes the gambler's bankroll to reflect the given amount won or lost.
*
* @param amount money won (positive) or lost (negative) by the gambler
*/
public void updateBankroll (int amount) {
myMoney += amount;
}
}
118 changes: 118 additions & 0 deletions src/roulette/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package roulette;

import util.ConsoleReader;


/**
* Plays a game of roulette.
*
* @author Robert C. Duvall
*/
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)
};
private Wheel myWheel;

/**
* Construct the game.
*/
public Game () {
myWheel = new Wheel();
}

/**
* @return name of the game
*/
public String getName () {
return DEFAULT_NAME;
}

/**
* Play a round of roulette.
*
* Prompt player to make a bet, then spin the roulette wheel, and then verify
* that the bet is won or lost.
*
* @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);

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();
}
else {
System.out.println("*** Sorry :( You lose ***");
amount *= -1;
}
player.updateBankroll(amount);
}

/**
* Prompt the user to make a bet from a menu of choices.
*/
private int 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;
}
}
}
Loading

0 comments on commit d5b41bf

Please sign in to comment.