示例#1
0
  private ArrayList<Card> pickNRandomDifferentCards(int nCards) {
    if (nCards > 5) {
      throw new IllegalArgumentException("Don't yet know how to generate more than 5 random cards");
    }

    TreeSet<Rank> ranks = new TreeSet<Rank>();

    ArrayList<Card> cards = new ArrayList<Card>();

    while (ranks.size() != nCards) {
      Rank randomRank = Rank.getRandomRank();

      if (ranks.contains(randomRank)) {
        continue;
      }
      ranks.add(randomRank);

      if (ranks.size() == 5) {
        Iterator<Rank> iRank = ranks.iterator();
        boolean straight = true;
        Rank previousRank = iRank.next();
        while (iRank.hasNext()) {
          Rank nextRank = iRank.next();
          // annoying edge case of 2-3-4-5-A

          if (previousRank.addToRank(1) != nextRank) {
            if (!(previousRank == Rank.FIVE && nextRank == Rank.ACE)) {
              straight = false;
              break;
            }
          }
          previousRank = nextRank;
        }
        // make sure we don't accidentally crate a straight
        if (straight) {
          ranks.remove(randomRank);
          continue;
        }
      }
    }

    TreeSet<Suit> suits = new TreeSet<Suit>();

    for (Rank randomRank : ranks) {
      Suit randomSuit = Suit.getRandomSuit();
      // make sure that there are more than one suit
      while (suits.size() == 1 && suits.contains(randomSuit)) {
        randomSuit = Suit.getRandomSuit();
      }
      suits.add(randomSuit);

      Card card = Card.from(randomRank, randomSuit);
      cards.add(card);
    }

    return cards;
  }
示例#2
0
  private IHand generateRandomHighCard(StandardDeckFactory deckFactory) {

    ArrayList<Card> cards = pickNRandomDifferentCards(5);

    ImmutableSortedSet.Builder<Card> builder =
        ImmutableSortedSet.orderedBy(new Card.RankThenSuitComparer());
    builder.add(cards.get(0));
    builder.add(cards.get(1));
    builder.add(cards.get(2));
    builder.add(cards.get(3));
    builder.add(cards.get(4));

    return new SortedHand(builder.build());
  }
示例#3
0
  private IHand generateRandomPair(StandardDeckFactory deckFactory) {
    SortedHand randomHand = new SortedHand();

    ArrayList<Card> cards = pickNRandomDifferentCards(4);

    IDeck deck = deckFactory.build();
    ImmutableSortedSet.Builder<Card> builder =
        ImmutableSortedSet.orderedBy(new Card.RankThenSuitComparer());

    builder.add(deck.pickRandom(cards.get(0).rank()));
    builder.add(deck.pickRandom(cards.get(0).rank()));
    builder.add(cards.get(1));
    builder.add(cards.get(2));
    builder.add(cards.get(3));

    return new SortedHand(builder.build());
  }
示例#4
0
  private IHand generateRandomFlush(StandardDeckFactory deckFactory) {
    ArrayList<Card> cards = pickNRandomDifferentCards(5);

    Suit randomSuit = Suit.getRandomSuit();

    IDeck deck = deckFactory.build();
    ImmutableSortedSet.Builder<Card> builder =
        ImmutableSortedSet.orderedBy(new Card.RankThenSuitComparer());

    builder.add(Card.from(cards.get(0).rank(), randomSuit));
    builder.add(Card.from(cards.get(1).rank(), randomSuit));
    builder.add(Card.from(cards.get(2).rank(), randomSuit));
    builder.add(Card.from(cards.get(3).rank(), randomSuit));
    builder.add(Card.from(cards.get(4).rank(), randomSuit));

    return new SortedHand(builder.build());
  }