Drawer.javaTopGuesser.javaCards.java

Cards.java

import java.awt.*;
import java.io.*;
import objectdraw.*;

// Cards manages the deck of cards used to play Pictionary.  The file
// is a text file.  Each line represents a different card.
public class Cards {
   // Maximum deck size
   private static final int MAX_DECK_SIZE = 20;
   
   // The cards
   private String[] cards = new String[MAX_DECK_SIZE];
   
   // The number of cards in the deck.
   private int numCards;
   
   // The next card to return in the deck
   private int next = 0;

   // Construct a new Cards object.  Ask the user for a file containg cards.
   // Read in the cards to initialize the array.  Shuffle the cards.
   public Cards () {
      // Set up the input stream.
      File cardFile = getDeckFile();
      try {
         BufferedReader in = new BufferedReader (new FileReader (cardFile));
         
         // -- Insert code here to read in the cards. Stop when readLine() returns null.
         // -- Be sure numCards contains the number of cards in deck after reading.

         // Shuffle the cards
         shuffle();
         
      } catch (FileNotFoundException e) {
         // If the card file can't be found, just create a deck with one card.
         cards[0] = "cecil sagehen";
         numCards = 1;
      } catch (IOException e) {
         // If we get an error reading the file, make sure the deck has at least one
         // card and then shuffle what we did read in.
         if (numCards == 0) {
            cards[0] = "cecil sagehen";
            numCards = 1;
         } else {
            shuffle();
         }
      }
   }
   
      // Using a file dialog, get the name of a file to load
      // as the deck of cards.  Return null if the user cancels the
      // dialog box
   private File getDeckFile() {
      String fileName;
      
      FileDialog dialog = new FileDialog( new Frame(),
                                  "Select the file of cards.",
                                   FileDialog.LOAD );
      dialog.setVisible(true);
      
      if ( dialog.getDirectory() != null ) {
         fileName = dialog.getDirectory() + dialog.getFile();
          return new File (fileName);
       } else {
          return null;
       }
   }

   // Shuffle the cards
   private void shuffle() {
      RandomIntGenerator random = new RandomIntGenerator (0, numCards - 1);

      // The indexes of the cards to swap
      int first, second;
      
      // Number of swaps to do in the shuffle
      int numSwaps = 100;
      
      // Temporary string for swapping
      String temp;

      // Swap pairs of random cards repeatedly to shuffle
      for (int i = 0; i < numSwaps; i++) {
         first = random.nextValue();
         second = random.nextValue();
         temp = cards[first];
         cards[first] = cards[second];
         cards[second] = temp;
      }
   }
   
   // Returns the next card in the deck.
   public String nextCard () {
      String returnCard = cards[next];
      next = (next + 1) % numCards;
      return returnCard;
   }
}

Drawer.javaTopGuesser.javaCards.java