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