コード例 #1
0
ファイル: Table.java プロジェクト: mgp/blackjack-analyst
  /*
   * 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);
    }
  }
コード例 #2
0
ファイル: Table.java プロジェクト: mgp/blackjack-analyst
  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);
          }
        }
      }
    }
  }