Last active
June 25, 2017 10:57
-
-
Save gph03n1x/a5aa477c4e4abb4b9bc8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'John' | |
import random | |
colors = { | |
'B': 'Blue', | |
'R': 'Red', | |
'Y': 'Yellow', | |
'G': 'Green', | |
'W': 'White', | |
'B': 'Black' | |
} | |
class Mastermind(object): | |
def generate_random_code(self): | |
return [random.choice(colors.keys()) for number in range(1, 5)] | |
def __init__(self, tries): | |
self.code = self.generate_random_code() | |
self.tries = tries | |
def set_code(self, code): | |
self.code = code.split() | |
def guess(self, attempt): | |
self.decode_attempt = attempt.split() | |
self.correct_spot, self.correct_colors = 0, 0 | |
self.code_copy = self.code | |
for part in range(1, 5): # Correct spots | |
if self.code[part] == self.decode_attempt[part]: | |
self.correct_spot = self.correct_spot + 1 | |
for code_part in self.decode_attempt: | |
if code_part in self.code_copy: | |
self.correct_colors = self.correct_colors + 1 | |
self.code_copy.remove(code_part) | |
self.tries = self.tries - 1 | |
def check_for_loss_or_win(self): | |
if self.correct_spot == 4: | |
print "you won" | |
elif self.tries == 0: | |
print "you lost" | |
mastermind = Mastermind(12) | |
#print mastermind.code, mastermind.tries |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment