Project Part 7 Working Procedures
1. Function check_result()
a) Two variables have to be initialized for the numbers of black peg and white peg as:
black_peg = 0 white_peg = 0
b) Then, use a for-loop and if statement to check whether the 3 guess images are the same as the chosen images one by one. The 3 guess images are represented by the list variable guess_image and 3 chosen images are represented by the list variable chosen_image.
c) For example, if the first element of the list variable guess_image is the same as the first element of the list variable chosen_image, then the variable black_peg will be increased by one. This indicates a correct guess in both the image and its position.
black_peg += 1
d) For the number of white peg, you have to figure out yourself how to get the correct number of white peg. It supposed to be the most demanding and challenging part in this project.
e) After the numbers of black peg and white peg are ready, a for-loop will be used to show the black peg by using a new Label widget with attribute bg=black as:
Label(main_window, width=1, height=1, relief=RAISED, bg=black).grid()
f) You may make use of the variable attempt for the row index of the grid layout of the Label
widget above. For the column index, you may use the counter from the for-loop.
g) After the display of black peg, a for-loop will be used to show the white peg by using a new Label widget with attribute bg=white as:
Label(main_window, width=1, height=1, relief=RAISED, bg=white).grid()
h) You may make use of the variable attempt for the row index of the grid layout of the Label widget above. For the column index, you may use the counter from the for-loop and the number of back peg. You may refer to image 1 below.
i) After the 4 Button widgets to show the guess images in the corresponding row and the O.K. button, an if statement should be used to test whether the number of black peg is 3 or not. If it is 3, then use a message box to congratulate the player as (Refer to images 2 & 3 below):
messagebox.showinfo(Mastermind, Congratulation! You win in +str(attempt) + attempt(s).)
j) After the statement to show message box, the function show_chosen_image() should be excuted to show the 3 chosen images at the top and at the same time, disable all the Button widgets.
show_chosen_image()
2. Window Menu
You may refer to Notes chapter 11 pages 163 to 168. You may refer to images 4 to 7 below.
Image 1
Image 2
Image 3
Image 4
Image 5
Image 6
Image 7
Program Structure
a) Up to now, the structure of the program is:
Import statements (3 statements)
Window definition statements (5 statements)
Functions definition (16 functions)
Menu definition statements (10 statements)
Main body of program
b) The flow of functions is:
new_board()
New game Exit
How to play About this game
check_result()
show_chosen_image()
how_to_play() about_this_game()
Variable definition statements (9 statements)
Function calling statements (2 statements)
8 functions for the Images available buttons image_0() to image_7()
3 functions for the Guess buttons guess_image_1() to guess_image_3()
Menu Options
Reviews
There are no reviews yet.