diff --git a/src/Casino_Simplified.puml b/src/Casino_Simplified.puml new file mode 100644 index 0000000..6287eb4 --- /dev/null +++ b/src/Casino_Simplified.puml @@ -0,0 +1,238 @@ +@startuml +title JakJak Casino - MVP UML + +skinparam classAttributeIconSize 0 +top to bottom direction + +class Casino { + - games : List + - accountManager : CasinoAccountManager + + start() + + playGame() +} + +class CasinoAccountManager { + + createAccount() + + getAccount() +} + +class CasinoAccount { + - balance : double + + deposit(amount) + + withdraw(amount) + + getBalance() +} + +interface GameInterface { + + setup() + + play() + + getRules() +} + +interface PlayerInterface { + + placeBet() + + getAccount() +} + +class CrapsGame { + +rollDice() + +isDoubles() + +resolveBet() +} + +class SlotsGame { + +spin(player : SlotsPlayer) : SpinResult + +determinePayout(result : SpinResult, bet : int) : int +} + +class SlotsPlayer { + -betAmount : int + +placeBet(amount : int) : boolean + +getBetAmount() : int +} + +class SlotMachine { + +spinReels() : SpinResult +} + +class SpinResult { + -symbols : List + +isWinner() : boolean +} + +class RouletteGame { + +spinWheel() : int + +determineWinner(player : RoulettePlayer, result : int) : boolean +} + +class RoulettePlayer { + -selectedNumber : int + -betAmount : int + +placeBet(number : int, amount : double) : boolean + +getSelectedNumber() : int +} + +class RouletteWheel { + +spin() : int +} + +class HorseRaceGame { + +runRace() : RaceHorse + +determineWinner(horses : List) : RaceHorse +} + +class HorseRacePlayer { + -selectedHorse : RaceHorse + -betAmount : double + +placeBet(horse : RaceHorse, amount : double) : boolean + +getSelectedHorse() : RaceHorse +} + +class RaceHorse { + -name : String + -position : int + +move() : int + +getPosition() : int +} + +class NumberGuessGame { + -randomNumber : int + +generateNumber() : int + +checkGuess(guess : int) : GuessResult +} + +class NumberGuessPlayer { + -guess : int + +makeGuess(number : int) : int +} + +enum GuessResult { + TOO_HIGH + TOO_LOW + CORRECT +} + +class CrapsPlayer { + +placeBet(type : CrapsBetType, amount : int) : CrapsBet + +getActiveBets() : List +} + +class BlackjackGame { + +deal(player : BlackjackPlayer, dealer : DealerPlayer) : boolean + +hit(player : BlackjackPlayer) : Card + +stand(player : BlackjackPlayer) : boolean + +determineWinner(player : BlackjackPlayer, dealer : DealerPlayer) : String +} + +class BlackjackPlayer { + -hand : Hand + +getHand() : Hand + +isBusted() : boolean +} + +class DealerPlayer { + -hand : Hand + +getHand() : Hand + +shouldHit() : boolean +} + +class Shoe { + +drawCard() : Card + +shuffle() : boolean +} + +class Hand { + -cards : List + +addCard(card : Card) : boolean + +getValue() : int +} + +class Card { + -rank : String + -suit : String + +getValue() : int +} + +abstract class CrapsBet { + -amount : int + -active : boolean + +resolve(roll : DiceRoll) : BetResult + +isActive() : boolean +} + +class PersistentBet { + +resolve(roll : DiceRoll) : BetResult +} + +class OneRollBet { + +resolve(roll : DiceRoll) : BetResult +} + +class HardWayBet { + -targetNumber : int + +resolve(roll : DiceRoll) : BetResult +} + +Casino --> CasinoAccountManager +Casino --> GameInterface : runs +CasinoAccountManager --> CasinoAccount : creates + +PlayerInterface --> CasinoAccount : uses + +GameInterface <|.. SlotsGame +GameInterface <|.. BlackjackGame +GameInterface <|.. CrapsGame +GameInterface <|.. RouletteGame +GameInterface <|.. HorseRaceGame +GameInterface <|.. NumberGuessGame + +PlayerInterface <|.. SlotsPlayer +PlayerInterface <|.. BlackjackPlayer +PlayerInterface <|.. CrapsPlayer +PlayerInterface <|.. RoulettePlayer +PlayerInterface <|.. HorseRacePlayer +PlayerInterface <|.. NumberGuessPlayer + +BlackjackGame *-- BlackjackPlayer +BlackjackGame *-- DealerPlayer +CrapsGame *-- CrapsPlayer +RouletteGame *-- RoulettePlayer +HorseRaceGame *-- HorseRacePlayer +NumberGuessGame *-- NumberGuessPlayer +NumberGuessGame --> GuessResult + +BlackjackGame *-- Shoe +Shoe *-- Deck : x decks combined +Deck *-- Card +Card *-- Hand +BlackjackPlayer *-- Hand +DealerPlayer *-- Hand + +CrapsGame *-- Die : 2 die + +RouletteGame *-- RouletteWheel + +SlotsGame *-- SlotsPlayer +SlotsGame *-- SlotMachine +SlotMachine --> SpinResult : creates + +HorseRaceGame *-- HorseRacePlayer +HorseRaceGame *-- "2..*" RaceHorse + +CrapsBet <|-- PersistentBet +CrapsBet <|-- OneRollBet +PersistentBet <|-- HardWayBet +CrapsPlayer --> CrapsBet : places +CrapsGame --> CrapsBet : resolves + + +legend left +*-- Composition (owns) +o-- Aggregation (uses) +--> Association +<|.. Implements ++ Public +- Private +endlegend + +@enduml \ No newline at end of file diff --git a/src/main/java/com/github/zipcodewilmington/Casino.java b/src/main/java/com/github/zipcodewilmington/Casino.java index 5eae9ac..b22c30a 100644 --- a/src/main/java/com/github/zipcodewilmington/Casino.java +++ b/src/main/java/com/github/zipcodewilmington/Casino.java @@ -4,75 +4,157 @@ import com.github.zipcodewilmington.casino.CasinoAccountManager; import com.github.zipcodewilmington.casino.GameInterface; import com.github.zipcodewilmington.casino.PlayerInterface; +import com.github.zipcodewilmington.casino.games.blackjack.BlackjackGame; +import com.github.zipcodewilmington.casino.games.blackjack.BlackjackPlayer; +import com.github.zipcodewilmington.casino.games.craps.CrapsGame; +import com.github.zipcodewilmington.casino.games.craps.CrapsPlayer; +import com.github.zipcodewilmington.casino.games.horserace.HorseRaceGame; +import com.github.zipcodewilmington.casino.games.horserace.HorseRacePlayer; import com.github.zipcodewilmington.casino.games.numberguess.NumberGuessGame; import com.github.zipcodewilmington.casino.games.numberguess.NumberGuessPlayer; -import com.github.zipcodewilmington.casino.games.slots.SlotsGame; -import com.github.zipcodewilmington.casino.games.slots.SlotsPlayer; +import com.github.zipcodewilmington.casino.games.roulette.RouletteGame; +import com.github.zipcodewilmington.casino.games.roulette.RoulettePlayer; import com.github.zipcodewilmington.utils.AnsiColor; import com.github.zipcodewilmington.utils.IOConsole; -/** - * Created by leon on 7/21/2020. - */ public class Casino implements Runnable { + private final IOConsole console = new IOConsole(AnsiColor.BLUE); @Override public void run() { - String arcadeDashBoardInput; + + String casinoDashboardInput; CasinoAccountManager casinoAccountManager = new CasinoAccountManager(); + do { - arcadeDashBoardInput = getArcadeDashboardInput(); - if ("select-game".equals(arcadeDashBoardInput)) { - String accountName = console.getStringInput("Enter your account name:"); - String accountPassword = console.getStringInput("Enter your account password:"); - CasinoAccount casinoAccount = casinoAccountManager.getAccount(accountName, accountPassword); - boolean isValidLogin = casinoAccount != null; - if (isValidLogin) { - String gameSelectionInput = getGameSelectionInput().toUpperCase(); - if (gameSelectionInput.equals("SLOTS")) { - play(new SlotsGame(), new SlotsPlayer()); - } else if (gameSelectionInput.equals("NUMBERGUESS")) { - play(new NumberGuessGame(), new NumberGuessPlayer()); - } else { - // TODO - implement better exception handling - String errorMessage = "[ %s ] is an invalid game selection"; - throw new RuntimeException(String.format(errorMessage, gameSelectionInput)); + + casinoDashboardInput = getCasinoDashboardInput(); + + switch (casinoDashboardInput) { + + case "1": + + console.println("=== Create Account ==="); + + String accountName = console.getStringInput("Enter Account Name:"); + String accountPassword = console.getStringInput("Enter Password:"); + + CasinoAccount newAccount + = casinoAccountManager.createAccount(accountName, accountPassword); + + casinoAccountManager.registerAccount(newAccount); + + console.println("Account Created Successfully!"); + + break; + + case "2": + + accountName = console.getStringInput("Enter Account Name:"); + accountPassword = console.getStringInput("Enter Password:"); + + CasinoAccount casinoAccount + = casinoAccountManager.getAccount(accountName, accountPassword); + + if (casinoAccount == null) { + console.println("Invalid Login!"); + break; } - } else { - // TODO - implement better exception handling - String errorMessage = "No account found with name of [ %s ] and password of [ %s ]"; - throw new RuntimeException(String.format(errorMessage, accountPassword, accountName)); - } - } else if ("create-account".equals(arcadeDashBoardInput)) { - console.println("Welcome to the account-creation screen."); - String accountName = console.getStringInput("Enter your account name:"); - String accountPassword = console.getStringInput("Enter your account password:"); - CasinoAccount newAccount = casinoAccountManager.createAccount(accountName, accountPassword); - casinoAccountManager.registerAccount(newAccount); + + String gameChoice = getGameSelectionInput(); + + switch (gameChoice) { + + case "1": + play(new RouletteGame(), + new RoulettePlayer(accountName, casinoAccount)); + break; + case "2": + console.println("Slots Coming Soon..."); + break; + + case "3": + play(new BlackjackGame(), + new BlackjackPlayer(accountName, casinoAccount)); + break; + + case "4": + play(new CrapsGame(), + new CrapsPlayer(accountName, casinoAccount)); + break; + + case "5": + play(new NumberGuessGame(), + new NumberGuessPlayer(accountName, casinoAccount)); + break; + + case "6": + play( + new HorseRaceGame(accountName), + new HorseRacePlayer(accountName, casinoAccount) + ); + break; + + case "7": + console.println("Returning to Main Menu..."); + break; + + default: + console.println("Invalid Game Selection."); + } + + break; + + case "3": + + console.println("Thank you for visiting JakJak Casino!"); + break; + + default: + + console.println("Invalid Menu Choice."); } - } while (!"logout".equals(arcadeDashBoardInput)); + + } while (!casinoDashboardInput.equals("3")); } - private String getArcadeDashboardInput() { - return console.getStringInput(new StringBuilder() - .append("Welcome to the Arcade Dashboard!") - .append("\nFrom here, you can select any of the following options:") - .append("\n\t[ create-account ], [ select-game ]") - .toString()); + private String getCasinoDashboardInput() { + + return console.getStringInput( + new StringBuilder() + .append("\n==============================") + .append("\n JAKJAK CASINO") + .append("\n==============================") + .append("\n1. Create Account") + .append("\n2. Login & Select Game") + .append("\n3. Exit") + .append("\n") + .append("\nEnter Choice:") + .toString()); } private String getGameSelectionInput() { - return console.getStringInput(new StringBuilder() - .append("Welcome to the Game Selection Dashboard!") - .append("\nFrom here, you can select any of the following options:") - .append("\n\t[ SLOTS ], [ NUMBERGUESS ]") - .toString()); + + return console.getStringInput( + new StringBuilder() + .append("\n==============================") + .append("\n SELECT A GAME") + .append("\n==============================") + .append("\n1. Roulette") + .append("\n2. Slots") + .append("\n3. Blackjack") + .append("\n4. Craps") + .append("\n5. Number Guess") + .append("\n6. Horse Race") + .append("\n7. Return to Main Menu") + .append("\n") + .append("\nEnter Choice:") + .toString()); } - private void play(Object gameObject, Object playerObject) { - GameInterface game = (GameInterface)gameObject; - PlayerInterface player = (PlayerInterface)playerObject; + private void play(GameInterface game, PlayerInterface player) { + game.add(player); game.run(); } diff --git a/src/main/java/com/github/zipcodewilmington/casino/BankAccount.java b/src/main/java/com/github/zipcodewilmington/casino/BankAccount.java new file mode 100644 index 0000000..e2efa37 --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/BankAccount.java @@ -0,0 +1,4 @@ +package com.github.zipcodewilmington.casino; + +public class BankAccount { +} diff --git a/src/main/java/com/github/zipcodewilmington/casino/CasinoAccount.java b/src/main/java/com/github/zipcodewilmington/casino/CasinoAccount.java index 654c749..c446001 100644 --- a/src/main/java/com/github/zipcodewilmington/casino/CasinoAccount.java +++ b/src/main/java/com/github/zipcodewilmington/casino/CasinoAccount.java @@ -1,9 +1,40 @@ package com.github.zipcodewilmington.casino; -/** - * Created by leon on 7/21/2020. - * `ArcadeAccount` is registered for each user of the `Arcade`. - * The `ArcadeAccount` is used to log into the system to select a `Game` to play. - */ public class CasinoAccount { + + private final String username; + private final String password; + private int balance; + + public CasinoAccount(String username, String password) { + this.username = username; + this.password = password; + this.balance = 250; + } + + public void deposit(int amount) { + if (amount > 0) { + balance += amount; + } + } + + public boolean withdraw(int amount) { + if (amount > 0 && balance >= amount) { + balance -= amount; + return true; + } + return false; + } + + public int getBalance() { + return balance; + } + + public String getUsername() { + return username; + } + + public boolean checkPassword(String password) { + return this.password.equals(password); + } } diff --git a/src/main/java/com/github/zipcodewilmington/casino/CasinoAccountManager.java b/src/main/java/com/github/zipcodewilmington/casino/CasinoAccountManager.java index 2d09ec2..8b42edb 100644 --- a/src/main/java/com/github/zipcodewilmington/casino/CasinoAccountManager.java +++ b/src/main/java/com/github/zipcodewilmington/casino/CasinoAccountManager.java @@ -1,46 +1,50 @@ package com.github.zipcodewilmington.casino; -/** - * Created by leon on 7/21/2020. - * `ArcadeAccountManager` stores, manages, and retrieves `ArcadeAccount` objects - * it is advised that every instruction in this class is logged - */ +import java.util.HashMap; +import java.util.Map; + public class CasinoAccountManager { - /** - * @param accountName name of account to be returned - * @param accountPassword password of account to be returned - * @return `ArcadeAccount` with specified `accountName` and `accountPassword` - */ + + private final Map accountByName = new HashMap<>(); + public CasinoAccount getAccount(String accountName, String accountPassword) { - String currentMethodName = new Object(){}.getClass().getEnclosingMethod().getName(); - String currentClassName = getClass().getName(); - String errorMessage = "Method with name [ %s ], defined in class with name [ %s ] has not yet been implemented"; - throw new RuntimeException(String.format(errorMessage, currentMethodName, currentClassName)); + + CasinoAccount account = accountByName.get(accountName); + + if (account == null) { + return null; + } + + if (!account.checkPassword(accountPassword)) { + return null; + } + + return account; } - /** - * logs & creates a new `ArcadeAccount` - * - * @param accountName name of account to be created - * @param accountPassword password of account to be created - * @return new instance of `ArcadeAccount` with specified `accountName` and `accountPassword` - */ public CasinoAccount createAccount(String accountName, String accountPassword) { - String currentMethodName = new Object(){}.getClass().getEnclosingMethod().getName(); - String currentClassName = getClass().getName(); - String errorMessage = "Method with name [ %s ], defined in class with name [ %s ] has not yet been implemented"; - throw new RuntimeException(String.format(errorMessage, currentMethodName, currentClassName)); + + if (accountByName.containsKey(accountName)) { + return null; + } + + CasinoAccount account = + new CasinoAccount(accountName, accountPassword); + + registerAccount(account); + + return account; } - /** - * logs & registers a new `ArcadeAccount` to `this.getArcadeAccountList()` - * - * @param casinoAccount the arcadeAccount to be added to `this.getArcadeAccountList()` - */ public void registerAccount(CasinoAccount casinoAccount) { - String currentMethodName = new Object(){}.getClass().getEnclosingMethod().getName(); - String currentClassName = getClass().getName(); - String errorMessage = "Method with name [ %s ], defined in class with name [ %s ] has not yet been implemented"; - throw new RuntimeException(String.format(errorMessage, currentMethodName, currentClassName)); + + if (casinoAccount == null) { + throw new IllegalArgumentException("Cannot register a null account"); + } + + accountByName.put( + casinoAccount.getUsername(), + casinoAccount + ); } -} +} \ No newline at end of file diff --git a/src/main/java/com/github/zipcodewilmington/casino/PlayerInterface.java b/src/main/java/com/github/zipcodewilmington/casino/PlayerInterface.java index c50b511..d5d9f0a 100644 --- a/src/main/java/com/github/zipcodewilmington/casino/PlayerInterface.java +++ b/src/main/java/com/github/zipcodewilmington/casino/PlayerInterface.java @@ -10,7 +10,7 @@ public interface PlayerInterface { /** * @return the `ArcadeAccount` used to log into the `Arcade` system to play this game */ - CasinoAccount getArcadeAccount(); + CasinoAccount getCasinoAccount(); /** * Defines how a specific implementation of `PlayerInterface` plays their respective game. diff --git a/src/main/java/com/github/zipcodewilmington/casino/cards/Card.java b/src/main/java/com/github/zipcodewilmington/casino/cards/Card.java new file mode 100644 index 0000000..c0729b2 --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/cards/Card.java @@ -0,0 +1,29 @@ +package com.github.zipcodewilmington.casino.cards; + +public class Card { + + private final Suit suit; + private final Rank rank; + + public Card(Suit suit, Rank rank) { + this.suit = suit; + this.rank = rank; + } + + public Suit getSuit() { + return suit; + } + + public Rank getRank() { + return rank; + } + + public int getValue() { + return rank.getValue(); + } + + @Override + public String toString() { + return getRank() + " of " + getSuit(); + } +} diff --git a/src/main/java/com/github/zipcodewilmington/casino/cards/Deck.java b/src/main/java/com/github/zipcodewilmington/casino/cards/Deck.java new file mode 100644 index 0000000..d879026 --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/cards/Deck.java @@ -0,0 +1,46 @@ +package com.github.zipcodewilmington.casino.cards; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class Deck { + private final List cards; + + public Deck() { + this.cards = new ArrayList<>(); + initialize(); + } + + private void initialize() { + cards.clear(); + for (Suit suit : Suit.values()) { + for (Rank rank : Rank.values()) { + cards.add(new Card(suit, rank)); + } + } + } + + public void shuffle() { + Collections.shuffle(cards); + } + + public Card deal() { + if (cards.isEmpty()) { + return null; + } + return cards.remove(0); + } + + public boolean isEmpty() { + return cards.isEmpty(); + } + + public int size() { + return cards.size(); + } + + public List getCards() { + return new ArrayList<>(cards); + } +} diff --git a/src/main/java/com/github/zipcodewilmington/casino/cards/Rank.java b/src/main/java/com/github/zipcodewilmington/casino/cards/Rank.java new file mode 100644 index 0000000..1a9d763 --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/cards/Rank.java @@ -0,0 +1,27 @@ +package com.github.zipcodewilmington.casino.cards; + +public enum Rank { + TWO(2), + THREE(3), + FOUR(4), + FIVE(5), + SIX(6), + SEVEN(7), + EIGHT(8), + NINE(9), + TEN(10), + JACK(10), + QUEEN(10), + KING(10), + ACE(11); + + private final int value; + + Rank(int value) { + this.value = value; + } + + public int getValue() { + return value; + } +} diff --git a/src/main/java/com/github/zipcodewilmington/casino/cards/Suit.java b/src/main/java/com/github/zipcodewilmington/casino/cards/Suit.java new file mode 100644 index 0000000..ce2fe42 --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/cards/Suit.java @@ -0,0 +1,18 @@ +package com.github.zipcodewilmington.casino.cards; + +public enum Suit { + CLUBS("♣"), + DIAMONDS("♦"), + HEARTS("♥"), + SPADES("♠"); + + private final String symbol; + + Suit(String symbol) { + this.symbol = symbol; + } + + public String getSymbol() { + return symbol; + } +} diff --git a/src/main/java/com/github/zipcodewilmington/casino/games/blackjack/BlackjackGame.java b/src/main/java/com/github/zipcodewilmington/casino/games/blackjack/BlackjackGame.java new file mode 100644 index 0000000..facae0a --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/games/blackjack/BlackjackGame.java @@ -0,0 +1,290 @@ +package com.github.zipcodewilmington.casino.games.blackjack; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +import com.github.zipcodewilmington.casino.CasinoAccount; +import com.github.zipcodewilmington.casino.GameInterface; +import com.github.zipcodewilmington.casino.PlayerInterface; +import com.github.zipcodewilmington.casino.cards.Card; + +public class BlackjackGame implements GameInterface { + + private final List players; + private final BlackjackPlayer dealer; + private final Shoe shoe; + + public BlackjackGame() { + this.players = new ArrayList<>(); + this.shoe = new Shoe(6); + this.shoe.shuffle(); + + CasinoAccount dealerAccount = new CasinoAccount("Dealer", ""); + this.dealer = new BlackjackPlayer("Dealer", dealerAccount); + } + + @Override + public void add(PlayerInterface player) { + if (player instanceof BlackjackPlayer) { + players.add((BlackjackPlayer) player); + } + } + + @Override + public void remove(PlayerInterface player) { + if (player instanceof BlackjackPlayer) { + players.remove(player); + } + } + + public Card dealFromShoe() { + return shoe.deal(); + } + + public int cardsRemainingInShoe() { + return shoe.size(); + } + + public void dealInitialCards() { + dealer.clearHand(); + + for (BlackjackPlayer player : players) { + player.clearHand(); + } + + for (int round = 0; round < 2; round++) { + + for (BlackjackPlayer player : players) { + player.addCard(dealFromShoe()); + } + + dealer.addCard(dealFromShoe()); + } + } + + public Card hit(BlackjackPlayer player) { + Card card = dealFromShoe(); + + if (card != null) { + player.addCard(card); + } + + return card; + } + + public boolean isBusted(BlackjackPlayer player) { + return player.getHandValue() > 21; + } + + public void playDealerTurn() { + while (dealer.getHandValue() < 17) { + hit(dealer); + } + } + + public String determineWinner(BlackjackPlayer player) { + int playerValue = player.getHandValue(); + int dealerValue = dealer.getHandValue(); + + if (playerValue > 21) { + return "Dealer"; + } + + if (dealerValue > 21) { + return player.getPlayerName(); + } + + if (playerValue > dealerValue) { + return player.getPlayerName(); + } + + if (dealerValue > playerValue) { + return "Dealer"; + } + + return "Push"; + } + + public BlackjackPlayer getDealer() { + return dealer; + } + + public List getPlayers() { + return new ArrayList<>(players); + } + + @Override + public void run() { + + Scanner scanner = new Scanner(System.in); + + boolean playing = true; + + while (playing) { + + System.out.println(); + System.out.println("=== BLACKJACK ==="); + + for (BlackjackPlayer player : players) { + + System.out.println( + player.getPlayerName() + + " Balance: $" + + player.getCasinoAccount().getBalance() + ); + + System.out.print("Enter wager: "); + int wager = scanner.nextInt(); + + if (!player.placeBet(wager)) { + System.out.println("Insufficient funds."); + continue; + } + } + + dealInitialCards(); + + for (BlackjackPlayer player : players) { + + System.out.println(); + System.out.println("Dealer shows: " + dealer.getHand().get(0)); + + while (!isBusted(player)) { + + System.out.println(); + System.out.println( + player.getPlayerName() + + "'s hand: " + + player.getHand() + ); + + System.out.println( + "Hand Value: " + + player.getHandValue() + ); + + System.out.print("Hit, Stand, or Double Down? (h/s/d): "); + + String choice + = scanner.next() + .trim() + .toLowerCase(); + + if (choice.equals("h")) { + + Card card = hit(player); + System.out.println("You drew: " + card); + + } else if (choice.equals("d")) { + + if (player.doubleDown()) { + + Card card = hit(player); + + System.out.println("Bet doubled!"); + System.out.println("You drew: " + card); + + break; + + } else { + + System.out.println("Not enough money to double down."); + } + + } else if (choice.equals("s")) { + + break; + } + } + + if (isBusted(player)) { + + System.out.println( + player.getPlayerName() + + " busted!" + ); + + } else { + + playDealerTurn(); + } + + System.out.println(); + System.out.println( + "Dealer's hand: " + + dealer.getHand() + ); + + System.out.println( + "Dealer Value: " + + dealer.getHandValue() + ); + + String winner + = determineWinner(player); + + if (winner.equals(player.getPlayerName())) { + + if (player.hasBlackjack()) { + + int blackjackPayout + = player.getBetAmount() + + (player.getBetAmount() * 3 / 2); + + player.getCasinoAccount() + .deposit(blackjackPayout); + + System.out.println("BLACKJACK! 3:2 payout!"); + + } else { + + player.getCasinoAccount() + .deposit(player.getBetAmount() * 2); + } + + } else if (winner.equals("Push")) { + + player.getCasinoAccount() + .deposit(player.getBetAmount()); + } + + System.out.println( + player.getPlayerName() + + ": " + + player.getHandValue() + + " | Dealer: " + + dealer.getHandValue() + ); + + System.out.println( + "Winner: " + winner + ); + + System.out.println( + "Balance: $" + + player.getCasinoAccount() + .getBalance() + ); + + System.out.println(); + System.out.print( + "Play another hand? (y/n): " + ); + + String again + = scanner.next() + .trim() + .toLowerCase(); + + if (!again.equals("y")) { + + System.out.println("Leaving Blackjack."); + + playing = false; + break; + } + } + } + } +} diff --git a/src/main/java/com/github/zipcodewilmington/casino/games/blackjack/BlackjackPlayer.java b/src/main/java/com/github/zipcodewilmington/casino/games/blackjack/BlackjackPlayer.java new file mode 100644 index 0000000..3b1d9ec --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/games/blackjack/BlackjackPlayer.java @@ -0,0 +1,99 @@ +package com.github.zipcodewilmington.casino.games.blackjack; + +import java.util.ArrayList; +import java.util.List; + +import com.github.zipcodewilmington.casino.CasinoAccount; +import com.github.zipcodewilmington.casino.PlayerInterface; +import com.github.zipcodewilmington.casino.cards.Card; +import com.github.zipcodewilmington.casino.cards.Rank; + +public class BlackjackPlayer implements PlayerInterface { + + private final String playerName; + private final CasinoAccount account; + private final List hand; + private int betAmount; + + public BlackjackPlayer(CasinoAccount account) { + this("Blackjack Player", account); + } + + public BlackjackPlayer(String playerName, CasinoAccount account) { + this.playerName = playerName; + this.account = account; + this.hand = new ArrayList<>(); + this.betAmount = 0; + } + + public String getPlayerName() { + return playerName; + } + + public CasinoAccount getCasinoAccount() { + return account; + } + + public boolean placeBet(int amount) { + + if (account.withdraw(amount)) { + betAmount = amount; + return true; + } + + return false; + } + + @Override + public SomeReturnType play() { + return null; + } + + public void addCard(Card card) { + if (card != null) { + hand.add(card); + } + } + + public List getHand() { + return new ArrayList<>(hand); + } + + public void clearHand() { + hand.clear(); + } + + public boolean hasBlackjack() { + return hand.size() == 2 && getHandValue() == 21; + } + + public int getBetAmount() { + return betAmount; + } + + public boolean doubleDown() { + + if (account.withdraw(betAmount)) { + betAmount *= 2; + return true; + } + + return false; + } + + public int getHandValue() { + int total = 0; + int aces = 0; + for (Card card : hand) { + total += card.getValue(); + if (card.getRank() == Rank.ACE) { + aces++; + } + } + while (total > 21 && aces > 0) { + total -= 10; + aces--; + } + return total; + } +} diff --git a/src/main/java/com/github/zipcodewilmington/casino/games/blackjack/Shoe.java b/src/main/java/com/github/zipcodewilmington/casino/games/blackjack/Shoe.java new file mode 100644 index 0000000..e2a5ebf --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/games/blackjack/Shoe.java @@ -0,0 +1,43 @@ +package com.github.zipcodewilmington.casino.games.blackjack; + +import com.github.zipcodewilmington.casino.cards.Card; +import com.github.zipcodewilmington.casino.cards.Deck; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class Shoe { + private final List cards; + + public Shoe(int numberOfDecks) { + if (numberOfDecks < 1) { + throw new IllegalArgumentException("numberOfDecks must be at least 1"); + } + this.cards = new ArrayList<>(); + initialize(numberOfDecks); + } + + private void initialize(int numberOfDecks) { + cards.clear(); + for (int i = 0; i < numberOfDecks; i++) { + Deck deck = new Deck(); + cards.addAll(deck.getCards()); + } + } + + public void shuffle() { + Collections.shuffle(cards); + } + + public Card deal() { + if (cards.isEmpty()) { + return null; + } + return cards.remove(0); + } + + public int size() { + return cards.size(); + } +} diff --git a/src/main/java/com/github/zipcodewilmington/casino/games/craps/CrapsBetType.java b/src/main/java/com/github/zipcodewilmington/casino/games/craps/CrapsBetType.java new file mode 100644 index 0000000..2d0fc5e --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/games/craps/CrapsBetType.java @@ -0,0 +1,11 @@ +package com.github.zipcodewilmington.casino.games.craps; + +public enum CrapsBetType { + PASS_LINE, + DONT_PASS, + FIELD, + HARD_4, + HARD_6, + HARD_8, + HARD_10 +} \ No newline at end of file diff --git a/src/main/java/com/github/zipcodewilmington/casino/games/craps/CrapsGame.java b/src/main/java/com/github/zipcodewilmington/casino/games/craps/CrapsGame.java new file mode 100644 index 0000000..dc14086 --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/games/craps/CrapsGame.java @@ -0,0 +1,405 @@ +package com.github.zipcodewilmington.casino.games.craps; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.Scanner; + +import com.github.zipcodewilmington.casino.GameInterface; +import com.github.zipcodewilmington.casino.PlayerInterface; + +public class CrapsGame implements GameInterface { + + private final List players; + private final Random random; + private final Scanner scanner; + + private int die1; + private int die2; + private int point; + + public CrapsGame() { + this.players = new ArrayList<>(); + this.random = new Random(); + this.scanner = new Scanner(System.in); + this.point = 0; + } + + @Override + public void add(PlayerInterface player) { + if (player instanceof CrapsPlayer) { + players.add((CrapsPlayer) player); + } + } + + @Override + public void remove(PlayerInterface player) { + if (player instanceof CrapsPlayer) { + players.remove(player); + } + } + + public int rollDice() { + die1 = random.nextInt(6) + 1; + die2 = random.nextInt(6) + 1; + + return die1 + die2; + } + + public boolean isDoubles() { + return die1 == die2; + } + + public int getDie1() { + return die1; + } + + public int getDie2() { + return die2; + } + + public int getPoint() { + return point; + } + + @Override + public void run() { + + System.out.println("\n=== CRAPS ==="); + + for (CrapsPlayer player : players) { + playGame(player); + } + } + + private void playGame(CrapsPlayer player) { + + System.out.println("\nWelcome, " + player.getPlayerName()); + System.out.println("Balance: $" + + player.getCasinoAccount().getBalance()); + + CrapsBetType betType = chooseBet(); + + System.out.print("Enter wager: $"); + int wager = scanner.nextInt(); + + if (!player.placeBet(betType, wager)) { + System.out.println("Unable to place bet."); + return; + } + + System.out.println( + "Bet placed: $" + wager + " on " + betType + ); + + switch (betType) { + + case PASS_LINE: + playPassLine(player); + break; + + case DONT_PASS: + playDontPass(player); + break; + + case FIELD: + playField(player); + break; + + case HARD_4: + case HARD_6: + case HARD_8: + case HARD_10: + playHardWay(player); + break; + } + + System.out.println( + "Balance: $" + + player.getCasinoAccount().getBalance() + ); + } + + private CrapsBetType chooseBet() { + + System.out.println("\nChoose a bet:"); + System.out.println("1. Pass Line"); + System.out.println("2. Don't Pass"); + System.out.println("3. Field"); + System.out.println("4. Hard 4"); + System.out.println("5. Hard 6"); + System.out.println("6. Hard 8"); + System.out.println("7. Hard 10"); + + int choice = scanner.nextInt(); + + switch (choice) { + case 1: + return CrapsBetType.PASS_LINE; + case 2: + return CrapsBetType.DONT_PASS; + case 3: + return CrapsBetType.FIELD; + case 4: + return CrapsBetType.HARD_4; + case 5: + return CrapsBetType.HARD_6; + case 6: + return CrapsBetType.HARD_8; + case 7: + return CrapsBetType.HARD_10; + default: + return CrapsBetType.PASS_LINE; + } + } + + private void playPassLine(CrapsPlayer player) { + + point = 0; + + int roll = rollAndDisplay(); + + // Come-out roll + if (roll == 7 || roll == 11) { + winEvenMoney(player); + return; + } + + if (roll == 2 || roll == 3 || roll == 12) { + lose(player); + return; + } + + point = roll; + + System.out.println("Point is " + point); + + while (true) { + + roll = rollAndDisplay(); + + if (roll == point) { + System.out.println("Point hit!"); + winEvenMoney(player); + point = 0; + return; + } + + if (roll == 7) { + System.out.println("Seven out!"); + lose(player); + point = 0; + return; + } + } + } + + private void playDontPass(CrapsPlayer player) { + + point = 0; + + int roll = rollAndDisplay(); + + if (roll == 2 || roll == 3) { + winEvenMoney(player); + return; + } + + if (roll == 7 || roll == 11) { + lose(player); + return; + } + + if (roll == 12) { + System.out.println("Push!"); + returnBet(player); + return; + } + + point = roll; + + System.out.println("Point is " + point); + + while (true) { + + roll = rollAndDisplay(); + + if (roll == 7) { + System.out.println("Seven out!"); + winEvenMoney(player); + point = 0; + return; + } + + if (roll == point) { + System.out.println("Point hit!"); + lose(player); + point = 0; + return; + } + } + } + + private void playField(CrapsPlayer player) { + + int roll = rollAndDisplay(); + + if (roll == 2 || roll == 12) { + + int payout = player.getBetAmount() * 3; + + player.getCasinoAccount().deposit(payout); + + System.out.println( + "Field wins! Double payout." + ); + + } else if ( + roll == 3 || + roll == 4 || + roll == 9 || + roll == 10 || + roll == 11 + ) { + + winEvenMoney(player); + + } else { + + lose(player); + } + } + + private void playHardWay(CrapsPlayer player) { + + int target = getHardWayTarget( + player.getBetType() + ); + + System.out.println( + "Rolling for Hard " + target + ); + + while (true) { + + int roll = rollAndDisplay(); + + if (roll == 7) { + System.out.println("Seven out!"); + lose(player); + return; + } + + if (roll == target) { + + if (isDoubles()) { + + int multiplier = + getHardWayMultiplier(target); + + int payout = + player.getBetAmount() + * (multiplier + 1); + + player.getCasinoAccount() + .deposit(payout); + + System.out.println( + "Hard " + target + " wins!" + ); + + } else { + + System.out.println( + target + + " rolled the easy way!" + ); + + lose(player); + } + + return; + } + } + } + + private int getHardWayTarget(CrapsBetType type) { + + switch (type) { + + case HARD_4: + return 4; + + case HARD_6: + return 6; + + case HARD_8: + return 8; + + case HARD_10: + return 10; + + default: + return 0; + } + } + + private int getHardWayMultiplier(int target) { + + if (target == 4 || target == 10) { + return 7; + } + + return 9; + } + + private int rollAndDisplay() { + + int total = rollDice(); + + System.out.println( + "Rolled: " + + die1 + + " + " + + die2 + + " = " + + total + ); + + return total; + } + + private void winEvenMoney(CrapsPlayer player) { + + int payout = + player.getBetAmount() * 2; + + player.getCasinoAccount() + .deposit(payout); + + System.out.println( + player.getPlayerName() + + " wins $" + + player.getBetAmount() + + "!" + ); + } + + private void returnBet(CrapsPlayer player) { + + player.getCasinoAccount() + .deposit(player.getBetAmount()); + + System.out.println("Bet returned."); + } + + private void lose(CrapsPlayer player) { + + System.out.println( + player.getPlayerName() + + " loses $" + + player.getBetAmount() + + "." + ); + } +} \ No newline at end of file diff --git a/src/main/java/com/github/zipcodewilmington/casino/games/craps/CrapsPlayer.java b/src/main/java/com/github/zipcodewilmington/casino/games/craps/CrapsPlayer.java new file mode 100644 index 0000000..ec6743b --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/games/craps/CrapsPlayer.java @@ -0,0 +1,54 @@ +package com.github.zipcodewilmington.casino.games.craps; + +import com.github.zipcodewilmington.casino.CasinoAccount; +import com.github.zipcodewilmington.casino.PlayerInterface; + +public class CrapsPlayer implements PlayerInterface { + + private final String playerName; + private final CasinoAccount account; + + private CrapsBetType betType; + private int betAmount; + + public CrapsPlayer(String playerName, CasinoAccount account) { + this.playerName = playerName; + this.account = account; + } + + public boolean placeBet(CrapsBetType betType, int amount) { + if (amount <= 0) { + return false; + } + + if (!account.withdraw(amount)) { + return false; + } + + this.betType = betType; + this.betAmount = amount; + return true; + } + + public String getPlayerName() { + return playerName; + } + + public CrapsBetType getBetType() { + return betType; + } + + public int getBetAmount() { + return betAmount; + } + + @Override + public CasinoAccount getCasinoAccount() { + return account; + } + + @Override + public Object play() { + return null; + } +} \ No newline at end of file diff --git a/src/main/java/com/github/zipcodewilmington/casino/games/horserace/HorseRace.java b/src/main/java/com/github/zipcodewilmington/casino/games/horserace/HorseRace.java new file mode 100644 index 0000000..f76d8ba --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/games/horserace/HorseRace.java @@ -0,0 +1,27 @@ +package com.github.zipcodewilmington.casino.games.horserace; + +import java.util.Random; + +public class HorseRace { + private String name; + private int position; + + public HorseRace(String name) { + this.name= name; + this.position = 0; + } + + + public void move () { + Random random = new Random(); + position += random.nextInt(5) + 1; + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } +} diff --git a/src/main/java/com/github/zipcodewilmington/casino/games/horserace/HorseRaceGame.java b/src/main/java/com/github/zipcodewilmington/casino/games/horserace/HorseRaceGame.java new file mode 100644 index 0000000..d6784b5 --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/games/horserace/HorseRaceGame.java @@ -0,0 +1,48 @@ +package com.github.zipcodewilmington.casino.games.horserace; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Scanner; + +import com.github.zipcodewilmington.casino.GameInterface; +import com.github.zipcodewilmington.casino.PlayerInterface; + +public class HorseRaceGame implements GameInterface { + + private String name; + private int position; + + public HorseRaceGame(String name) { + this.name = name; + this.position = 0; + } + + public void move() { + Random random = new Random(); + position += random.nextInt(5) + 1; + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } + + @Override + public void add(PlayerInterface player) { + } + + @Override + public void remove(PlayerInterface player) { + } + + @Override + public void run() { + System.out.println("\n=== HORSE RACE ==="); + System.out.println("The race is starting..."); + } +} diff --git a/src/main/java/com/github/zipcodewilmington/casino/games/horserace/HorseRacePlayer.java b/src/main/java/com/github/zipcodewilmington/casino/games/horserace/HorseRacePlayer.java new file mode 100644 index 0000000..bfbfac7 --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/games/horserace/HorseRacePlayer.java @@ -0,0 +1,28 @@ +package com.github.zipcodewilmington.casino.games.horserace; + +import com.github.zipcodewilmington.casino.CasinoAccount; +import com.github.zipcodewilmington.casino.PlayerInterface; + +public class HorseRacePlayer implements PlayerInterface { + + private HorseRace selectedHorse; + private final CasinoAccount account; + + public HorseRacePlayer(String playerName, CasinoAccount account) { + this.account = account; +} + + public HorseRace getSelectedHorse() { + return selectedHorse; + } + + @Override + public CasinoAccount getCasinoAccount() { + return account; + } + + @Override + public Object play() { + return null; + } +} diff --git a/src/main/java/com/github/zipcodewilmington/casino/games/numberguess/NumberGuessGame.java b/src/main/java/com/github/zipcodewilmington/casino/games/numberguess/NumberGuessGame.java index 7957094..ae14253 100644 --- a/src/main/java/com/github/zipcodewilmington/casino/games/numberguess/NumberGuessGame.java +++ b/src/main/java/com/github/zipcodewilmington/casino/games/numberguess/NumberGuessGame.java @@ -1,7 +1,76 @@ package com.github.zipcodewilmington.casino.games.numberguess; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.Scanner; + +import com.github.zipcodewilmington.casino.GameInterface; +import com.github.zipcodewilmington.casino.PlayerInterface; + /** * Created by leon on 7/21/2020. */ -public class NumberGuessGame { +public class NumberGuessGame implements GameInterface { + private final List players = new ArrayList<>(); + + @Override + public void add(PlayerInterface player) { + players.add(player); + } + + @Override + public void remove(PlayerInterface player) { + players.remove(player); + } + + @Override + public void run() { + for (PlayerInterface player : players) { + Random random = new Random(); + Scanner scanner = new Scanner(System.in); + + int secretNumber = random.nextInt(100) + 1; + int guesses = 0; + + while (true) { + System.out.println("======================"); + System.out.println("Guess a number between 1 and 100!"); + System.out.println("======================"); + System.out.println("Guesses so far: " + guesses); + + System.out.print("Enter your guess: "); + int guess = scanner.nextInt(); + + guesses++; + + //check if the guess is correct + if (guess == secretNumber) { + System.out.println ("Congradulations! You guessed the number!"); + break; + } + + //Guess the number to low + if (guess < secretNumber) { + System.out.println("To low :("); + } + + // guess was to high + + if (guess > secretNumber) { + System.out.println("Bring it down some!"); + } + + if (guesses == 15) { + System.out.println("========================="); + System.out.println("GAME OVER!"); + System.out.println("You've used all 15 guesses"); + System.out.println("The secret number was:" + secretNumber); + break; + } + + + } + } +} } \ No newline at end of file diff --git a/src/main/java/com/github/zipcodewilmington/casino/games/numberguess/NumberGuessPlayer.java b/src/main/java/com/github/zipcodewilmington/casino/games/numberguess/NumberGuessPlayer.java index aa5cce2..792326c 100644 --- a/src/main/java/com/github/zipcodewilmington/casino/games/numberguess/NumberGuessPlayer.java +++ b/src/main/java/com/github/zipcodewilmington/casino/games/numberguess/NumberGuessPlayer.java @@ -1,7 +1,37 @@ package com.github.zipcodewilmington.casino.games.numberguess; +import com.github.zipcodewilmington.casino.CasinoAccount; +import com.github.zipcodewilmington.casino.PlayerInterface; + /** * Created by leon on 7/21/2020. */ -public class NumberGuessPlayer { -} \ No newline at end of file +public class NumberGuessPlayer implements PlayerInterface { + + private final String playerName; + private final CasinoAccount account; + + public NumberGuessPlayer() { + this("Number Guess Player", + new CasinoAccount("Number Guess Player", "")); + } + + public NumberGuessPlayer(String playerName, CasinoAccount account) { + this.playerName = playerName; + this.account = account; + } + + public String getPlayerName() { + return playerName; + } + + @Override + public CasinoAccount getCasinoAccount() { + return account; + } + + @Override + public SomeReturnType play() { + return null; + } +} diff --git a/src/main/java/com/github/zipcodewilmington/casino/games/roulette/RouletteBetType.java b/src/main/java/com/github/zipcodewilmington/casino/games/roulette/RouletteBetType.java new file mode 100644 index 0000000..659902e --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/games/roulette/RouletteBetType.java @@ -0,0 +1,8 @@ +package com.github.zipcodewilmington.casino.games.roulette; + +public enum RouletteBetType { + NUMBER, + RED, + BLACK, + GREEN +} \ No newline at end of file diff --git a/src/main/java/com/github/zipcodewilmington/casino/games/roulette/RouletteGame.java b/src/main/java/com/github/zipcodewilmington/casino/games/roulette/RouletteGame.java new file mode 100644 index 0000000..d80f43e --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/games/roulette/RouletteGame.java @@ -0,0 +1,164 @@ +package com.github.zipcodewilmington.casino.games.roulette; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +import com.github.zipcodewilmington.casino.GameInterface; +import com.github.zipcodewilmington.casino.PlayerInterface; +import com.github.zipcodewilmington.utils.AnsiColor; +import com.github.zipcodewilmington.utils.IOConsole; + +public class RouletteGame implements GameInterface { + + private RouletteWheel wheel; + private List players; + private IOConsole console; + + public RouletteGame() { + this.wheel = new RouletteWheel(); + this.players = new ArrayList<>(); + this.console = new IOConsole(AnsiColor.GREEN); + } + + @Override + public void add(PlayerInterface player) { + players.add(player); + } + + @Override + public void remove(PlayerInterface player) { + players.remove(player); + } + + @Override + public void run() { + + Scanner scanner = new Scanner(System.in); + + for (PlayerInterface playerInterface : players) { + + RoulettePlayer player = (RoulettePlayer) playerInterface; + + System.out.println("\nBalance: $" + + player.getCasinoAccount().getBalance()); + + System.out.println("1. Bet on a number"); + System.out.println("2. Bet on RED"); + System.out.println("3. Bet on BLACK"); + + System.out.print("Choose bet type: "); + int choice = scanner.nextInt(); + + System.out.print("Enter wager amount: "); + int betAmount = scanner.nextInt(); + + boolean betPlaced = false; + + if (choice == 1) { + + System.out.print("Choose a number 0-36: "); + int selectedNumber = scanner.nextInt(); + + betPlaced + = player.placeNumberBet(selectedNumber, betAmount); + + } else if (choice == 2) { + + betPlaced + = player.placeColorBet( + RouletteBetType.RED, + betAmount + ); + + } else if (choice == 3) { + + betPlaced + = player.placeColorBet( + RouletteBetType.BLACK, + betAmount + ); + + } else { + + System.out.println("Invalid selection."); + return; + } + + if (!betPlaced) { + System.out.println("Unable to place bet."); + return; + } + + int winningNumber = wheel.spin(); + + RouletteBetType winningColor + = getWinningColor(winningNumber); + + boolean winner; + + if (player.getBetType() == RouletteBetType.NUMBER) { + + winner + = player.getSelectedNumber() == winningNumber; + + } else { + + winner + = player.getBetType() == winningColor; + } + + System.out.println("\nWinning Number: " + winningNumber); + System.out.println("Winning Color: " + winningColor); + + if (winner) { + + player.getCasinoAccount() + .deposit(player.getBetAmount() * 2); + + System.out.println("Winner!"); + + } else { + + System.out.println("You Lost!"); + } + + System.out.println("Balance: $" + + player.getCasinoAccount().getBalance()); + } + } + + + private RouletteBetType getWinningColor(int winningNumber) { + + if (winningNumber == 0) { + return RouletteBetType.GREEN; + } + + switch (winningNumber) { + + case 1: + case 3: + case 5: + case 7: + case 9: + case 12: + case 14: + case 16: + case 18: + case 19: + case 21: + case 23: + case 25: + case 27: + case 30: + case 32: + case 34: + case 36: + return RouletteBetType.RED; + + default: + return RouletteBetType.BLACK; + } + } +} diff --git a/src/main/java/com/github/zipcodewilmington/casino/games/roulette/RoulettePlayer.java b/src/main/java/com/github/zipcodewilmington/casino/games/roulette/RoulettePlayer.java new file mode 100644 index 0000000..34c12c5 --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/games/roulette/RoulettePlayer.java @@ -0,0 +1,68 @@ +package com.github.zipcodewilmington.casino.games.roulette; + +import com.github.zipcodewilmington.casino.CasinoAccount; +import com.github.zipcodewilmington.casino.PlayerInterface; + +public class RoulettePlayer implements PlayerInterface { + + private final String playerName; + private final CasinoAccount account; + + private RouletteBetType betType; + private int selectedNumber; + private int betAmount; + + public RoulettePlayer(String playerName, CasinoAccount account) { + this.playerName = playerName; + this.account = account; + this.selectedNumber = 0; + this.betAmount = 0; + } + + public boolean placeNumberBet(int selectedNumber, int betAmount) { + if (account.withdraw(betAmount)) { + this.betType = RouletteBetType.NUMBER; + this.selectedNumber = selectedNumber; + this.betAmount = betAmount; + return true; + } + + return false; + } + + public boolean placeColorBet(RouletteBetType betType, int betAmount) { + if (account.withdraw(betAmount)) { + this.betType = betType; + this.betAmount = betAmount; + return true; + } + + return false; + } + + public String getPlayerName() { + return playerName; + } + + public RouletteBetType getBetType() { + return betType; + } + + public int getSelectedNumber() { + return selectedNumber; + } + + public int getBetAmount() { + return betAmount; + } + + @Override + public CasinoAccount getCasinoAccount() { + return account; + } + + @Override + public Boolean play() { + return null; + } +} diff --git a/src/main/java/com/github/zipcodewilmington/casino/games/roulette/RouletteResult.java b/src/main/java/com/github/zipcodewilmington/casino/games/roulette/RouletteResult.java new file mode 100644 index 0000000..fb0e46a --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/games/roulette/RouletteResult.java @@ -0,0 +1,27 @@ +package com.github.zipcodewilmington.casino.games.roulette; + +public class RouletteResult { + + private boolean winner; + private int winningNumber; + private RouletteBetType winningColor; + + public RouletteResult(boolean winner, int winningNumber, RouletteBetType winningColor) { + + this.winner = winner; + this.winningNumber = winningNumber; + this.winningColor = winningColor; + } + + public boolean isWinner() { + return winner; + } + + public int getWinningNumber() { + return winningNumber; + } + + public RouletteBetType getWinningColor() { + return winningColor; + } +} \ No newline at end of file diff --git a/src/main/java/com/github/zipcodewilmington/casino/games/roulette/RouletteWheel.java b/src/main/java/com/github/zipcodewilmington/casino/games/roulette/RouletteWheel.java new file mode 100644 index 0000000..98ebdd6 --- /dev/null +++ b/src/main/java/com/github/zipcodewilmington/casino/games/roulette/RouletteWheel.java @@ -0,0 +1,16 @@ +package com.github.zipcodewilmington.casino.games.roulette; + +import java.util.Random; + +public class RouletteWheel { + + private Random random; + + public RouletteWheel() { + this.random = new Random(); + } + + public int spin() { + return random.nextInt(37); + } +} \ No newline at end of file diff --git a/src/main/java/com/github/zipcodewilmington/casino/games/slots/SlotsGame.java b/src/main/java/com/github/zipcodewilmington/casino/games/slots/SlotsGame.java index 8cb20c7..7de5735 100644 --- a/src/main/java/com/github/zipcodewilmington/casino/games/slots/SlotsGame.java +++ b/src/main/java/com/github/zipcodewilmington/casino/games/slots/SlotsGame.java @@ -1,7 +1,31 @@ -package com.github.zipcodewilmington.casino.games.slots; +// package com.github.zipcodewilmington.casino.games.slots; -/** - * Created by leon on 7/21/2020. - */ -public class SlotsGame { -} +// import com.github.zipcodewilmington.casino.GameInterface; +// import com.github.zipcodewilmington.casino.PlayerInterface; + +// import java.util.ArrayList; +// import java.util.List; + +// /** +// * Created by leon on 7/21/2020. +// */ +// public class SlotsGame implements GameInterface { +// private final List players = new ArrayList<>(); + +// @Override +// public void add(PlayerInterface player) { +// players.add(player); +// } + +// @Override +// public void remove(PlayerInterface player) { +// players.remove(player); +// } + +// @Override +// public void run() { +// for (PlayerInterface player : players) { +// player.play(); +// } +// } +// } diff --git a/src/main/java/com/github/zipcodewilmington/casino/games/slots/SlotsPlayer.java b/src/main/java/com/github/zipcodewilmington/casino/games/slots/SlotsPlayer.java index f89ebd7..8a65708 100644 --- a/src/main/java/com/github/zipcodewilmington/casino/games/slots/SlotsPlayer.java +++ b/src/main/java/com/github/zipcodewilmington/casino/games/slots/SlotsPlayer.java @@ -1,7 +1,35 @@ -package com.github.zipcodewilmington.casino.games.slots; +// package com.github.zipcodewilmington.casino.games.slots; -/** - * Created by leon on 7/21/2020. - */ -public class SlotsPlayer { -} \ No newline at end of file +// import com.github.zipcodewilmington.casino.CasinoAccount; +// import com.github.zipcodewilmington.casino.PlayerInterface; + +// /** +// * Created by leon on 7/21/2020. +// */ +// public class SlotsPlayer implements PlayerInterface { +// private final String playerName; +// private final CasinoAccount account; + +// public SlotsPlayer() { +// this("Slots Player", new CasinoAccount()); +// } + +// public SlotsPlayer(String playerName, CasinoAccount account) { +// this.playerName = playerName; +// this.account = account; +// } + +// public String getPlayerName() { +// return playerName; +// } + +// @Override +// public CasinoAccount getArcadeAccount() { +// return account; +// } + +// @Override +// public SomeReturnType play() { +// return null; +// } +// } \ No newline at end of file diff --git a/src/test/java/com/github/zipcodewilmington/casino/CasinoAccountManagerTest.java b/src/test/java/com/github/zipcodewilmington/casino/CasinoAccountManagerTest.java new file mode 100644 index 0000000..00a84f9 --- /dev/null +++ b/src/test/java/com/github/zipcodewilmington/casino/CasinoAccountManagerTest.java @@ -0,0 +1,50 @@ +package com.github.zipcodewilmington.casino; + +import com.github.zipcodewilmington.casino.games.numberguess.NumberGuessPlayer; +import com.github.zipcodewilmington.casino.games.slots.SlotsPlayer; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; + +public class CasinoAccountManagerTest { + + @Test + public void createAndRegisterAccount_allowsLoginLookup() { + CasinoAccountManager manager = new CasinoAccountManager(); + CasinoAccount account = manager.createAccount("player-one", "s3cret"); + manager.registerAccount(account); + + CasinoAccount actual = manager.getAccount("player-one", "s3cret"); + + assertSame(account, actual); + assertNotNull(actual.getAccountReference()); + } + + @Test + public void getAccount_withWrongPassword_returnsNull() { + CasinoAccountManager manager = new CasinoAccountManager(); + CasinoAccount account = manager.createAccount("player-one", "s3cret"); + manager.registerAccount(account); + + CasinoAccount actual = manager.getAccount("player-one", "wrong-password"); + + assertNull(actual); + } + + @Test + public void playersStoreNameAndLoggedInAccountReference() { + CasinoAccount account = new CasinoAccount("lucky-player", "pw"); + SlotsPlayer slotsPlayer = new SlotsPlayer("Lucky", account); + NumberGuessPlayer numberGuessPlayer = new NumberGuessPlayer("Lucky", account); + + assertEquals("Lucky", slotsPlayer.getPlayerName()); + assertEquals("Lucky", numberGuessPlayer.getPlayerName()); + assertSame(account, slotsPlayer.getArcadeAccount()); + assertSame(account, numberGuessPlayer.getArcadeAccount()); + assertSame(account, slotsPlayer.getAccount()); + assertSame(account, numberGuessPlayer.getAccount()); + } +} diff --git a/src/test/java/com/github/zipcodewilmington/casino/CasinoAccountTest.java b/src/test/java/com/github/zipcodewilmington/casino/CasinoAccountTest.java new file mode 100644 index 0000000..65cea84 --- /dev/null +++ b/src/test/java/com/github/zipcodewilmington/casino/CasinoAccountTest.java @@ -0,0 +1,25 @@ +package com.github.zipcodewilmington.casino; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class CasinoAccountTest { + @Test + public void constructorWithNoArguments_defaultsBankAccountToNull() { + CasinoAccount casinoAccount = new CasinoAccount(); + + assertNull(casinoAccount.getBankAccount()); + } + + @Test + public void constructorWithBankAccount_setsBankAccount() { + BankAccount bankAccount = new BankAccount(); + CasinoAccount casinoAccount = new CasinoAccount(bankAccount); + + assertNotNull(casinoAccount.getBankAccount()); + assertSame(bankAccount, casinoAccount.getBankAccount()); + } +} diff --git a/src/test/java/com/github/zipcodewilmington/casino/games/blackjack/BlackjackGameTest.java b/src/test/java/com/github/zipcodewilmington/casino/games/blackjack/BlackjackGameTest.java new file mode 100644 index 0000000..0bec5ce --- /dev/null +++ b/src/test/java/com/github/zipcodewilmington/casino/games/blackjack/BlackjackGameTest.java @@ -0,0 +1,23 @@ +package com.github.zipcodewilmington.casino.games.blackjack; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +import com.github.zipcodewilmington.casino.CasinoAccount; + +public class BlackjackGameTest { + + @Test + public void dealInitialCards_dealsTwoCardsToPlayerAndDealer() { + BlackjackPlayer dealer = new BlackjackPlayer(new CasinoAccount()); + BlackjackPlayer player = new BlackjackPlayer(new CasinoAccount()); + BlackjackGame game = new BlackjackGame(5, dealer); + + game.add(player); + game.dealInitialCards(); + + assertEquals(2, player.getHand().size()); + assertEquals(2, dealer.getHand().size()); + assertEquals(48, game.cardsRemainingInShoe()); + } +} diff --git a/src/test/java/com/github/zipcodewilmington/casino/games/blackjack/ShoeTest.java b/src/test/java/com/github/zipcodewilmington/casino/games/blackjack/ShoeTest.java new file mode 100644 index 0000000..de1f3fb --- /dev/null +++ b/src/test/java/com/github/zipcodewilmington/casino/games/blackjack/ShoeTest.java @@ -0,0 +1,38 @@ +package com.github.zipcodewilmington.casino.games.blackjack; + +import com.github.zipcodewilmington.casino.cards.Card; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class ShoeTest { + + @Test + public void constructorWithOneDeck_has52Cards() { + Shoe shoe = new Shoe(1); + assertEquals(52, shoe.size()); + } + + @Test + public void constructorWithMultipleDecks_hasExpectedCardCount() { + Shoe shoe = new Shoe(6); + assertEquals(312, shoe.size()); + } + + @Test + public void deal_reducesShoeSizeByOne() { + Shoe shoe = new Shoe(1); + int before = shoe.size(); + Card card = shoe.deal(); + + assertNotNull(card); + assertEquals(before - 1, shoe.size()); + } + + @Test + public void constructorWithZeroDecks_throwsIllegalArgumentException() { + assertThrows(IllegalArgumentException.class, () -> new Shoe(0)); + } +}