Beispiel #1
0
 /**
  * Compare this card with the argument.
  *
  * @param otherCard the other card to compare to this
  * @return true if the rank, suit, and point value of this card are equal to those of the
  *     argument; false otherwise.
  */
 public boolean matches(Card otherCard) {
   if (this.rank().equals(otherCard.rank())
       && (this.suit().equals(otherCard.suit()))
       && (this.pointValue() == otherCard.pointValue())) {
     return true;
   } else {
     return false;
   }
 }
Beispiel #2
0
 public void testInstance() {
   for (Card card : Card.values()) assertEquals(card, Card.instance(card.rank(), card.suit()));
   for (Rank rank : Rank.values())
     for (Suit suit : Suit.values()) assertNotNull(Card.instance(rank, suit));
   System.out.println("************************************");
   for (Rank rank : Rank.values())
     if (rank.ordinal() <= 2)
       for (Suit suit : Suit.values())
         System.out.println(rank + " " + suit + " " + Card.instance(rank, suit));
   System.out.println("************************************");
 }
Beispiel #3
0
  public static void main(String[] args) {

    System.out.println("Witaj w grze Kareta");
    Deck deck = new Deck();
    DiscardPile discardPile = new DiscardPile();
    Card hCard;
    Card cCard;

    while (true) {

      hCard = deck.deal();
      cCard = deck.deal();
      if (hCard.rank() != cCard.rank()) break;

      deck.putBack(hCard);
      deck.putBack(cCard);
      deck.shuffle();
    }

    int curPlayer = HUMAN;

    if (cCard.rank().ordinal() > hCard.rank().ordinal()) curPlayer = COMPUTER;

    deck.putBack(hCard);
    hCard = null;
    deck.putBack(cCard);
    cCard = null;
    Card[] hCards = new Card[4];
    Card[] cCards = new Card[4];
    if (curPlayer == HUMAN)
      for (int i = 0; i < 4; i++) {

        cCards[i] = deck.deal();
        hCards[i] = deck.deal();
      }
    else
      for (int i = 0; i < 4; i++) {
        hCards[i] = deck.deal();
        cCards[i] = deck.deal();
      }
    while (true) {
      if (curPlayer == HUMAN) {

        showHeldCards(hCards);
        int choice = 0;
        while (choice < 'A' || choice > 'D') {

          choice = prompt("Której karty chcesz siê pozbyæ ( A, B, " + "C, D) ?");

          switch (choice) {
            case 'a':
              choice = 'A';
              break;
            case 'b':
              choice = 'B';
              break;
            case 'c':
              choice = 'C';
              break;
            case 'd':
              choice = 'D';
          }
        }
        discardPile.setTopCard(hCards[choice - 'A']);
        hCards[choice - 'A'] = deck.deal();
        if (isFourOfAKind(hCards)) {
          System.out.println();
          System.out.println("Cz³oweik wygrywa");
          System.out.println();
          putDown("Karty Cz³owieka:", hCards);
          System.out.println();
          putDown("Karty Komputera:", cCards);
          return;
        }

        curPlayer = COMPUTER;
      } else {

        int choice = leastDesirableCard(cCards);
        discardPile.setTopCard(cCards[choice]);
        cCards[choice] = deck.deal();
        if (isFourOfAKind(cCards)) {

          System.out.println();
          System.out.println("Komputer Wygrywa");
          System.out.println();
          putDown("Karty Komputera", cCards);
          return;
        }

        curPlayer = HUMAN;
      }

      if (deck.isEmpty()) {
        while (discardPile.topCard() != null) deck.putBack(discardPile.getTopCard());
        deck.shuffle();
      }
    }
  }