示例#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 generateRandomStraight() {

    Rank randomRank = Rank.getRandomRank(Rank.TEN);

    ImmutableSortedSet.Builder<Card> builder =
        ImmutableSortedSet.orderedBy(new Card.RankThenSuitComparer());

    builder.add(Card.from(randomRank, Suit.getRandomSuit()));
    builder.add(Card.from(randomRank.addToRank(1), Suit.getRandomSuit()));
    builder.add(Card.from(randomRank.addToRank(2), Suit.getRandomSuit()));
    builder.add(Card.from(randomRank.addToRank(3), Suit.SPADES));
    builder.add(Card.from(randomRank.addToRank(4), Suit.HEARTS));

    return new SortedHand(builder.build());
  }
示例#3
0
  public static void main(String[] args) {

    List<Card> cards = new LinkedList<Card>();
    for (Value v : Value.values()) {
      for (Suit s : Suit.values()) {
        cards.add(new Card(v, s));
      }
    }

    Random rand = new Random();
    Card[] cards1 = new Card[7];
    Card[] cards2 = new Card[7];

    for (int i = 0; i < 7; i++) {
      // Card c = cards.remove(rand.nextInt(cards.size())) ;
      Card c1 = cards.remove(2 + i * 3);
      cards1[i] = c1;
      Card c2 = cards.remove(rand.nextInt(cards.size()));
      cards2[i] = c2;
    }

    System.out.println("------ Card 1 ---------");
    for (Card c : cards1) {
      System.out.println(c.getSuit() + " " + c.getValue());
    }
    System.out.println(Round.getRank(cards1));
    System.out.println("------ Card 2 ---------");
    for (Card c : cards2) {
      System.out.println(c.getSuit() + " " + c.getValue());
    }
    System.out.println(Round.getRank(cards2));
  }
示例#4
0
  public static void main(String[] args) {
    Collection<Suit> suits = Arrays.asList(Suit.values());
    Collection<Rank> ranks = Arrays.asList(Rank.values());

    List<Card> deck = new ArrayList<Card>();
    for (Iterator<Suit> i = suits.iterator(); i.hasNext(); )
      for (Iterator<Rank> j = ranks.iterator(); j.hasNext(); )
        deck.add(new Card(i.next(), j.next()));

    // Preferred idiom for nested iteration on collections and arrays
    //        for (Suit suit : suits)
    //            for (Rank rank : ranks)
    //                deck.add(new Card(suit, rank));
  }
示例#5
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());
  }
示例#6
0
文件: Card.java 项目: bartok362/OST
 static {
   for (Suit suit : Suit.values())
     for (Face face : Face.values()) theDeck.add(new Card(face, suit));
 }
 Card(final String value) {
   final int suitIndex = value.length() - 1;
   this.value = value.substring(0, suitIndex);
   this.suit = Suit.valueById(value.substring(suitIndex));
 }