-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmastermind.py
61 lines (46 loc) · 1.31 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
import random
print('Create secret code!')
colours = [ 'R', 'Y', 'G', 'B', 'K', 'W' ]
def create_code():
return random.choices(colours, k=4)
def calculate_score(code, guess):
correct = 0
included = 0
indexes = []
for index, element in enumerate(guess):
if code[index] == element:
correct = correct + 1
indexes.append(index)
indexes.reverse()
for index in indexes:
code.pop(index)
guess.pop(index)
for element in guess:
try:
index = code.index(element)
code.pop(index)
included = included + 1
except ValueError:
print
return [correct, included]
# code = ['G', 'W', 'B', 'K']
code = create_code()
print('Code: ', code)
# guess = ['K', 'K', 'W', 'B']
# guess = create_code()
print("Enter four colours separated by spaces. Possible colours are: R, Y, G, B, K, W:")
guess_string = ''
while 1:
guess_string = input('> ')
if guess_string == 'exit':
break
guess = guess_string.split()
print('Guess: ', guess)
score = calculate_score(code.copy(), guess)
if score[0] == 4:
print('You won!')
break
else:
print('Correct pegs: ', score[0])
print('Right colour, wrong position: ', score[1])
print