CS21 Lab 8: Top-Down Design: Lingo

Due 11:59pm Thursday night, March 26 (note: you have one more day on this)

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.

Run update21, if you haven't already, to create the cs21/labs/08 directory. Then cd into your cs21/labs/08 directory and create the python programs for lab 8 in this directory (handin21 looks for your lab 8 assignments in your cs21/labs/08 directory):

$ update21
$ cd cs21/labs/08
$ vim lingo.py
Introduction

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.


Example

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:
---a-

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 is:
a----

Your next guess is again. This time the guessed word has two a's, as does the secret word. The first guessed a is in the wrong position and will appear as lower case, but the second guessed a is in the correct position (the third character) and should show up as an upper case guess in the status. There is also a correctly guessed g but it is in the wrong position and will appear as lower case. Since neither the i nor the n appear in the target word, the status of this guess is the following: agA--

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 m and e) and you're still trying to figure out where the other a goes. The status message now that you have the g in the right position and the a is still in the wrong position:
Ga---

Here is this example as it may appear in a game (see the Sample Output section below for other examples of a working solution):

Enter a 5 letter word: spray
- - - a -
Enter a 5 letter word: arrow
a - - - -
Enter a 5 letter word: again
a g A - -
Enter a 5 letter word: games
G a - - -
Enter a 5 letter word: curve
- U - V -
Enter a 5 letter word: suave
- U A V -
Enter a 5 letter word: guava
You won in 7 turns!

Requirements
Helpful tips
Sample Output

Here is some sample output from three runs of a lingo program, notice the error handling:

$ python lingo.py

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 Welcome to Lingo
   Each turn you will try to guess the secret word
   If you guess the word, you win
   If you guess some letters in the word, lingo will display:
     guessed letters in the correct position in upper case
     guessed letters in the incorrect position in lower case
 Good Luck!
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

word:  -  -  -  -  - 
Guess a  5 letter word: hello

word:  -  e  l  -  - 
Guess a  5 letter word: letter
Invalid Input, enter a word with 5 characters. try again...
Guess a  5 letter word: oops
Invalid Input, enter a word with 5 characters. try again...
Guess a  5 letter word: helpm
Invalid Input, helpm is not a valid word. try again...
Guess a  5 letter word: piles

word:  P  I  l  E  - 
Guess a  5 letter word: pixel

You guessed the word in 3 tries.  Congratulations!


$ python lingo.py

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 Welcome to Lingo
   Each turn you will try to guess the secret word
   If you guess the word, you win
   If you guess some letters in the word, lingo will display:
     guessed letters in the correct position in upper case
     guessed letters in the incorrect position in lower case
 Good Luck!
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

word:  -  -  -  -  - 
Guess a  5 letter word: snort

word:  -  -  -  r  - 
Guess a  5 letter word: retort
Invalid Input, enter a word with 5 characters. try again...
Guess a  5 letter word: great

word:  -  r  -  a  - 
Guess a  5 letter word: barks

word:  b  A  r  -  - 
Guess a  5 letter word: rstuv
Invalid Input, rstuv is not a valid word. try again...
Guess a  5 letter word: roles

word:  R  -  -  -  - 
Guess a  5 letter word: ramps

word:  R  A  -  -  - 
Guess a  5 letter word: rabid

word:  R  A  B  i  - 
Guess a  5 letter word: rabbi

You guessed the word rabbi in 7 tries.  Congratulations!


$ python lingo.py

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 Welcome to Lingo
   Each turn you will try to guess the secret word
   If you guess the word, you win
   If you guess some letters in the word, lingo will display:
     guessed letters in the correct position in upper case
     guessed letters in the incorrect position in lower case
 Good Luck!
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

word:  -  -  -  -  - 
Guess a  5 letter word: grill

word:  -  -  i  l  - 
Guess a  5 letter word: trips

word:  t  -  i  p  - 
Guess a  5 letter word: QUIT

The word you were trying to guess is: plait
Better luck next time!


Extra Features
Do not attempt any of these until you have the required part done and fully tested. Some extra features to add if you have time:
  1. Have the user enter the size of the word s/he wants to use for the game. Your program should correctly test for invalid input (size cannot be negative or zero, nor an alpabetic charater(s)), and it should read in all words of the user's chosen size from the dictionary file.
  2. Add a graphics component to your game. You could think of highlighting guessed letters in a certain color or a certain colored containing box. Take a look at the first picture on this page (the one with rows of guessed words) to get some ideas. If you try this one, you should submit two separate programs for lab 8 (one which is your solution to the basic lab 8 assignment, the other which is your solution with a graphical user interface). Copy your working basic solution to a new file, lingo_withgraphics.py, and add graphics to this new file so that you still have a separate file, lingo.py, that contains your solution without graphics.
Submit

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.