Lab 04

Exceptions, Pictures, and Mastermind
Due by 6pm on Tuesday, March 3

The purpose of this lab is to:

Part 1 - Using Exceptions

Copy your program from lab 03 to your lab04 directory and modify it to handle the exception that is thrown when the user inputs 0 for n. If you don't have a program or if yours doesn't throw an exception when n is 0, use this one.

Copy the program to your lab04 directory and modify it to handle the exception that is thrown when the user inputs n is 0.

Handin what you have of your lab so far.

Part 2 - Picture this

Create a program called that draws the picture you designed on your prelab using the picture module. A few of the things you can do:

To adjust the pen width, use the setPenWidth function. To position and draw with the pen, use the setPosition, setDirection, rotate and drawForward functions. To draw simple shapes, you can use functions like drawCircle, drawCircleFill, drawRect, drawRectFill, etc. Use setFillColor to change the fill color used when creating shapes. Use setOutlineColor to change the color of shape edges and pen lines. Don't forget to use the display function followed by an input function call so your image gets displayed, and gives you time to savor your creation before closing the window.

You can read more about different picture functions here

Some of the best sketches will be shown in class to general acclaim.

Please handin what you have of your lab so far.

Part 3 - Mind Mastery

Mastermind is a neat (although often frustrating) puzzle game. It works a something like this: There are two players. One player (your program) is the codemaker, the other (the user) is the codebreaker. The codemaker chooses a sequence of four colored pegs, out of a possible six colors (red, blue, green, yellow, orange, and purple). He may repeat colors and place them in any order he wishes. This sequence is hidden from the codebreaker. The codebreaker has 10 chances to guess the sequence. The codebreaker places colored pegs down to indicate each of her guesses. After each guess, the codemaker is required to reveal certain information about how close the guess was to the actual hidden sequence.

Describe the Problem:
Write a program called that allows the user to play a text-based version of the fantastic game Mastermind.
input: repeatedly get guesses from the user, until they either guess the code, or run out of guesses.
goal: generate a random code, and correctly provide the user with feedback on their guesses.

Understand the Problem:
The trickiest part of this game is determining how to provide feedback on the codebreaker's guesses. In particular, next to each guess that the codebreaker makes, the codemaker places up to four clue pegs. Each clue peg is either black or white. Each black peg indicates a correct color in a correct spot. Each white peg indicates a correct color in an incorrect spot. No indication is given as to which clue corresponds to which guess.

For example, suppose that the code is RYGY (red yellow green yellow). Then the guess GRGY (green red green yellow) would cause the codemaker to put down 2 black pegs (since guesses 3 and 4 were correct) and 1 white peg (since the red guess was correct, but out of place). Note that no peg was given for guess 1 even though there was a green in the code; this is because that green had already been "counted" (a black peg had been given for that one).

As another example, again using RYGY as our code, the guess YBBB would generate 1 white peg and 0 black; yellow appears twice in the code, but the guess only contains one yellow peg. Likewise, for the guess BRRR, only 1 white peg is given; there is an R in the code, but only one. Below is a table with guesses and the correponding number of black and white pegs given for that guess (still assuming the code is RYGY).

guess black pegs white pegs
YYYY 2 0
YRYR 0 3
BBPO 0 0
PGYR 0 3
YYYG 1 2
RYGY 4 0

Check here for an online graphical version of the game (where their red pegs are our black pegs).

A sample run of our text-based program may look like this:

Sample output

    I have a 4 letter code, made from 6 colours.
    The colours are R, G, B, Y, P, or O.

	Your guess: GGGG
    Not quite. You get 0 black pegs, 0 white pegs.

        Your guess: YYYY
    Not quite. You get 1 black pegs, 0 white pegs.

        Your guess: YOYO
    Not quite. You get 0 black pegs, 2 white pegs.

        Your guess: PPYO
    Not quite. You get 1 black pegs, 2 white pegs.

        Your guess: POYB
    Not quite. You get 1 black pegs, 3 white pegs.

        Your guess: PBOY
    You win! Aren't you smart.

Design an Algorithm:
Once you understand how the game works, you should design a pseudocode plan of attack. The general steps are:

Some of these steps are straight-forward, but certainly it would be worth your while to write down an approach to randomly generating the code, and giving the clue. Here is our recommendation for the latter algorithm is as follows:

Implement a Design:
Now that you have some of the kinks worked out in theory, it is time to write your program

You may assume the user always provides a guess with the available colors, and always in uppercase.

Make and use an integer constant NUM_TURNS that represents the number of allowable turns and set this to 10..

To generate the code, write a function


that generates the codemaker's code (and returns it as a String to the caller). That is, this function should randomly generate 4 colored pegs, selected from R, B, G, Y, O, and P, and return it as a 4-letter string. You'll want to use the random methods as discussed in lab03 in order to randomly generate a color for each peg. In particular, you'll generate an integer between 0 and 5 inclusive. You can use this as an index into the string "RBGYOP" of all of the color symbols to get your next color.

Test your generateCode function thoroughly before continuing. Once it's working, write a second function

    evaluateGuess( code, guess )

that returns the numbers of white and black clue pegs according to the given guess and code. Keep going around the guess loop until the number of black pegs is 4 or the user has name NUM_TURNS guesses.

Note that you can "change" the ith character in a string s to an 'x' as follows:

    s = s[0:i] + "x" + s[i+1:len(s)]

Also note you can omit the len(s) from the above expression. That is, if you write s[i:], Python interprets that as the substring of s from position i to the end. Similarly, s[:i] denotes the substring of s from the beginning up to (but not including) i.

Test the Program:
It is hard to test your program with a random code that you don't know. One trick that helps is to have your generateCode( ) function return a code that you do know. After generateCode( ) is debugged; insert a new first line:
            return "RYGY"
you can then tell if your evaluateGuess( ) function is giving correct answers. Remember to remove (or comment out) this line before you hand in your program.


If you followed the Honor Code in this assignment, make a README file that says

I affirm that I have adhered to the Honor Code in this assignment.

You now just need to electronically handin all your files. As a reminder

     % cd             # changes to your home directory
     % cd cs150       # goes to your cs150 folder
     % handin         # starts the handin program
                      # class is 150
                      # assignment is 4
                      # file/directory is lab04
     % lshand         # should show that you've handed in something

You can also specify the options to handin from the command line

     % cd ~/cs150     # goes to your cs150 folder
     % handin -c 150 -a 4 lab04

File Checklist

You should have submitted the following files: (for ease of grading)

Last edited January 27, 2015 T. Wexler, A. Sharp, C. Taylor, R. Geitz