/**
   * determines the set of cards that are possible to play. it depends on the cards played, the set
   * of cards available and the current trump suit. <br>
   * <br>
   * if there were already one or more cards played in one punch, and the player has any cards in
   * the first played card's color, he has to play one of these. else the player may play any cards
   * in his hand.
   *
   * @param played the set of cards played in this round.
   * @param hand the available cards where the calculation is applied to.
   * @param trump this parameter is currently not influencing the result, but there may be game
   *     variations where the possible cards set in a players hand may depend on the current trump
   *     suit in the future.
   * @return a set of cards that are legal to play in the current punches' state.
   */
  public static List<Card> calcPossibleCards(List<Card> played, List<Card> hand, Card.Color trump) {
    if (hand == null || hand.isEmpty() || trump == null)
      throw new RuntimeException("hand=" + hand + ", trump=" + trump);
    if (played == null || played.isEmpty() || hand.size() == 1) return hand; // all possible
    assert disjoint(played, hand) : "hand=" + hand + ", played=" + played;

    Card first = played.get(0);

    if (first.equals(Card.WELI)) return hand; // all possible

    Card.Color firstColor = first.getColor();
    List<Card> possible = null;

    for (Card c : hand)
      if (c.getColor() == firstColor || c.equals(Card.WELI)) {
        if (possible == null) possible = new Card.CardList();
        possible.add(c);
      }

    if (possible == null || (possible.size() == 1 && possible.contains(Card.WELI))) {
      return hand; // no cards of the 1st card's color: may play any
    }

    return possible;
  }
  /**
   * Verifies that three given Cards constitute a set
   *
   * @param card1 first Card
   * @param card2 second Card
   * @param card3 third Card
   * @return true if the three Cards constitute a set false if the three Cards do not constitute a
   *     set
   */
  public boolean checkForSet(Card card1, Card card2, Card card3) {
    // SET isSet to true
    boolean isSet = true;

    // IF card1 and card2 and card3 are null
    if (card1 == null || card2 == null || card3 == null) {
      // SET isSet to false
      isSet = false;
    }
    // ELSE IF card1 == card2 and card2 == card3 and card1 == card3
    else if (card1.equals(card2) && card2.equals(card3) && card1.equals(card3)) {
      // SET isSet to false
      isSet = false;
    }
    // ELSE
    else {
      // SET isSet to the AND of the return value of isFeatureSet with
      // each cards color
      isSet &= isFeatureSet(card1.getColor(), card2.getColor(), card3.getColor());
      // SET isSet to the AND of the return value of isFeatureSet with
      // each cards shape
      isSet &= isFeatureSet(card1.getShape(), card2.getShape(), card3.getShape());
      // SET isSet to the AND of the return value of isFeatureSet with
      // each cards fill
      isSet &= isFeatureSet(card1.getFill(), card2.getFill(), card3.getFill());
      // SET isSet to the AND of the return value of isFeatureSet with
      // each cards number
      isSet &= isFeatureSet(card1.getNumber(), card2.getNumber(), card3.getNumber());
    }
    // END IF

    // RETURN isSet
    return isSet;
  }
