|
@@ -0,0 +1,50 @@
|
|
|
+import random
|
|
|
+
|
|
|
+
|
|
|
+COLORS = {
|
|
|
+ 'R': 'red',
|
|
|
+ 'G': 'green',
|
|
|
+ 'B': 'blue',
|
|
|
+ 'Y': 'yellow',
|
|
|
+ 'O': 'orange',
|
|
|
+ 'P': 'purple',
|
|
|
+}
|
|
|
+SLOTS = 4
|
|
|
+
|
|
|
+
|
|
|
+class Mastermind():
|
|
|
+ riddle: list
|
|
|
+ guesses: list
|
|
|
+
|
|
|
+ def __init__(self):
|
|
|
+ self.reset()
|
|
|
+
|
|
|
+ def guess(self, combination: list):
|
|
|
+ self.guesses.append(combination)
|
|
|
+ black = len([1 for c, r in zip(combination, self.riddle) if c == r])
|
|
|
+ white = -black
|
|
|
+ for c in COLORS.keys():
|
|
|
+ white += max(combination.count(c) - self.riddle.count(c), 0)
|
|
|
+ return (black, white)
|
|
|
+
|
|
|
+ def reset(self):
|
|
|
+ self.riddle = random.choices(list(COLORS.keys()), k=SLOTS)
|
|
|
+ self.guesses = []
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ mm = Mastermind()
|
|
|
+ print("Colors:")
|
|
|
+ for k, c in COLORS.items():
|
|
|
+ print(" " + k + ": " + c)
|
|
|
+ print("")
|
|
|
+
|
|
|
+ while True:
|
|
|
+ guess = input("What's your guess? ")
|
|
|
+ res = mm.guess(list(guess))
|
|
|
+ print(res)
|
|
|
+ if res[0] == 4:
|
|
|
+ print("That's right! Congratulations!")
|
|
|
+ mm.reset()
|