Java Reference
In-Depth Information
6.
Write a program that plays the game tic-tac-toe. Represent the game board by an array of nine values. Each
location in the array contains either an X, an O, or a blank. The total number of possible board configurations is
3 9 , or approximately 20,000. Associated with every possible configuration is a best move.
Generate all possible board configurations, and let them be search keys in a dictionary. For each search key, let
the next best move be its associated value. Once you have created the dictionary, use it to decide the moves for a
computer-based player in a game of tic-tac-toe.
7.
A picture dictionary is a collection of images, each of which is identified by a descriptive word. Form a picture
dictionary from the data in external files, which you can create from royalty-free images found online. Design
and implement a user interface that provides search and display functions.
A NSWERS TO S ELF -T EST Q UESTIONS
1.
DictionaryInterface<Name, String> myDictionary = new Dictionary<Name, String>();
2.
myDictionary.add( new Name("Joe", "Java"), "555-1234");
3. Name britney = new Name("Britney", "Storm");
if (myDictionary.contains(britney))
System.out.println("Britney's phone number is " + myDictionary.getValue(britney));
else
System.out.println("Britney is not in the dictionary");
or
String phoneNumber = myDictionary.getValue( new Name("Britney", "Storm"));
if (phoneNumber == null )
System.out.println("Britney is not in the dictionary");
else
System.out.println("Britney's phone number is " + phoneNumber);
4.
The Scanner methods hasNext and next that readFile calls throw only runtime exceptions, which need not be
caught. So although the call to readFile can be outside of a try block, it is inside the try block because its
argument—the Scanner object data —is local to the try block. By declaring data outside of the try block, you
could move the call to readFile after the last catch block.
5.
public String remove(Name personName)
{
return phoneBook.remove(personName);
} // end remove
6.
public String changePhoneNumber(Name personName, String newPhoneNumber)
{
return phoneBook.add(personName, newPhoneNumber);
} // end changePhoneNumber
7.
We called getValue instead of contains to simplify the logic. If we called contains and found that the current
word was already in the dictionary, we would need to call getValue to get its frequency. But we can use the result
of getValue to see whether the word is in the dictionary.
Search WWH ::




Custom Search