package cse1030.games.boggle; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; /** * A singleton class representing a game of Boggle. The instance is responsible * for maintaining the 16 standard dice used in Boggle. The instance also has an * English word dictionary. * * @author CSE1030_F_13_14 * */ public class Boggle { /** * The letters on the 16 boggle dice. */ private static final String[] LETTERS = { "AAEEGN", "ELRTTY", "WAOOTT", "ABBJOO", "EHRTVW", "CIMOTU", "DISTTY", "EIOSST", "YDELRV", "ACHOPS", "UHIMNQ", "EEINSU", "EEGHNW", "AFFKPS", "HLNNRZ", "XDEILR" }; /** * The singleton instance. */ private static final Boggle INSTANCE = new Boggle(); /** * The 16 boggle dice. */ private List dice; /** * The dictionary of lower case English words. */ private Set dictionary; /** * Constructs the instance by creating a collection of of the 16 standard * boggle dice and initializing an English word dictionary. * * @throws NullPointerException * if the dictionary cannot be initialized */ private Boggle() { this.dictionary = new HashSet(); this.readDictionary(); this.dice = new ArrayList(); for (String s : Boggle.LETTERS) { this.dice.add(new BoggleDie(s)); } } /** * Reads the dictionary file and stores the words from the file * in the Set this.dictionary. * *

* The dictionary file is named dictionary.txt and needs to be * located in the cse.games.boggle directory. * */ private final void readDictionary() { URL dictionaryURL = this.getClass().getResource("dictionary.txt"); try { String filename = dictionaryURL.getFile(); Scanner dictionaryInput = new Scanner(new File(filename)); while (dictionaryInput.hasNext()) { String word = dictionaryInput.next(); this.dictionary.add(word.trim()); } dictionaryInput.close(); } catch (IOException ex) { throw new NullPointerException("dictionary file could not be read"); } } /** * Returns the single Boggle instance. * * @return the single Boggle instance */ public static Boggle getInstance() { return Boggle.INSTANCE; } /** * Returns a list of the 16 dice in their current state. The order of dice is * guaranteed to be stable between calls to shuffleAndRoll. * *

* Clients are unable to modify the game dice using the returned list; i.e., * modifying the returned list has no effect on the dice held by the * singleton. * * @return a list of the 16 dice in their current state. */ public List getDice() { List result = new ArrayList(); for (BoggleDie d : this.dice) { result.add(new BoggleDie(d)); } return result; } /** * Randomly shuffles the order of the dice and rolls all of dice. This * simulates the shaking of the dice in the physical version of the game. */ public void shuffleAndRoll() { Collections.shuffle(this.dice); for (BoggleDie d : this.dice) { d.roll(); } } /** * Looks up a word to confirm that it is an actual English word at least 3 * letters in length. * * @param word * the word to look up * @return true if the word is an English word that is at least 3 * letters long, false otherwise */ public boolean lookUp(String word) { return true; } }