Ejemplo n.º 1
0
 public void takeCardOutOfDeck(Card a) {
   for (int i = 0; i < cards.size(); i++)
     if (a.equals(cards.get(i))) {
       cards.remove(i);
       return;
     }
 }
Ejemplo n.º 2
0
  /**
   * Main class that test the functionality of methods in Card and BinSet class.
   *
   * @param args
   */
  public static void main(java.lang.String[] args) {

    Card one = new Card(Ranks.FIVE, "SPADES");
    Card two = new Card(Ranks.NINE, "HEARTS");
    Card three = new Card(Ranks.KING, "DIAMONDS");
    Card four = new Card(Ranks.ACE, "DIAMONDS");
    Card five = new Card(Ranks.ACE, "CLUBS");
    Card six = new Card(Ranks.FIVE, "SPADES");

    System.out.println("Five of Spades equals Nine of Hearts:");
    System.out.println(one.equals(two));
    System.out.println();
    System.out.println("Five of Spades equals King of Diamonds:");
    System.out.println(one.equals(three));
    System.out.println();
    System.out.println("Five of Spades equals Ace of Diamonds:");
    System.out.println(one.equals(four));
    System.out.println();
    System.out.println("Five of Spades equals Ace of Clubs:");
    System.out.println(one.equals(five));
    System.out.println();
    System.out.println("Five of Spades equals Five of Spades:");
    System.out.println(one.equals(six));
    System.out.println();

    System.out.println("___________________________________________________");
    System.out.println("Value is greater than 0 when 1 is compared to 2.");
    System.out.println(one.compareTo(two));
    System.out.println();
    System.out.println("Value is greater than 0 when 1 is compared to 3.");
    System.out.println(one.compareTo(three));
    System.out.println();
    System.out.println("Value is greater than 0 when 1 is compared to 4.");
    System.out.println(one.compareTo(four));
    System.out.println();
    System.out.println("Value is greater than 0 when 1 is compared to 5.");
    System.out.println(one.compareTo(five));
    System.out.println();
    System.out.println("Value is equal to 0 when 1 is compared to 6.");
    System.out.println(one.compareTo(six));
    System.out.println();

    System.out.println("___________________________________________________");
    System.out.println("Card1 as a String:");
    System.out.println(one.toString());
    System.out.println();
    System.out.println("Card2 as a String:");
    System.out.println(two.toString());
    System.out.println();
    System.out.println("Card3 as a String:");
    System.out.println(three.toString());
    System.out.println();
    System.out.println("Card4 as a String:");
    System.out.println(four.toString());
    System.out.println();
    System.out.println("Card5 as a String:");
    System.out.println(five.toString());
    System.out.println();
    System.out.println("Card6 as a String:");
    System.out.println(six.toString());
    System.out.println();
    System.out.println("___________________________________________________");

    System.out.println("Create List1.");
    BinSet<String> List1 = new BinSet<String>();

    System.out.println();
    System.out.println("Add A to List1:");
    System.out.println(List1.add("A"));
    System.out.println();
    System.out.println("Add B to List1:");
    System.out.println(List1.add("B"));
    System.out.println();
    System.out.println("Add A to List1:");
    System.out.println(List1.add("A"));
    System.out.println();
    System.out.println("Print out List1:");
    System.out.println(List1.toString());
    System.out.println();
    System.out.println("___________________________________________________");

    System.out.println("Create List2.");
    Collection<String> List2 = new ArrayList<String>();

    System.out.println();
    System.out.println("Add C to List2:");
    List2.add("C");
    System.out.println("Add D to List2:");
    List2.add("D");
    System.out.println("Add E to List2:");
    List2.add("E");
    System.out.println();
    System.out.println("Are we able to add List2 to List1?");
    System.out.println(List1.addAll(List2));
    System.out.println();

    System.out.println("Clear List2.");
    List2.clear();
    System.out.println();

    System.out.println("Add A to List2.");
    List2.add("A");
    System.out.println("Add C to List2.");
    List2.add("C");
    System.out.println("Add D to List2.");
    List2.add("D");
    System.out.println("Add E to List2.");
    List2.add("E");
    System.out.println();

    System.out.println("Can I add List2 to List1?");
    System.out.println(List1.addAll(List2));
    System.out.println();

    System.out.println("Display List1:");
    System.out.println(List1.toString());
    System.out.println();

    System.out.println("Is List2 Empty?");
    System.out.println(List2.isEmpty());
    System.out.println();

    System.out.println("Clear List2.");
    List2.clear();
    System.out.println();

    System.out.println("Is List2 Empty?");
    System.out.println(List2.isEmpty());
    System.out.println();

    System.out.println("Can I add List1 to List2?");
    System.out.println(List2.addAll(List1));
    System.out.println();
    System.out.println("Does List1 Contain all of List2?");
    System.out.println(List1.containsAll(List2));
    System.out.println();
    System.out.println("Does List2 Contain Z?");
    System.out.println(List2.contains("Z"));
    System.out.println();

    System.out.println("Does List2 Contain A?");
    System.out.println(List2.contains("A"));
    System.out.println();

    System.out.println("Can I remove Z from List2?");
    System.out.println(List2.remove("Z"));
    System.out.println();
    System.out.println("Can I remove A from List2?");
    System.out.println(List2.remove("A"));
    System.out.println();
    System.out.println("Display List2:");
    System.out.println(List2.toString());
    System.out.println();

    System.out.println("Display List1:");
    System.out.println(List1.toString());
    System.out.println();

    Object[] array = List1.toArray();
    System.out.println("Display the array created from List1:");
    System.out.println(Arrays.toString(array));
    System.out.println();
  }
