- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMasterMind.py
More file actions
Latest commit
49 lines (44 loc) · 1.47 KB
/
Copy pathMasterMind.py
File metadata and controls
49 lines (44 loc) · 1.47 KB
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
'''
MasterMind game:
two player. codemaker,codebreaker
codemaker make 4 numbers, coderbreaker guess up to 12 times
codemaker will give codercreaker how many digit are right number in right position
and how many digit are right number in wrong position
'''
ROUNDMAX=12# the maxium number of rounds for the game
# return the map of the codebreaker's guess
# how many numbers are right and in the right position
# how many numbers are right but in the right position
defResult(code,guessCode):
res= {
'RightNumRightPos':0,
'RightNumWrongPos':0
}
# build code map : key - digit , value - index
codeMap= {}
foriinrange(len(code)):
codeMap[code[i]] =i
iflen(guessCode) >4 :
return'wrong number'
else:
forguessinguessCode :
ifguessincodeMap:
ifcodeMap[guess] ==guessCode.index(guess):
res['RightNumRightPos'] +=1
else:
res['RightNumWrongPos'] +=1
returnres
# main loop for the game
defMasterMind(code):
round=ROUNDMAX
whileround>=0 :
guessCode=str(raw_input('guess the code \n'))
res=Result(code,guessCode)
RightNumRightPos=res['RightNumRightPos']
RightNumWrongPos=res['RightNumWrongPos']
print'Right number in right position: %d Right number in wrong position: %d'% (RightNumRightPos, RightNumWrongPos)
ifRightNumRightPos==4:
return'Congrats You Win'
round-=1
return'Sorry you lose, the real number is %s'%code
printMasterMind('1234')