CS21 Lab 7: Top-Down Design: Lingo

Top-Down Design due first (Nov 1)!!!
Due 11:59pm Tuesday, 6 November 2012

Please read through the *entire* lab before starting! Also, as always, run update21 to create your cs21/labs/07 directory and create your programs for lab 7 in this directory.

Over the next two weeks, you will write a program that plays a word game called lingo.

For this lab, you will focus on using top-down design to create the overall structure of the program. Once your proposed design has been reviewed by your professor, you will use bottom-up implementation and unit testing to incrementally construct the full program.

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

$ update21
$ cd cs21/labs/07
$ 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 UPPERCASE letter 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!

Game Requirements
Lab Requirements

You should use Top-Down Design and your program's structure should reflect this.

For this lab you must turn in your top-down design first! You should create your design in a file called design.py and email it to your professor. We will then look over your design, make comments on it, and send it back. If we think your design is inadequate, we will ask you to fix and resubmit it. Once we have approved your top-down design, you can implement the full program.

Here are the top-down design requirements:

Here is an example of a stubbed out program. What you email us should be similarly structured.

NOTE: once you email us your design, we will try to get comments back to you within 24-36 hours. However, if everyone emails us their design at the same time, it may take longer. You are encouraged to start your design early, so you have enough time to get our helpful comments and still implement the full program.

No designs will be accepted after Thursday, Nov 1.

Once your design has been approved, copy it to another file to implement your game:

      $ cd cs21/labs/07
      $ cp design.py lingo.py
      $ vim lingo.py
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 alphabetic character(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 7 (one which is your solution to the basic lab 7 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.