CSE 231 Summer 2015 Programming Project #3 Assignment Overview This project focu
ID: 662954 • Letter: C
Question
CSE 231
Summer 2015
Programming Project #3
Assignment Overview
This project focuses on programming with loops and conditionals, as well as our first project
working extensively with strings. It is worth 60 points (6% of your overall grade). It
is
due
Mon
day,
June 8th
before midnight.
The Problem
We are going to play the classic game Mastermind.
Here are the rules
:
http://en.wikipedia.org/wiki/Mastermind_(board_game
)
You can p
lay here:
http://www.archimedes
-
lab.org/mastermind.html
We are going to play the game with a slight variation, using
letters
instead of colors
:
RYBGWO
(representing Red, Yellow, Blue, Green, White, Orange)
. Thus each guess will be a
4
-
character
string
, and the
key
(the
string
being decoded) will be a
4
-
character string.
We will also stipulate that
no
character
will repeat in the key
(to make your progra
m a little easier)
. Thus
,
RYBG is a legal key
but RYYB
is not legal and should not be used in our game.
In our game, you provide the key to be guessed at the start of the program, and then your program
allows a player to try and guess the entered key.
(T
he program could easily generate a random key,
but that would make
test
ing
your program
more challenging
.
)
Your program does not try to play
mastermind, it simply enforces the rules and provides feedback to the player.
After
each guess
made by the player
,
your program
generates
two numbers: h
ow many of the 4
characters
in the guess are exactly correct (the correct
character
in the correct position of the key)
and how many of the guess
characters
are in the key but not in the correct position.
Note that those
two values cannot sum to more than 4, or put another way, each
character
in the guess produces at
most one value.
Th
e player gets
eight
guesses;
if they do not
guess
the key in 8
guesses, they lose.
Program Specifications
Your program w
ill play the mastermind game as follows:
1.
Prompt for the
4
-
character
key we will play with. No error checking here.
2.
Prompt the user for a guess.
a.
we check to see if the guess is exactly length 4
b.
we check t
o see if the guess contains only characters RYBGWO
c.
we check to see if the guess has no rep
eats
d.
lower case characters are allowed for input
any violation of these rules prints an error message and prompts for a guess again.
Bad input does not count as a gues
s toward losing the game.
3.
End the game if:
a.
the gu
es
s is correct. Print out a win
message
including the count of how many
guesses were made
(see example below)
.
b.
it was the last guess (the
eighth
) and the guess is incorrect. Print out a loser message
and the value of the key
.
4.
If
the game isn
Explanation / Answer
print "Input the secret key (RYBGWO): ",
s = raw_input()
s.upper()
turn = 1;
print "Mastermind: your challenge is to guess the secret key."
print "There are six colors with no repeats: RYBGWO."
print "Input your guess (RYBGWO): ",
guess = raw_input()
l = []
while (turn < 8):
guess.upper()
if (guess == s):
print "You won It took "+str(turn)+" guess"
break
if (guess.isalpha() == True):
summ = guess.count('R') + guess.count('Y') + guess.count('B') + guess.count('G') + guess.count('W') + guess.count('O')
if (guess.count('R') <= 1 and guess.count('Y') <= 1 and guess.count('B') <= 1 and guess.count('G') <= 1 and guess.count('W') <= 1 and guess.count('O') <= 1 and summ == 4):
if (len(guess) == 4):
curr_col = 0
curr_pos = 0
for i in range(len(guess)):
if (guess[i] is s):
curr_col += 1
if (guess[i] == s[i]):
curr_pos += 1
print "Correct color and position count: ", curr_pos
print "Correct color, but wrong position count: ", 4 - curr_col
else:
print "Bad guess so input another guess (RYBGWO): ",
guess = raw_input()
continue
else:
print "Bad guess so input another guess (RYBGWO): ",
guess = raw_input()
continue
else:
print "Bad guess so input another guess (RYBGWO): ",
guess = raw_input()
continue
turn += 1
print "Guesses (out of 8): "+str(turn)
print "HISTORY:"
for ele in l:
print ele
l.append(guess)
print "You loss"