Esempio n. 1
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);
          }
        }
      }
    }
  }
Esempio n. 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;
    }
  }