jotto_model
, can be used with both user interfaces!
See snarfing help for information on how to use the Ambient/Snarf functionality in Eclipse. Note that the snarf URL for this semester is http://www.cs.duke.edu/courses/fall12/compsci101/snarf
jotto_commandline.py
for the command-line version or
jotto_gui.py
for the GUI version. All code is in the code directory
linked here.
jotto_model.py
module you will need to add global state and implement the functions
whose comments and signatures are provided in the file and described
here. A function signature is
its name, parameters, and return type. Read these descriptions
carefully, but be prepared to fix mistakes as they arise since sometimes
it is tricky to find errors when global variables are used.
Some of the functions will need to access (read) and sometimes alter
(write) the different global state kept in the module. For example
the load_words
function is written for you and fills the
global list _wordlist
. This list will be used in
start_game
and possibly in other functions as well.
start_game
needs to initialize the global state so
that the first guess can be made. This will include creating a list of
words that the secret word picked by the player could be, you should
call this list _possiblewords
. Initially
_possiblewords
contains the same words that
are in _wordlist
. You can't use _wordlist
itself for the list of possible words because _possiblewords
will change each time the user reports how many letters in common the
guess has with the user's secret word. Be sure that start_game
initializes all the state for a new game. In addition to the list of
possible words the state will likely include the number of
guesses that have been made and the last word guessed by the
computer (see below for why this last guess is needed). A simple
way to make a copy of a list, e.g., _wordlist
is to
use a full-slice:
_possiblewords = _wordlist[:]
get_guess
will return a randomly chosen word that
could be the secret word, i.e., the word
is chosen from _possiblewords
. It will alter global state to
keep track of what the last word guessed is for the
process_common_last
function described next ---
you'll need to determine what this state is, but please read the
documentation for process_common_last
carefully. You'll also need to remove this chosen word from the list of
possible words, otherwise the word might be guessed again. You
need to do this because it's possible that for a secret word like
"bagel", the computer guesses "gable". This has five letters in
common with the secret word, but you don't want the computer to
guess "gable" twice.
process_common_last
will use the number of letters in common
that's passed to it as a parameter and the global state that
represents the last word guessed by the computer to eliminate
all words from the global list of possible secret words that
don't have the same number of letters in common with the last
guess.
For example, if the last computer guess is "ghost" (stored in global state) and
the user chooses 3 as the number of letters in common with her secret
word ("toast" for example), the 3 is then
passed to process_common_last
so that the code you write removes all words
from _possiblewords
that do not have 3 letters in common
with "ghost". You'll likely want to call commonCount
in
implementing process_common_last
. You'll likely need to
update the number of guesses made too, that could be done in
get_guess
as well.
guess_count
returns the number of guesses made by the
computer in trying to guess the user's word. Ideally this will
simply be returning the value stored in a global variable or the
length of a global list or dictionary. Your code should not
make signficant calculations in determining the number of guesses made.