CS 21: Algorithmic Problem Solving

 

HW #12: Mastermind and Lingo

Due 11:59pm FRIDAY, May 4

The program handin21 will only submit files in the cs21/homework/12 directory.

Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page

Introduction
This homework (Homework #12) is the last required homework assignment for the semester. For this assignment, you will write two separate but closely related programs: one which implements the game of Mastermind, and one which implements part of the TV gameshow Lingo. The two games are EXTREMELY similar, and you will find much of the same logic used in the Mastermind program showing up in the Lingo program.

Both programs are short. By this, we do not mean that they will necessarily take you a short amount of time to implement and test. Rather, we mean that the complete solution for each program should be less than about 100 lines.

Both Mastermind and Lingo are turn-based guessing games. In both games, the computer selects a random sequence (numbers in one, letters in the other) and the player tries to guess the answer. After each guess made by the player, the computer provides status about how close (or far) the player is to arriving at the solution. In both cases, the status confers information about the number of items in the sequence that are correct, differing only in minor details about how this information is displayed. The game ends when the player guesses the correct sequence.

The solution to Mastermind is worth 70% of the grade for this assignment. It is more important to us that you get Mastermind working completely than it is for you to make failed attempts at both programs. If you do not have a complete, bug-free solution to Mastermind, do not even attempt the solution to Lingo.

Mastermind
Mastermind is a turn-based guessing game. The computer chooses a random sequence and the player must try and guess the sequence. In our version of Mastermind, the sequence will always be made up of a random selection of the digits from 0-9. (The exact length of this sequence will be a parameter to your program.) After each guess, the computer tells you how close you are to arriving at the solution.

To make things concrete, let's use an example where the computer is selecting a 4-digit sequence. 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: 3224.

It's time for you to begin guessing the sequence. Your initial guess is: 1221. Notice that there are no 1's in the target sequence (3224), but you did get both 2's right. So, the status you would receive is:
You have 2 digits exactly correct and another 0 in the wrong place.

Clearly you don't know the sequence yet, so you make another guess: 2354. Both of the first two digits you guessed (the 2 and the 3) are in the target sequence, but they are not in the right place. The 5 is not in the target sequence, but the 4 is, and it's in the right place. So, the status you would receive is:
You have 1 digits exactly correct and another 2 in the wrong place.

Here is the complete run of this sample game:

Enter a sequence of 4 numbers: 1221
You have 2 digits exactly correct and another 0 in the wrong place.
Enter a sequence of 4 numbers: 2354
You have 1 digits exactly correct and another 2 in the wrong place.
Enter a sequence of 4 numbers: 3311
You have 1 digits exactly correct and another 0 in the wrong place.
Enter a sequence of 4 numbers: 4524
You have 2 digits exactly correct and another 0 in the wrong place.
Enter a sequence of 4 numbers: 5656
You have 0 digits exactly correct and another 0 in the wrong place.
Enter a sequence of 4 numbers: 6643
You have 0 digits exactly correct and another 2 in the wrong place.
Enter a sequence of 4 numbers: 3224
You won in 7 turns!
The design of the solution is completely up to you, but we are providing the main function for you -- and you cannot change it! Your program must run exactly as the run above using the exact main function we provide:
def main():
    game = MasterMind(4)  # '4' specifies the number of digits in the sequence
    game.play()

if __name__ == '__main__':
    main()
The only remaining requirement of Mastermind (once you have the above working) is to informatively tell the user if they type in a sequence which is not composed entirely of digits or if they type in a sequence of the wrong length. In these cases, do not provide additional status information and ask them to provide another guess.
Lingo
Lingo is a turn-based guessing game. The computer chooses a random sequence and the player must try and guess the sequence. (Does this sound familiar?) This game differs from Mastermind in three key ways.

To make things concrete, let's use an example where the computer is selecting a 5 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 and the computer's choice: guava.

It's time for you to begin guessing the word. Your initial guess is: rates. Notice that there is only 1 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 rates is an 'inexact' match, you'd receive the status message:
-a---

Clearly you don't know the word yet, so you make another guess: apply. 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 thank. While it's true that you know there are no t's in the solution (from your guess of rates earlier), you're trying to see if some of these other letters are present (the h, n and k) and you're seeing if you can figure out where the a goes. The status message now that you have the a in the right position is:
--A--

Here is the complete run of this sample game:

Enter a 5 letter word: rates
-a---
Enter a 5 letter word: apply
a----
Enter a 5 letter word: thank
--A--
Enter a 5 letter word: pious
---u-
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!
As with Mastermind, the design of the solution is completely up to you, but we are providing the main function for you -- and you cannot change it! Your program must run exactly as the run above using the exact main function we provide:
def main():
    game = Lingo(5)  # '5' specifies the length of the target word
    game.play()

if __name__ == '__main__':
    main()
As before, the only remaining requirement of Lingo (once you have the above working) is to informatively tell the user if they type in a letter sequence which is not a word or if they type in a word of the wrong length. In these cases, do not provide additional status information and ask them to provide another guess.

(Warning: when you get this working, be prepared to spend a while playing as it is remarkably addictive.)