public static void main(final String args[]) {
    final Scanner in = new Scanner(System.in);

    final Queue<Card> deckPlayer1 = buildDeck(in);
    final Queue<Card> deckPlayer2 = buildDeck(in);

    final Queue<Card> cardsToWinPlayer1 = new LinkedList<>();
    final Queue<Card> cardsToWinPlayer2 = new LinkedList<>();
    int numberOfRounds = 0;
    while (gameContinue(deckPlayer1, deckPlayer2)) {
      final Card cardPlayer1 = deckPlayer1.poll();
      final Card cardPlayer2 = deckPlayer2.poll();
      cardsToWinPlayer1.add(cardPlayer1);
      cardsToWinPlayer2.add(cardPlayer2);

      final int result = cardPlayer1.compareTo(cardPlayer2);
      if (isDeuce(result)) {
        try {
          cardsToWinPlayer1.addAll(popThreeCards(deckPlayer1));
          cardsToWinPlayer2.addAll(popThreeCards(deckPlayer2));
        } catch (IllegalStateException e) {
          System.out.println("PAT");
          return;
        }
      } else {
        if (result == PLAYER_ONE) {
          playerWin(deckPlayer1, cardsToWinPlayer1, cardsToWinPlayer2);
        } else if (result == PLAYER_TWO) {
          playerWin(deckPlayer2, cardsToWinPlayer1, cardsToWinPlayer2);
        }
        ++numberOfRounds;
      }
    }

    displayTheWinner(deckPlayer1, numberOfRounds);
  }
Esempio 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();
  }