Ejemplo n.º 3
0
  public void playCard() {
    boolean fujaba__Success = false;
    Player player2 = null;
    Iterator fujaba__IterPlayer2ToCard2 = null;
    Card card2 = null;

    // story pattern successor
    try {
      fujaba__Success = false;

      // check object card1 is really bound
      JavaSDM.ensure(card1 != null);
      // check object cardsInPlay is really bound
      JavaSDM.ensure(cardsInPlay != null);
      // check object player1 is really bound
      JavaSDM.ensure(player1 != null);
      // check object turn is really bound
      JavaSDM.ensure(turn != null);
      // check link contain from card1 to cardsInPlay
      JavaSDM.ensure(cardsInPlay.equals(card1.getCardsInPlay()));

      // check link has from turn to player1
      JavaSDM.ensure(player1.equals(turn.getPlayer()));

      // search to-one link before from player1 to player2
      player2 = player1.getNext();

      // check object player2 is really bound
      JavaSDM.ensure(player2 != null);

      // check isomorphic binding between objects player2 and player1
      JavaSDM.ensure(!player2.equals(player1));

      // check link has from card1 to player1
      JavaSDM.ensure(card1.hasInPlayer(player1));

      // attribute condition suit == J
      JavaSDM.ensure(JavaSDM.stringCompare((String) card1.getSuit(), J) == 0);

      // attribute condition suit == card.getSuit()
      JavaSDM.ensure(JavaSDM.stringCompare((String) turn.getSuit(), card.getSuit()) == 0);

      // destroy link has from card1 to player1
      card1.removeFromPlayer(player1);
      fujaba__Success = true;
    } catch (JavaSDMException fujaba__InternalException) {
      fujaba__Success = false;
    }

    // story pattern Successor of successor
    try {
      fujaba__Success = false;

      // check object card1 is really bound
      JavaSDM.ensure(card1 != null);
      // check object cardsInPlay is really bound
      JavaSDM.ensure(cardsInPlay != null);
      // check object player1 is really bound
      JavaSDM.ensure(player1 != null);
      // check object player2 is really bound
      JavaSDM.ensure(player2 != null);
      // check object turn is really bound
      JavaSDM.ensure(turn != null);
      // check isomorphic binding between objects player2 and player1
      JavaSDM.ensure(!player2.equals(player1));

      // check link before from player2 to player1
      JavaSDM.ensure(player1.equals(player2.getFirst()));

      // check link contain from card1 to cardsInPlay
      JavaSDM.ensure(cardsInPlay.equals(card1.getCardsInPlay()));

      // check link has from turn to player1
      JavaSDM.ensure(player1.equals(turn.getPlayer()));

      // iterate to-many link has from player2 to card2
      fujaba__Success = false;
      fujaba__IterPlayer2ToCard2 = player2.iteratorOfCard();

      while (!(fujaba__Success) && fujaba__IterPlayer2ToCard2.hasNext()) {
        try {
          card2 = (Card) fujaba__IterPlayer2ToCard2.next();

          // check object card2 is really bound
          JavaSDM.ensure(card2 != null);
          // check isomorphic binding between objects card2 and card1
          JavaSDM.ensure(!card2.equals(card1));

          fujaba__Success = true;
        } catch (JavaSDMException fujaba__InternalException) {
          fujaba__Success = false;
        }
      }
      JavaSDM.ensure(fujaba__Success);
      // attribute condition suit != card.getSuit()
      JavaSDM.ensure(JavaSDM.stringCompare((String) turn.getSuit(), card.getSuit()) != 0);

      // attribute condition suit == J
      JavaSDM.ensure(JavaSDM.stringCompare((String) card1.getSuit(), J) == 0);

      // destroy link has from turn to player1
      turn.setPlayer(null);
      // create link has from turn to player2
      turn.setPlayer(player2);

      // create link contain from card2 to cardsInPlay
      card2.setCardsInPlay(cardsInPlay);

      fujaba__Success = true;
    } catch (JavaSDMException fujaba__InternalException) {
      fujaba__Success = false;
    }

    return;
  }
Ejemplo n.º 4
-17
 public void doMove(Move move)
     throws AgainstTheRulesException, NoFreeCellException, InvalidCardException {
   Card theCard = null;
   try {
     theCard = stacks[move.getFrom()].pop();
     stacks[move.getTo()].push(theCard);
   } catch (AgainstTheRulesException e1) {
     if (!theCard.equals(null)) {
       // If we managed to pop the card, deal it back.
       stacks[move.getFrom()].deal(theCard);
     }
     // TODO: If this move is already part of a movemacro, then we don't want to keep trying to
     // solve it.
     Move newMove = solveMove(move);
     newMove.execute();
     addToHistory(move);
   }
   this.repaint();
 }