Part 1 - Using Exceptions

Copy your monte.py 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 monte.py program or if yours doesn't throw an exception when n is 0, use this one.

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

Handin:
Handin what you have of your lab so far.

Part 2 - Picture this

Create a program called sketchy.py 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.

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 master.py 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).

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

Randomly choose the codemaker's code.

Repeatedly prompt the user for their guess

If their guess is correct, end the game with a congratulatory message.

Otherwise, give clues (pegs) that correspond to their guess.

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:

First assign the black pegs. Do this by iterating through both strings one character at a time (a for-loop with i going from 0 to 4 that looks at both code[i] and guess[i]), assigning a black peg if the characters in the same position match. If they do match, change that character/peg in your guess to an 'x' and in the code strings to a 'z', say, so that you know you have used both of these pegs for a clue (and you won't use them again when assigning white pegs and accidentaly double-count). (You can find some useful string methods on pg 125 of the text, or here in order to do this.)

Next assign the white pegs. Do this by considering the first peg in the guess string and trying to find the matching character in the code string (note that if the first character is an 'x' then there definitely won't be a match in the code string). If you find a match, again change the guess character to 'x', the matching code character to 'y' and continue with the second peg in the guess string, then the third, and so on. Because of your changes to 'x' and 'y', you won't assign both a black and white peg for one guess or clue peg.

You know you can only return once from a function. In Python it is easy to have a function return a pair as its value. For example, if b and w are your counts for the black and white pegs, your function can sayreturn b, w
If the function that is evaluating guesses is called evaluateGuess( code, guess ) and it has such a return statement, you can call this function with black, white = evaluateGuess( code, guess )

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

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

generateCode()

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.

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.

Handin

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