// SetDeck.java // NAME, ANDREW ID, SECTION import java.util.*; public class SetDeck implements Iterable { // Instance variables // YOU CHOOSE THESE // Construct a new deck of shuffled Set cards public SetDeck() { // YOU WRITE THIS } // Construct a new deck of Set cards that may or may not be shuffled, // according to the "shuffled" parameter. In an unshuffled deck, // the cards must be in order from 0 to 80 according to the result of // getCardIndex from the SetCard class. See the test method for details. // Hint: you will probably want to store the deck in an instance variable // that holds an array of 81 instances of SetCard. public SetDeck(boolean shuffled) { // YOU WRITE THIS } // Shuffle the deck using the built-in "shuffle" method in the // Collections class. To do this, we have to convert the array // into a "List". Fortunately, the Arrays class provides the "asList" // method, that does exactly this for us (and we are dealing with // an array of SetCard instances rather than primitives, so that // is not a problem, either). public void shuffle() { // YOU WRITE THIS } // Return true if there is another card to deal from the deck, // and false otherwise. public boolean hasNextCard() { // YOU WRITE THIS return false; } // Return the next SetCard to be dealt from the deck, and update // whatever instance variables are required so that after this call // there is one fewer cards left to deal from the deck. // You do not have to deal with the case where nextCard is called // when hasNextCard would return false (and so the deck is empty). public SetCard nextCard() { // YOU WRITE THIS return null; } // Return the number of cards that can still be dealt from // the deck. That is, the number of times nextCard can be called // until hasNextCard would return false. public int getCardsLeft() { // YOU WRITE THIS return 42; } // Return an instance of an Iterator that iterates over // the instances of SetCard in this deck public Iterator iterator() { // YOU WRITE THIS // Hint: you do NOT have to write your own Iterator class! // Instead, call Arrays.asList on your array of setCards to // convert it into a List, and then you can just return the result // from a call to the "iterator" method on that List! Wow! return null; } // main public static void main(String[] args) { testSetDeckClass(); } //////////////////////////////////// // testSetDeckClass //////////////////////////////////// public static void testSetDeckClass() { System.out.print("Testing SetDeck class... "); // first test an unshuffled deck SetDeck setDeck = new SetDeck(false); // unshuffled! for (int i=0; i<81; i++) { assert(setDeck.getCardsLeft() == 81-i); assert((setDeck.hasNextCard() == true) && (setDeck.nextCard().getCardIndex() == i)); } assert(setDeck.hasNextCard() == false); assert(setDeck.getCardsLeft() == 0); // now test that SetDeck is iterable setDeck = new SetDeck(false); // another unshuffled deck int seen = 0; for (SetCard setCard : setDeck) seen++; assert(seen == 81); // now test a shuffled deck HashSet seenCards = new HashSet(); setDeck = new SetDeck(); // default is shuffled! for (SetCard setCard : setDeck) { int cardIndex = setCard.getCardIndex(); assert((cardIndex >= 0) && (cardIndex < 81)); assert(seenCards.contains(cardIndex) == false); seenCards.add(cardIndex); } // now test that the shuffle itself works. We'll use a very // simplistic approach and just get two shuffled decks and require // that they not have more than 3 of the same cards occur // at the same indexes: int matches = 0; SetDeck setDeck1 = new SetDeck(); SetDeck setDeck2 = new SetDeck(); for (SetCard setCard1 : setDeck1) { SetCard setCard2 = setDeck2.nextCard(); if (setCard1.equals(setCard2)) matches++; } if (matches > 3) { System.out.println("There may be a problem with your shuffling, because"); System.out.println("two different SetDecks had the same cards in "); System.out.println(matches + " locations, where more than 3 is very unlikely."); System.out.println("Try running the test again. If this message comes up"); System.out.println("more than once-in-a-blue-moon, then you probably have a bug."); assert(matches <= 3); } System.out.println("Passed all tests!"); } }