-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmastermind.py
91 lines (77 loc) · 2.46 KB
/
mastermind.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# mastermind.py
# Author: Chris Corea
#
# Commandline implementation of Mastermind
from google.appengine.ext import db
from random import randint
def printInstructions():
print "\n\nThis is a game of wits and luck! " \
"Try to guess the right colors! \n" \
"The computer will automatically generate a random color code made up of six colors, \n" \
"you're job is to figure out that secret code. " \
"You will be asked to insert a color to try to match the secret code. \n" \
"Remember, the code that you generate must be exactly like the secret code, " \
"the order of the colors does matter,\n" \
"but don't worry, you will be able to see all" \
" of the combinations that you enter while getting feedback from the game.\nYou will first be" \
"asked to enter the number of how many colors you would like to have in play; you can choose up \n" \
"to 6 colors and they will be used in the order that they are listed in below. \n" \
"\nThe colors you will use for this game are listed below. Good luck!\n\n"
class Scores(db.Model):
user = db.UserProperty()
numberGuesses = db.IntegerProperty()
name = db.StringProperty()
def generateSecret():
colors = getColors()
return [colors[randint(0,5)] for x in xrange(4)]
def getGuess():
colors = getColors()
print "enter 4 colors from:"+str(colors)
i = 0
guess = []
while i < 4:
color=raw_input('enter a color: ')
guess.append(color)
i += 1
return guess
def getColors():
return ['red','green','blue','purple','orange','yellow']
def computeExacts(guess,secretCopy):
i = 0
match = 0
while i < 4:
if guess[i] == secretCopy[i]:
match += 1
# cross out
guess[i] = 'chris'
secretCopy[i] = 'david'
i += 1
return match
def computePartials(guess, secretCopy):
i = 0
partial_matches = 0
while i < 4:
j = 0
while j < 4:
if guess[i] == secretCopy[j]:
partial_matches += 1
# "cross out" finds
guess[i] = ''
secretCopy[j] = ''
j += 1
i += 1
return partial_matches
# main
if __name__=='__main__':
printInstructions()
secretCode = generateSecret()
matches = 0
while matches != 4:
secretCopy = secretCode[:]
guess = getGuess()
print guess
matches = computeExacts(guess, secretCopy)
partial_matches = computePartials(guess,secretCopy)
print 'Exact matches:' + str(matches)
print 'Partial matches:' + str(partial_matches)
print 'Congratulations!! You have beaten Mastermind!'