Created
September 12, 2019 05:09
-
-
Save aambrioso1/7f6aee0799fe6ac2bbd6049b0ddc4843 to your computer and use it in GitHub Desktop.
mastermind.py
This file contains hidden or 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
# We count two things. (1) How many letters are correct but in the wrong position. This is r and the first number in the output. (2) How many letters are correct but in the weon position | |
# Input variables. | |
length = 8 | |
code = 'ABCDEEFF' | |
guess ='AEFFECDZ' | |
# The set of letters in the code. This is used to elimimate repetitions. | |
let = set(code) | |
# Initialize variables for the counting | |
r = 0 | |
s = 0 | |
# The keys of this dictionary are the letters in the code. The values are the letter counts. | |
# We initialize the dictionary. | |
let_dict = dict(zip(let, [0 for i in range(len(let))])) | |
# We set up the letter counts for let_dict. | |
for c in code: | |
let_dict[c] += 1 | |
# We initialize a dictionary to keep track of what letters have been guessed. | |
guessed_let = dict(zip(let, [0 for i in range(len(let))])) | |
# This loop steps through the guessed letters while keeping track of which have been look at, which are in the correct and in the position and which are correct but in the wrong position. | |
for i in range(length): | |
if guess[i] in code: # Is quess in code? | |
if code[i] == guess[i]: # Is position correct. | |
guessed_let[code[i]] += 1 # We used the letter. | |
s += 1 | |
# The code that follows needs work since letters can be repeated in the game code. | |
elif guessed_let[guess[i]] < let_dict[guess[i]]: # Do we still have letters to gueess. | |
guessed_let[guess[i]] += 1 # We used this letter. | |
r += 1 | |
print(r) | |
print(s) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment