Created
November 23, 2016 13:02
-
-
Save JellyWX/6bdee7d780d8fc1c636c0bfb1896bc12 to your computer and use it in GitHub Desktop.
a python mastermind game
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
from random import randint | |
from sys import stdout | |
correct = [randint(1,8), randint(1,8), randint(1,8), randint(1,8)] | |
usr_guess = [0, 0, 0, 0] | |
usr_guess_output = [] | |
usr_guess_output_n = [] | |
guesses = 0 | |
print('Welcome to Mastermind. Guess the combination of numbers between 1 and 8. If you get the correct number and place, a \'*\' will be printed. If you get the correct number but wrong place, a \'-\' will be printed. If you get it wrong completely, a \'#\' will be printed. The position of the output does not correlate to the positions in the actual list of numbers.') | |
while(True): | |
usr_guess_output_n = [] | |
usr_guess_output = [] | |
correct_count = 0 | |
guesses += 1 | |
# try: #Makes sure that the program still works even if the user messes up the input | |
usr_guess = raw_input('Guess at the numbers (separate with commas) > ').split(',') #Splits the user input into a list of integers | |
usr_guess = [int(x) for x in usr_guess ] #Converts all list items into integers for comparisons | |
#print(str(usr_guess)) --debug code-- | |
#print(str(correct)) --debug code-- | |
print('') | |
i = 0 | |
for i in range(0,4): #Iterates the lists to check for comparisons | |
if correct[i] == usr_guess[i]: | |
usr_guess_output.append('*') | |
correct_count += 1 | |
elif correct[i] in usr_guess: | |
usr_guess_output.append('-') | |
else: | |
usr_guess_output.append('#') | |
if(correct_count > 3): | |
break | |
for i in usr_guess_output: | |
if i == '*': | |
usr_guess_output_n.append('*') | |
for i in usr_guess_output: | |
if i == '-': | |
usr_guess_output_n.append('-') | |
for i in usr_guess_output: | |
if i == '#': | |
usr_guess_output_n.append('#') | |
print(str(usr_guess_output_n).replace(',','').replace('[','').replace(']','').replace('\'','')) | |
# except: | |
# print('something went wrong. you probably input something other than an integer') | |
# guesses -= 1 | |
print('\nWell done! It took you ' + str(guesses) + ' guesses!') | |
raw_input('Press enter to exit') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello
I must point out that the output of that code is not quite right.
There is a bug that makes some responses not accurate.
Imagine this:
correct = (8,6,6,5)
you guess is (8,8,6,5)
the response should be: * * * #
but it is: * * * -
The problemis in line 36 of the code. Your code check if the next number form "correct" list is in whole "usr_guess" list.
The problem could occur when "correct" list will contain more than one occurrence of some number.
In exaple above program will check:
8 <=> 8 = True >> print ("")
6 <=> 8 >> program goes to elif >> 6 in (8,8,6,5) = True >> print ("-") #the 6 is in the "usr_guess" list, only one time but it is.
6 <=> 6 = True >> print ("")
5 <=> 5 = True >> print ("*")
I do not know best sollution for this problem. I have some idea how to solve this issue, but my sollution require a lot of code and is not simple to explain so it is probably not the best idea ;-)