A skeleton version of the program will appear when you run update21 in a terminal window. The program handin21 will only submit files in this directory.
You may work with one partner on this assignment. If you work with a
partner, put your name and the name of your partner in the list of
authors at the top of your program. Only one partner needs to run
handin21 to submit the files for the group. Both partners
will receive the same grade.
For this assignment, you will write a program that implements the TV game show Lingo. The focus of this assignment is for you to practice using top-down design to construct your solution incrementally.
Lingo is a turn-based guessing game. The computer selects a random word and the player tries to guess the word. Each guess must also be a valid word of the same length (this is what makes the game interesting and hard). After a guess is made, the computer provides status information about how close (or how far) the player is to arriving at the solution. The status information includes which letters are correct, but in the wrong position, and which letters are correct, and in the right position.
To make things concrete, let's use an example where the computer is selecting a five-letter word. Normally, as a player of the game, you would not know what the computer had chosen. However, let's make things easier and peek at the computer's choice: guava.
It's time for you to begin guessing the word. Your initial guess is:
spray. Notice that there is only one letter that you guessed
that's also in the target word (the letter a). Rather than
indicate the number of letters you have correct, the status message
will specify which letters are exactly correct and which are in the
sequence, but in the wrong place. You'll do this by UPPER-CASING the
exact matches, leaving the 'inexact' matches alone, and replacing the
wrong letters with dashes. So, in this case, where the letter
a of your guess spray is an 'inexact' match, you'd
receive the status message:
Clearly you don't know the word yet, so you make another guess:
arrow. Again, the only correct letter is the a, and
now it's in a different wrong place. So, the status you would receive
Your next guess is uvula. You continue to have the
a in the wrong position, but now you guessed two other
correct letters, u and v. Since there is only one
u in the target word, and since neither of the u's
that you guessed are in the correct position, your status information
should indicate that one u is in the wrong position:
Your next guess is games. While it's true that you know there
are no s's in the solution (from your guess of spray
earlier), you're trying to see if some of these other letters are
present (the g, m and e) and you're still
trying to figure out where the a goes. The status message
now that you have the g in the right position and the a is still in the wrong position:
Here is run of this sample game:
How many letters should the secret word have? 5 Enter a 5 letter word: spray ---a- Enter a 5 letter word: arrow a---- Enter a 5 letter word: uvula uv--a Enter a 5 letter word: games Ga--- Enter a 5 letter word: curve -U-V- Enter a 5 letter word: suave -UAV- Enter a 5 letter word: guava You won in 7 turns!
if word in wordList: ...
>>> word = "apple" >>> word 'apple' >>> charList = list(word) >>> charList ['a', 'p', 'p', 'l', 'e'] >>> rejoined = ''.join(charList) >>> rejoined 'apple'
We suggest that you store the secret word and the guessed word as strings. Then to produce the status information, create list versions of these words that can be modified. You can use the list version of the guessed word to hold the status information. First search for exact matches. When an exact match is found, change the letter in the guessed word to upper case and hide the letter in the secret word by changing it to some other character, such as '.' (you don't want to find it again when you search for inexact matches).
For example, given the target word guava and the user guess gulag, we expect the status message: GU-a-. We'll start by converting both the target and the guess into lists:
target: ['g','u','a','v','a'] guess: ['g','u','l','a','g']
Now, we'll check for exact matches, beginning from left to right in the guess list. "Does the first letter of 'gulag' match the first letter of 'guava'?" It does, so we'll capitalize the first letter of the guess list and we'll change the first letter of the target list to a period:
target: ['.','u','a','v','a'] guess: ['G','u','l','a','g']
Next, "Does the second letter of 'gulag' match the second letter of 'guava'?" It does again, so we'll capitalize the second letter of the guess list and we'll change the second letter of the target list to a period:
target: ['.','.','a','v','a'] guess: ['G','U','l','a','g']
Next, we'll ask if the third, fourth, or fifth letters match, and none will, so the two lists will remain unchanged.
We move on to finding inexact matches. For each letter in the guess list, we do the following:
Following this algorithm, we look at the first letter is the guess list: G. Since it's capitalized, we go on to the next letter: U. It, too, is capitalized, so we move on to the third letter, l, which is not in the target list, so we replace the letter in the guess list with a '-':
target: ['.','.','a','v','a'] guess: ['G','U','-','a','g']
The fourth letter in the guess list, a, is in the target list, so we replace the letter in the target list with a '.':
target: ['.','.','.','v','a'] guess: ['G','U','-','a','g']
The final letter in the guess list, g, is not in the target list, so replace the letter in the guess list with a '-':
target: ['.','.','.','v','a'] guess: ['G','U','-','a','-']
The guess list is now nearly the status information which you need to provide. You just need to join the letters in the list together to form the string 'GU-a-'.
Warning: When you get this working, be prepared to spend a while playing as it is remarkably addictive!
Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.