public void playCard(Card card) { playerHand.validateHasCard(card); playerHand.remove(card); currentHand .orElseThrow(() -> new GameRuleException("Player is not participating in a hand!")) .playsCard(card, this); }
/* * Deal two cards to each player. */ protected void dealPlayers() { for (Player player : players) { // deal first card to player if (player.bets.isEmpty()) { continue; } // if player has placed bet, deal first card Card dealtCard = shoe.getNextCard(); // add card to hand PlayerHand firstHand = player.bets.get(0); firstHand.add(dealtCard); // notify strategies of dealt card notifyDealt(dealtCard); } for (Player player : players) { // deal second card to player if (player.bets.isEmpty()) { continue; } // if player has placed bet, deal second card Card dealtCard = shoe.getNextCard(); // add card to hand PlayerHand firstHand = player.bets.get(0); firstHand.add(dealtCard); // notify strategies of dealt card notifyDealt(dealtCard); // notify observers of dealt hand player.playerDealt(firstHand); playerDealt(player, firstHand); } }
public void tradingCards(List<Card> cards) { if (!trading) { throw new GameRuleException("Trading not allowed at this time."); } cards.stream().forEach(playerHand::validateHasCard); playerHand.removeAll(cards); trading = false; currentHand .orElseThrow(() -> new GameRuleException("Player is not participating in a hand!")) .tradesCard(this, cards); }
public void receiveCards(List<Card> cards) { playerHand.addAll(cards); endpoint.send(new ReceivedTradeDto(cards)); }
/* * Draw cards for the house. */ protected void drawHouse() { // notify players of down card notifyDealt(dealerHand.cards.get(1)); // notify observers of down card dealerDealt(dealerHand.cards.get(1), dealerHand); // draw cards for dealer while (!dealerHand.isBusted() && (dealerStrategy.getAction(dealerHand) == DealerStrategyAction.HIT)) { Card dealtCard = shoe.getNextCard(); dealerHand.add(dealtCard); // notify players of dealt card notifyDealt(dealtCard); // notify observers of dealt card dealerDraws(dealtCard, dealerHand); } // dealer hand is finished dealerHand.finished = true; // evaluate dealer hand boolean dealerBusted = dealerHand.isBusted(); int dealerHighValue = dealerHand.getHighValidValue(); if (dealerBusted) { // notify observers of dealer bust dealerBusts(dealerHand); } else { // notify observers of dealer stand dealerStands(dealerHand); } // evaluate hands of each player, adjusting bankrolls for (Player player : players) { for (PlayerHand hand : player.bets) { if (!hand.isBlackjack() && !hand.isBusted()) { // player hand did not adjust bankroll earlier, get its best value int highValue = hand.getHighValidValue(); if (dealerBusted || (dealerHighValue < highValue)) { // dealer hand busted or player hand beat dealer hand, add to // bankroll player.bankroll += hand.betAmount; // notify observers of player win player.playerWins(hand, hand.betAmount, player.bankroll); playerWins(player, hand, hand.betAmount, player.bankroll); } else if (dealerHighValue > highValue) { // dealer hand beat player hand, deduct from bankroll player.bankroll -= hand.betAmount; // notify observers of player loss player.playerLoses(hand, hand.betAmount, player.bankroll); playerLoses(player, hand, hand.betAmount, player.bankroll); } else { // dealer hand equals player hand, notify observers of push player.playerPush(hand, player.bankroll); playerPush(player, hand, player.bankroll); } } } } }
protected void drawPlayers() { // dealer up card is first card in hand Card dealerCard = dealerHand.cards.get(0); for (Player player : players) { ArrayList<PlayerHand> playerBets = player.bets; for (int betNum = 0; betNum < playerBets.size(); ++betNum) { PlayerHand currHand = playerBets.get(betNum); if (currHand.finished) { // player already dealt winnings for blackjack continue; } while (true) { // get player action PlayerStrategyAction action = player.strategy.getAction(currHand, dealerCard); if (action == PlayerStrategyAction.STAND) { // set hand as finished currHand.finished = true; // notify observers that player stands player.playerStands(currHand); playerStands(player, currHand); break; } else if (action == PlayerStrategyAction.HIT) { // add next card to hand Card dealtCard = shoe.getNextCard(); currHand.add(dealtCard); // notify players of dealt card notifyDealt(dealtCard); // notify observers of dealt card player.playerDraws(dealtCard, currHand); playerDraws(player, dealtCard, currHand); if (currHand.isBusted()) { // player busted, set hand as finished currHand.finished = true; // deduct from bankroll player.bankroll -= currHand.betAmount; // notify observers that player busted player.playerBusts(currHand, currHand.betAmount, player.bankroll); playerBusts(player, currHand, currHand.betAmount, player.bankroll); break; } else if (currHand.beenSplit) { if (currHand.isBlackjack()) { currHand.finished = true; // player hand is blackjack, add to bankroll int amountWon = (int) (1.5 * currHand.betAmount); player.bankroll += amountWon; // notify observers of player blackjack player.playerBlackjack(currHand, amountWon, player.bankroll); playerBlackjack(player, currHand, amountWon, player.bankroll); break; } else if (currHand.cards.get(0).isAce()) { if (currHand.isPair()) { // allow resplitting of aces action = player.strategy.getAction(currHand, dealerCard); if (action == PlayerStrategyAction.SPLIT) { // notify observers that player splits player.playerSplits(currHand); playerSplits(player, currHand); // make new bet with split card PlayerHand newHand = currHand.makeSplit(); playerBets.add(newHand); continue; } } // player must now stand currHand.finished = true; // notify observers that player stands player.playerStands(currHand); playerStands(player, currHand); break; } } } else if (action == PlayerStrategyAction.DOUBLE_DOWN) { if (currHand.cards.size() != 2) { // cannot double down if more than two cards, set hand as finished currHand.finished = true; // notify observers that player stands player.playerStands(currHand); playerStands(player, currHand); break; } // double bet currHand.betAmount *= 2; // add next card to hand Card dealtCard = shoe.getNextCard(); currHand.add(dealtCard); // set hand as finished currHand.finished = true; // notify players of dealt card notifyDealt(dealtCard); // notify observers of dealt card player.playerDoublesDown(dealtCard, currHand); playerDoublesDown(player, dealtCard, currHand); if (currHand.isBusted()) { // deduct from bankroll player.bankroll -= currHand.betAmount; // notify observers that player busted player.playerBusts(currHand, currHand.betAmount, player.bankroll); playerBusts(player, currHand, currHand.betAmount, player.bankroll); } break; } else if (action == PlayerStrategyAction.SPLIT) { if (!currHand.isPair()) { // cannot split if hand is not a pair, set hand as finished currHand.finished = true; // notify observers that player stands player.playerStands(currHand); playerStands(player, currHand); break; } // notify observers that player splits player.playerSplits(currHand); playerSplits(player, currHand); // make new bet with split card PlayerHand newHand = currHand.makeSplit(); playerBets.add(newHand); } } } } }
/* * Deal two cards to the house. */ protected boolean dealHouse() { // deal up card and down card for dealer Card upCard = shoe.getNextCard(); Card downCard = shoe.getNextCard(); dealerHand = new Hand(upCard, downCard); // notify strategies of dealer up card notifyDealt(upCard); // notify observers of dealer up card dealerDealt(upCard); if (upCard.isAce()) { // dealer showing ace, offer players insurance for (Player player : players) { if (player.bets.isEmpty()) { // player did not place bet continue; } // player has only one bet, get insurance on it PlayerHand firstBet = player.bets.get(0); int insuranceBet = player.strategy.getInsuranceBet(firstBet, firstBet.betAmount); final int halfBet = firstBet.betAmount / 2; if (insuranceBet > halfBet) { insuranceBet = halfBet; } else if (insuranceBet < 0) { insuranceBet = 0; } // set insurance amount and notify observers player.insuranceBet = insuranceBet; player.playerInsures(insuranceBet, player.bankroll); playerInsures(player, insuranceBet, player.bankroll); } } if ((upCard.isAce() || (upCard.getValue() == 10)) && (dealerHand.getHighValidValue() == 21)) { // dealer has blackjack, notify strategies of down card notifyDealt(downCard); // notify observers of blackjack on dealer dealerBlackjack(dealerHand); for (Player player : players) { if (player.bets.isEmpty()) { // player did not place bet continue; } if (player.insuranceBet > 0) { // player wins on insurance int amountWon = 2 * player.insuranceBet; player.bankroll += amountWon; player.insuranceBet = 0; // notify observers of win on insurance player.playerWinsInsurance(amountWon, player.bankroll); playerWinsInsurance(player, amountWon, player.bankroll); } // player has only one bet PlayerHand firstBet = player.bets.get(0); if (firstBet.getHighValidValue() < 21) { // player hand is not blackjack, deduct from bankroll player.bankroll -= firstBet.betAmount; // notify observers of loss to dealer player.playerLoses(firstBet, firstBet.betAmount, player.bankroll); playerLoses(player, firstBet, firstBet.betAmount, player.bankroll); } else { // notify observers of push with dealer player.playerPush(firstBet, player.bankroll); playerPush(player, firstBet, player.bankroll); } } // do not continue drawing players and drawing house return false; } else { for (Player player : players) { if (player.bets.isEmpty()) { // player did not place bet continue; } if (player.insuranceBet > 0) { // player loses on insurance int amountLost = player.insuranceBet; player.bankroll -= amountLost; player.insuranceBet = 0; // notify observers of loss on insurance player.playerLosesInsurance(amountLost, player.bankroll); playerLosesInsurance(player, amountLost, player.bankroll); } // player has only one bet PlayerHand firstBet = player.bets.get(0); if (firstBet.getHighValidValue() == 21) { firstBet.finished = true; // player hand is blackjack, add to bankroll int amountWon = (int) (1.5 * firstBet.betAmount); player.bankroll += amountWon; // notify observers of player blackjack player.playerBlackjack(firstBet, amountWon, player.bankroll); playerBlackjack(player, firstBet, amountWon, player.bankroll); } } // continue drawing players and drawing house return true; } }