Created
September 12, 2019 05:24
-
-
Save aambrioso1/7d4e7d4aa39f176f8979bb031cf2f088 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 wrong position. This is s and the second number output. | |
# Input variables. | |
length = 4 | |
code = 'BACC' | |
guess ='CABB' | |
# 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. | |
r += 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. | |
s += 1 | |
# Output the counts. | |
print(r, s) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment