/**
   * Return's opponent's bet, as a convenient method. If the opponent folds, checks, or calls, this
   * returns 0. Otherwise, it will return the amount that the player bet.
   *
   * @param me
   * @return
   */
  public int getOpponentBet(AbstractPlayer me) {
    AbstractPlayer opponent = getOpponent(me);

    PlayerActivityRecord a = playerActivityRecords.get(playerActivityRecords.size() - 1);
    PlayerAction playerAction = a.getPlayerAction();

    if (opponent.getName().equals(a.getName())
        && (playerAction.getActionType() == ActionType.BET_OR_RAISE)) {
      return playerAction.getBet();
    } else {
      return 0;
    }
  }
  public static void main(String args[]) throws IOException, Exception {
    PhysicalGameState pgs = MapGenerator.basesWorkers8x8Obstacle();
    GameState gs = new GameState(pgs, UnitTypeTable.utt);
    int MAXCYCLES = 5000;
    boolean gameover = false;

    AI ai1 = new RandomBiasedAI();
    AI ai2 = new WorkerRush(UnitTypeTable.utt, new BFSPathFinding());

    Trace trace = new Trace();
    TraceEntry te = new TraceEntry(gs.getPhysicalGameState().clone(), gs.getTime());
    trace.addEntry(te);

    do {
      PlayerAction pa1 = ai1.getAction(0, gs);
      PlayerAction pa2 = ai2.getAction(1, gs);
      gs.issueSafe(pa1);
      gs.issueSafe(pa2);

      if (!pa1.isEmpty() || !pa2.isEmpty()) {
        te = new TraceEntry(gs.getPhysicalGameState().clone(), gs.getTime());
        te.addPlayerAction(pa1);
        te.addPlayerAction(pa2);
        trace.addEntry(te);
      }

      // simulate:
      gameover = gs.cycle();
    } while (!gameover && gs.getTime() < MAXCYCLES);

    te = new TraceEntry(gs.getPhysicalGameState().clone(), gs.getTime());
    trace.addEntry(te);

    XMLWriter xml = new XMLWriter(new FileWriter("trace.xml"));
    trace.toxml(xml);
    xml.flush();
  }
Example #3
0
  private void playSeat(int seatIndex) {
    Seat seat = this.seats.get(seatIndex);

    for (int i = 0; i < seat.hands.size(); i++) {
      CardsAndBet hand = seat.hands.get(i);

      if (i == 0 && HandUtilities.blackjack(hand.cards())) {
        hand.setResult(Result.PLAYER_BLACKJACK);
      } else {
        List<PlayerAction> allowedActions =
            new ArrayList<PlayerAction>(Arrays.asList(PlayerAction.values()));

        if (hand.cards().size() == 1) // hand is result of splitting
        {
          if (hand.cards().get(0).value() == Value.ACE) // hand is result of splitting aces
          {
            allowedActions.remove(PlayerAction.HIT);
            allowedActions.remove(PlayerAction.DOUBLE);
          }

          allowedActions.remove(PlayerAction.SURRENDER);
          hand.dealCardTo(dealFaceUpCard());
        }

        if (i != 0 || hand.cards().size() != 2) allowedActions.remove(PlayerAction.SURRENDER);

        if (hand.cards().size() != 2
            || seat.hands.size() == 7
            || // 7 hands means 4 valid hands (rest are sources of splits)
            (hand.cards().get(0).value().numericValue()
                != hand.cards().get(1).value().numericValue()))
          allowedActions.remove(PlayerAction.SPLIT);

        // TODO: player should not be asked to make a decision if split hand is 21
        // TODO: why does this only happen in the observer, not the basic player when just playing?

        if (HandUtilities.value(hand.cards()) == HandUtilities.MAX_VALUE) allowedActions.clear();

        while (allowedActions.size() >= 2) // player still has at least 1 decision to make
        {
          ArrayList<PlayerAction> allowedActionsCopy = new ArrayList<PlayerAction>(allowedActions);

          PlayerAction action =
              seat.player.act(hand.cards(), this.dealerHand.get(0), allowedActionsCopy);

          if (!allowedActions.contains(action))
            throw new IllegalActionException(
                seat.player.id() + " attempted to " + action + " when not allowed.");

          for (BlackjackObserver observer : this.observers)
            observer.decision(
                seatIndex, hand.cards(), this.dealerHand.get(0), allowedActionsCopy, action);

          allowedActions.clear();

          switch (action) {
            case HIT:
              hand.dealCardTo(dealFaceUpCard());

              if (HandUtilities.value(hand.cards()) < HandUtilities.MAX_VALUE) {
                allowedActions.add(PlayerAction.HIT);
                allowedActions.add(PlayerAction.STAND);
              }

              break;

            case DOUBLE:
              hand.dealCardTo(dealFaceUpCard());
              hand.doubleBet();
              break;

            case STAND:
              break;

            case SURRENDER:
              hand.setResult(Result.SURRENDER);
              break;

            case SPLIT:
              List<Card> cards = hand.cards();
              hand.setResult(Result.SOURCE_OF_SPLIT);

              seat.hands.add(i + 1, new CardsAndBet(hand.bet()));
              seat.hands.get(i + 1).dealCardTo(cards.get(0));

              seat.hands.add(i + 2, new CardsAndBet(hand.bet()));
              seat.hands.get(i + 2).dealCardTo(cards.get(1));

              break;
          }
        }

        if (!hand.isDone() && HandUtilities.bust(hand.cards())) hand.setResult(Result.DEALER_WIN);
      }
    }
  }