GUI Programming With Python - Bulls and Cows - Mastermind in Tkinter
GUI Programming With Python - Bulls and Cows - Mastermind in Tkinter
The Algorithm is explained in detail in our chapter "Mastermind / Bulls and Cows" in Advanced Topics. You
can also find the the code for the module combinatorics.
def answer_ok(a):
""" checking of an evaulation given by the human player makes
sense. 3 blacks and 1 white make no sense for example. """
(rightly_positioned, permutated) = a
if (rightly_positioned + permutated > number_of_positions) \
or (rightly_positioned + permutated < len(colours) -
number_of_positions):
return False
if rightly_positioned == 3 and permutated == 1:
return False
return True
http://www.python-course.eu/tkinter_mastermind.php 1/4
10/7/2014 GUI Programming with Python: Bulls and Cows / Mastermind in Tkinter
def get_evaluation():
""" get evaluation from entry fields """
rightly_positioned = int(entryWidget_both.get())
permutated = int(entryWidget_only_colours.get())
return (rightly_positioned, permutated)
def new_evaluation(current_colour_choices):
""" This funtion gets an evaluation of the current guess, checks
the consistency of this evaluation, adds the guess together with
the evaluation to the list of guesses, shows the previous guesses
and creates a ne guess """
rightly_positioned, permutated = get_evaluation()
if rightly_positioned == number_of_positions:
return(current_colour_choices, (rightly_positioned,
permutated))
current_colour_choices = create_new_guess()
show_current_guess(current_colour_choices)
if not current_colour_choices:
return(current_colour_choices, (-1, permutated))
return(current_colour_choices, (rightly_positioned, permutated))
def create_new_guess():
""" a new guess is created, which is consistent to the
previous guesses """
next_choice = next(permutation_iterator)
while inconsistent(next_choice, guesses):
try:
next_choice = next(permutation_iterator)
except StopIteration:
print("Error: Your answers were inconsistent!")
return ()
return next_choice
def new_evaluation_tk():
global current_colour_choices
res = new_evaluation(current_colour_choices)
current_colour_choices = res[0]
def show_current_guess(new_guess):
row = 1
http://www.python-course.eu/tkinter_mastermind.php 2/4
10/7/2014 GUI Programming with Python: Bulls and Cows / Mastermind in Tkinter
Label(root, text=" New Guess: ").grid(row=row,
column=0,
columnspan=4)
row +=1
col_count = 0
for c in new_guess:
print(c)
l = Label(root, text=" ", bg=c)
l.grid(row=row,column=col_count, sticky=W, padx=2)
col_count += 1
def view_guesses():
row = 3
Label(root, text="Old Guesses").grid(row=row,
column=0,
columnspan=4)
Label(root, text="c&p").grid(row=row,
padx=5,
column=number_of_positions + 1)
Label(root, text="p").grid(row=row,
padx=5,
column=number_of_positions + 2)
# dummy label for distance:
Label(root, text=" ").grid(row=row,
column=number_of_positions +
3)
row += 1
# vertical dummy label for distance:
Label(root, text=" ").grid(row=row,
column=0,
columnspan=5)
if __name__ == "__main__":
colours = ["red","green","blue","yellow","orange","pink"]
guesses = []
number_of_positions = 4
row_offset = 1
root = Tk()
root.title("Mastermind")
root["padx"] = 30
root["pady"] = 20
http://www.python-course.eu/tkinter_mastermind.php 3/4
10/7/2014 GUI Programming with Python: Bulls and Cows / Mastermind in Tkinter
entryLabel = Label(root)
entryLabel["text"] = "Completely Correct:"
entryLabel.grid(row=row_offset,
sticky=E,
padx=5,
column=number_of_positions + 4)
entryWidget_both = Entry(root)
entryWidget_both["width"] = 5
entryWidget_both.grid(row=row_offset, column=number_of_positions +
5)
entryLabel = Label(root)
entryLabel["text"] = "Wrong Position:"
entryLabel.grid(row=row_offset+1,
sticky=E,
padx=5,
column= number_of_positions + 4)
entryWidget_only_colours = Entry(root)
entryWidget_only_colours["width"] = 5
entryWidget_only_colours.grid(row=row_offset+1,
column=number_of_positions + 5)
root.mainloop()
© 2011 - 2014 Bernd Klein, Bodenseo; Design by Denise Mitchinson adapted for python-course.eu by Bernd Klein
http://www.python-course.eu/tkinter_mastermind.php 4/4