Compsci 100, Spring 2006, Jotto
This website has the Rules of Jotto, although this assignment will not be using the scoring system they describe.
Heres's a simple web-based version of game although the version you write will be much better and you'll be able to share it with friends.
In this write-up there is first a description and screen shots of how the game is played. Then the code you write is described after the game description and screen shots.
You Guess the Computer's Secret Word
In this mode you get 15, 12, or 10 guesses to guess the computer's secret word when a new game starts. You'll need to write code for a class JottoModel that allows the user to play a game in this way. The code is described after the screen shots and game description.
On the right there's a screen shot of the end of a medium game (user gets 12 guesses)
in which I'm trying to guess the computer's word. After I've guessed
pails I can determine that the following letters are not
in the secret word from the words with zero letters in common with the
computer's secret word.
a e i l p r s t ySince fruit has two in common, these must be the letters u fSince spoil has one in common, that letter must be 'o' (given the letters not in the word). From ghost we know one of 'g' or 'h', but not both, is in the secret word. After guessing stage we know the letter must be 'g'. So we now know these letters are in the secret word: f g o uand after guessing goofy we can determine these letters are not in the secret word. a b c d e h i l m p r s t yAt this point I guessed fungo which is a kind of baseball bat used in batting practice. |
After guessing the computer responded that there were five letters in common and that I won the game as shown on the left below.
I don't always win. On the right is a screen shot of an easy game (I got 15 guesses) in which I figured out the five letters on guess number 12 with slate, then guessed three more words with the same letters only to lose.
Computer Guesses Your Word
Here's a screen shot of me choosing to let the computer guess my secret word by choosing the New Game menu and the appropriate option. (The code section below tells what happens in the model code you write when this choice is made.)
When the computer guesses your word, the code you write will not
mimic what a human does. For extra credit you can use some
human-like reasoning, but as you'll see a simple method allows the
computer to guess most words very quickly.
The main idea is for the computer to guess a word that could be the secret word, choosing one such word at random. When the user responds with the number of letters in common with the user's secret word, the computer eliminates every word in its dictionary that doesn't have this many letters in common. For example, if the computer guesses fruit and the user responds that there are two letters in common with the user's secret word, the computer can remove ghost, ruins, tires and lots of other words from the dictionary since these words have, respectively, one, three, and three letters in common with fruit and thus cannot be the secret word. The computer would leave flips, track, and others in the dictionary since these have two letters in common with fruit and could possibly be the user's secret word. The computer continues to guess a word that could be the secret word until either the computer guesses correctly or there are no words left to guess. The latter could happen, for example, if the user's word is not in the computer's dictionary or if the user makes a mistake in responding about the number of letters in common (see below for examples). |
The computer generates a guess and I type in the number of letters in common with my secret word which is radii in the examples that follow. The computer has guessed my word in nine guesses and I type the number 6 to indicate this is my secret word (as shown on the right). This is an easy way to convey "you guessed right" to the computer which in general expects a number from the user in this version of the game.
Here are two more shots of the computer guessing my word radii. As these show, the computer doesn't use the same sequence of guesses every time, and sometime guesses the word very quickly!
Finally, two more shots of the computer trying to guess radii. On the left it seems like a sequence of very strange guesses leading to the secret word. On the right I made a mistake and entered 3 for the word drain which has four letters in common. The computer can't account for this mistake and indicates no more guesses after six guesses.
Code You Write
You're given Jotto the main class that launches the GUI, the JottoViewer GUI class and an AbstractJottoModel abstract class you will subclass to create the model that communicates with the view/GUI.You're also given a file of five-letter words though you're free to use your own file (the GUI allows this).
The code has been designed so that you can create a subclass of AbstractJottoModel, implementing the three methods described in comments of that class and designing state appropriately for the model to either guess the human-user's word or for the human-user to guess the model's secret word.
You may find it useful to write helper methods in the class you write, that are called by the three methods you must write --- these three methods are abstract in the base class.
You must call your class JottoModel
to make the main
program Jotto work with your code.
Model/View Communication
The view calls the model by sending an Object, typically a String, to the model'sprocess
method. Since you're writing
JottoModel
as
a subclass of
AbstractJottoModel,
you should be able to ignore
process
(relying on its implementation
in AbstractJottoModel)
and only write processHumanCount(..)
and processHumanGuess(..)
which will change state in your
model and call appropriate methods in the view. These methods are
called, respectively, when processing
the number of letters in common when the computer is guessing the user's
secret word (processHumanCount
)
and when the user is
guessing the computer's secret word (processHumanGuess
).
The model communicates with the view by calling:
-
showModalMessage
which pops up a dialog box the user must take action with -- e.g., when the game is over. -
messageViews
to show an informational message in the view, e.g., the number of guesses left. This is shown in the textbox in the lower part of the GUI/view in the screen shots above. -
processModelResponse
which sends a String to the view, i.e., the computer's guess or the number of letters in common depending on whether the computer/model is guessing the secret word.
For example, when the computer is guessing the user's word, the model's
newGame
method will be called -- you write this code. You
must start the model-view-model-view... communication by sending a guess
to the view via processModelResponse
. The view will then
send the number of letters in common back to the model. The model must
then make a guess and send the guess to the view which will send back a
number of letters in common (by calling process
which calls
processHumanCount
), and so on. Thus model-view
communication is started by the model in this scenario and works like
this:
- New game started, the model sets its state and then
the model calls
myView.processModelResponse(guess)
to show the user the word the computer/model is guessing. - View calls model's
process
method which callsprocessHumanCount
(assuming state set appropriately above) with the number of letters in common with the last guess. If this number is six, the game is over. Otherwise, the model callsmyView.processModelResponse
with a guess unless there are no more guesses left. This step repeats.
When newGame
is called when the user is guessing the
computer's word, the first communication between the model and the view
will be generated by the view -- the model gets the user's guess and
sends the number of letters in common back to the view via
processModelResponse
.
The scenario works like this.
- After new game is called in the model, the model will set state.
- At some point the view will call the model's
process
method which will callprocessHumanGuess
assuming state was set appropriately in the first step. The model code you write will determine the number of letters in common with the secret word and send this information to the view viamyView.processModelResponse
. This repeats until the view guesses the secret word or until the number of guesses allowed is reached.
CompSci 108 Fall 2009 |
Software Design and Implementation |