Example #1
0
  /*
   * 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);
          }
        }
      }
    }
  }
Example #2
0
  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);
          }
        }
      }
    }
  }