forked from adrianeyre/codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMastermind.py
More file actions
57 lines (54 loc) · 2.06 KB
/
Mastermind.py
File metadata and controls
57 lines (54 loc) · 2.06 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
50
51
52
53
54
55
56
57
import random
def mastermind(game):
colours = ["Red", "Blue", "Green", "Orange", "Purple", "Yellow"]
amount = {"Red": 0, "Blue": 0, "Green": 0, "Orange": 0, "Purple": 0, "Yellow": 0, "": 0}
guess = []
for colour in colours:
answer = game.check([colour, colour, colour, colour])
colour_amount = answer.count("Black")
amount[colour] = colour_amount
for a in range(0,colour_amount):
guess.append(colour)
answer = False
while answer != "WON!" and answer != "Error":
answer = game.check(random.sample(guess, len(guess)))
import random
class MasterMind():
def __init__(self):
self.colours = ["Red", "Blue", "Green", "Orange", "Purple", "Yellow"]
self.goes = 0
self.result = []
for a in range(1,5):
self.result.append(self.colours[random.randint(0,len(self.colours)-1)])
self.amount = {"Red": 0, "Blue": 0, "Green": 0, "Orange": 0, "Purple": 0, "Yellow": 0, "": 0}
for colour in self.result:
self.amount[colour] += 1
def check(self, attempt):
self.goes += 1
if len(attempt) != 4: return ["Error"]
if self.goes > 60: return ["Error"]
if attempt == self.result:
return "WON!"
amount = {"Red": 0, "Blue": 0, "Green": 0, "Orange": 0, "Purple": 0, "Yellow": 0, "": 0}
for colour in self.amount:
amount[colour] = self.amount[colour]
result = []
new_attempt = []
for colour in attempt:
new_attempt.append(colour)
index = -1
for colour in attempt:
index += 1
if colour not in self.colours:
return "Error"
if colour == self.result[index]:
result.append("Black")
amount[colour] -= 1
new_attempt[index] = ""
for colour in new_attempt:
if amount[colour] > 0:
result.append("White")
amount[colour] -= 1
return random.sample(result, len(result))
game = MasterMind()
mastermind(game)