Beispiel #3
0
  public CardPanel(Card card) {
    // Prueba Borrar
    String colorCard;
    this.card = card;
    String cardColor = card.getColor(), cardType = card.getType(), imagePath = null;

    if (cardColor != null) {
      setBackground(getColor(cardColor));
    }

    if (cardType.equals("Number")) {
      imagePath = card.getNumber();
      System.out.print("Numero " + imagePath);

    } else {
      imagePath = card.getType();
    }

    setSize(150, 200);
    try {
      image = ImageIO.read(new File(imagePath + ".png"));
    } catch (IOException e) {
      System.out.println("Error IO " + imagePath);
      System.out.println(" largo " + cardType.length());
    }
  }
 private static boolean contains(Collection<Card> coll, Card.Color c, Card.Value v) {
   for (Iterator<Card> itr = coll.iterator(); itr.hasNext(); ) {
     Card card = itr.next();
     if (card.getColor() == c && card.getValue() == v) return true;
   }
   return false;
 }
  /**
   * tests if a card would be the highest card after adding it to the current punch. the player of
   * this card will "own" the punch until an other player punches again.
   *
   * @param played the list of cards played so far (null will be treated as an empty card stack
   *     where every card c would be the highest. the list will not be modified)
   * @param card the card where to test if it would be the highest one in the given stack
   * @param trumpSuit the current trump suit, must not be null
   * @return true if this card would punch the other cards
   */
  public static boolean punches(
      final List<Card> played, final Card card, final Card.Color trumpSuit) {
    if (played == null || played.isEmpty()) return true;

    assert trumpSuit != null;
    assert !played.contains(card);

    //////////////////// special cases: ////////////////////////////
    // only sau in trump suit (and papa, if enabled) punches weli
    final Card.Color cardCol = card.getColor();
    final Card.Value val = card.getValue();

    // nobody can beat papa
    if (card.equals(Card.PAPA) && card.isSpecialCard()) return true;

    int papaIndex = played.indexOf(Card.PAPA);
    if (papaIndex >= 0 && played.get(papaIndex).isSpecialCard()) return false;

    if (played.contains(Card.WELI)) return val == Card.Value.sau && cardCol == trumpSuit;
    if (card.equals(Card.WELI)) return !contains(played, trumpSuit, Card.Value.sau);

    //////////////////// regular comparison: ////////////////////////////
    Card first = played.get(0);
    final Card.Color firstCol = first.getColor();

    if (cardCol != firstCol && cardCol != trumpSuit)
      return false; // color punch: card different than 1st and not trump

    for (Card ithCard : played) {
      Card.Color ithColor = ithCard.getColor();

      // color punches:
      if (cardCol != trumpSuit) {
        if (ithColor == firstCol && cardCol != firstCol)
          return false; // ith has 1st, card neither 1st nor trump
        if (ithColor == trumpSuit) return false; // ith in trump, card not

      } else if (ithColor != trumpSuit)
        continue; // don't need to determine value, this color is higher

      // value punch:
      if (cardCol == ithColor && ithCard.getValue().compareTo(val) > 0)
        return false; // same color, ith higher
    }

    return true;
  }
  public static final Map<Card.Color, Integer> pointsPerColor(Collection<Card> cards) {
    Map<Card.Color, Integer> ppc = new HashMap<Card.Color, Integer>();

    for (Card c : cards) {
      if (c.equals(Card.WELI)) continue;

      Card.Color key = c.getColor();
      Integer sum = ppc.get(key);
      int cardIntVal = c.getValue().intValue();

      ppc.put(key, cardIntVal + (sum != null ? sum : 0));
    }

    return ppc;
  }
  /**
   * maps the count of cards to each different color occuring in the given set of cards. the weli
   * card will be omitted in this calculation, since it has no color.
   *
   * @param cards the cards to create the mapping for
   * @return a mapping from the occuring colors to the number of its cards
   */
  public static final Map<Card.Color, Integer> colorSpread(Collection<Card> cards) {
    Map<Card.Color, Integer> spread = new HashMap<Card.Color, Integer>();

    for (Card c : cards) {
      Card.Color key = c.getColor();

      if (c.equals(Card.WELI)) {
        assert key == null : key;
        continue;
      }

      Integer cnt = spread.get(key);
      spread.put(key, cnt == null ? 1 : (1 + cnt));
    }

    return spread;
  }
  public static Card.Color suggestColor(List<Card> hand) {
    Map<Card.Color, Integer> score = new HashMap<Card.Color, Integer>();

    for (Card c : hand) {
      if (c.equals(Card.WELI)) continue;

      Card.Color color = c.getColor();
      Integer sum = score.get(color);
      int cardValue = c.getValue().intValue();
      score.put(color, sum == null ? cardValue : (sum + cardValue));
    }

    Map.Entry<Card.Color, Integer> best = null, tmp = null;
    Iterator<Map.Entry<Card.Color, Integer>> itr;

    for (itr = pointsPerColor(hand).entrySet().iterator(); itr.hasNext(); ) {
      tmp = itr.next();

      if (best == null || best.getValue().compareTo(tmp.getValue()) < 0) best = tmp;
    }

    return best.getKey();
  }
  private String getReasons(boolean set) {
    String retString = "";

    Card c1 = getInlayCard(new Natural(0));
    Card c2 = getInlayCard(new Natural(1));
    Card c3 = getInlayCard(new Natural(2));

    if (set) {
      if (c1.getColor() == c2.getColor() && c1.getColor() == c3.getColor()) {
        retString = retString + "-All cards are " + c1.getColor() + "\n";
      } else {
        retString = retString + "-All cards have different colors\n";
      }

      if (c1.getFill() == c2.getFill() && c1.getFill() == c3.getFill()) {
        retString = retString + "-All cards have a " + c1.getFill() + " fill\n";
      } else {
        retString = retString + "-All cards have a different fill\n";
      }

      if (c1.getNumber() == c2.getNumber() && c1.getNumber() == c3.getNumber()) {
        retString = retString + "-All cards are of number " + c1.getNumber() + "\n";
      } else {
        retString = retString + "-All cards have different number\n";
      }

      if (c1.getShape() == c2.getShape() && c1.getShape() == c3.getShape()) {
        retString = retString + "-All cards are " + c1.getShape() + " shapes\n";
      } else {
        retString = retString + "-All cards are have different shapes\n";
      }
    } else {
      if (!isFeatureSet(c1.getColor(), c2.getColor(), c3.getColor())) {
        if (c1.getColor() == c2.getColor()) {
          retString =
              retString + "-Card1 and Card2 have same color but Card3 has a different color\n";
        } else if (c1.getColor() == c3.getColor()) {
          retString =
              retString + "-Card1 and Card 3 have same color but Card2 has a different color\n";
        } else if (c2.getColor() == c3.getColor()) {
          retString =
              retString + "-Card2 and Card3 have same color but Card1 has a different color\n";
        }
      }
      if (!isFeatureSet(c1.getFill(), c2.getFill(), c3.getFill())) {
        if (c1.getFill() == c2.getFill()) {
          retString =
              retString + "-Card1 and Card2 have same fill but Card3 has a different fill\n";
        } else if (c1.getFill() == c3.getFill()) {
          retString =
              retString + "-Card1 and Card 3 have same fill but Card2 has a different fill\n";
        } else if (c2.getFill() == c3.getFill()) {
          retString =
              retString + "-Card2 and Card3 have same fill but Card1 has a different fill\n";
        }
      }
      if (!isFeatureSet(c1.getNumber(), c2.getNumber(), c3.getNumber())) {
        if (c1.getNumber() == c2.getNumber()) {
          retString =
              retString + "-Card1 and Card2 have same number but Card3 has a different number\n";
        } else if (c1.getNumber() == c3.getNumber()) {
          retString =
              retString + "-Card1 and Card 3 have same number but Card2 has a different number\n";
        } else if (c2.getNumber() == c3.getNumber()) {
          retString =
              retString + "-Card2 and Card3 have same number but Card1 has a different number\n";
        }
      }
      if (!isFeatureSet(c1.getShape(), c2.getShape(), c3.getShape())) {
        if (c1.getShape() == c2.getShape()) {
          retString =
              retString + "-Card1 and Card2 have same shape but Card3 has a different shape\n";
        } else if (c1.getShape() == c3.getShape()) {
          retString =
              retString + "-Card1 and Card 3 have same shape but Card2 has a different shape\n";
        } else if (c2.getShape() == c3.getShape()) {
          retString =
              retString + "-Card2 and Card3 have same shape but Card1 has a different shape\n";
        }
      }
    }

    return retString;
  }