0% found this document useful (0 votes)
28 views

Programming Fundamentals Report

Programming Fundamentals Report

Uploaded by

hibaaltafwork3
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Programming Fundamentals Report

Programming Fundamentals Report

Uploaded by

hibaaltafwork3
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

TABLE OF CONTENTS

project summary........................................................................................1
algorithm..................................................................................................2
project flow...............................................................................................3
code explanation........................................................................................4
screenshots...............................................................................................5
PROJECT SUMMARY
this code is a gui-based implementation of the classic mastermind game using
the tkinter library in python. the game is a code-breaking challenge where the
player has to guess a sequence of four colors (red, blue, green, and yellow) in
the correct order.

here's a detailed breakdown of the code:

1. the first section imports the necessary libraries, including tkinter for the gui,
random for generating the secret code, and tkinter.messagebox for displaying
messages.

2. the check function is called when the player clicks the "check" button. it
compares the player's current guess with the secret code and updates the
number of chances left and the number of correct choices.

3. the startgame function sets up the game window, generates a new secret
code, and initializes the player's choices and chances.

4. the main window has a title, a geometry of 700x500 pixels, and a gray
background. it displays a label with instructions, four sets of radio buttons for the
player to make their choices, labels to display the number of chances left and the
number of correct choices, and a "check" button to submit the guess.

5. when the player clicks the "start game" button, the startgame function is
called, and a new game window is opened with the game setup.

6. the game continues until the player guesses the correct sequence or runs out
of chances. if the player wins, a congratulatory message is displayed. if they
lose, the correct answer is revealed.

the code is designed to create an engaging and interactive game environment for
the player to enjoy the classic mastermind game.

1
GITHUB LINK

GITHUB - ALIX80/SNAKEGAME_01

2
ALGORITHM
### complete algorithm of the mastermind game code

here's a step-by-step explanation of the algorithm implemented in the provided


code for the mastermind game:

#### initialization

1. *import libraries*
- import necessary libraries (tkinter, random, tkinter.messagebox).

2. *global variables initialization*


- define global variables: n (number of chances), answer (secret code), ch1,
ch2, ch3, ch4 (variables to store player's guesses), and correct (number of
correct guesses).

#### check function (check)

3. *check function definition*


- define the check function to compare the player's guess with the secret code.

4. *initialize count*
- initialize count to zero.

5. *compare guesses*
- compare each of the player's guesses (ch1, ch2, ch3, ch4) with the
corresponding values in the secret code (answer).
- increment count for each correct match.

6. *get remaining chances*


- retrieve the remaining chances from n.

7. *print guesses and count*


- print the player's current guesses and the count of correct guesses for
debugging purposes.

8. *check and update chances*


- if chances are greater than zero:
- update the correct variable with the count of correct guesses.
- if all four guesses are correct (count == 4):
- display a congratulatory message using tkinter.messagebox.showinfo.
- set remaining chances to zero.
- decrease the remaining chances by one.
- update the n variable with the new number of chances.

3
- if chances are zero or less:
- decrease the remaining chances by one.
- if chances are still less than or equal to zero:
- generate the correct answer in text form and display it using
tkinter.messagebox.showinfo.
- update the n variable with the new number of chances.

#### start game function (startgame)

9. *start game function definition*


- define the startgame function to initialize and start a new game.

10. *create new window*


- create a new tkinter window (wn).

11. *set window properties*


- set the window title, size, and background color.

12. *initialize game variables*


- reset the number of chances (n) to 5.
- initialize correct to zero.
- initialize ch1, ch2, ch3, ch4 to store player's guesses.
- generate a random secret code (answer) with four numbers (each between 1
and 4).

13. *print secret code*


- print the secret code for debugging purposes.

14. *create gui components*


- create a label to instruct the player to make their choice.
- create radio buttons for each color option (red, blue, green, yellow) for each
of the four guess positions (ch1, ch2, ch3, ch4).
- create labels to display the remaining chances and the number of correct
guesses.
- create a "check" button to submit the player's guess, which triggers the
check function when clicked.

15. *run the main loop*


- run the main event loop (wn.mainloop()) to display the window and handle
user interactions.

#### main gui window

16. *create main window*

4
- create the main window (wn) for the application.

17. *set main window properties*


- set the window title, size, and background color.

18. *initialize variables*


- initialize n to store the number of chances.
- initialize correct to store the number of correct guesses.

19. *create instructions label*


- create a label to display instructions to the player.

20. *create start game button*


- create a "start game" button that triggers the startgame function when
clicked.

21. *run the main loop*


- run the main event loop (wn.mainloop()) to display the main window and
handle user interactions.

### summary

- the script initializes the gui components and sets up the game environment.
- the check function handles the logic for comparing the player's guesses with
the secret code, updating the number of chances, and providing feedback.
- the startgame function initializes a new game, generates a random secret code,
and sets up the game-specific gui components.
- the main gui window provides an interface to start the game and displays the
instructions.

5
PROJECT FLOW

start

Display Start Window

Start Game Button | | Pressed

Initialize Game

Game Loop

Check if Game Running (If no,


go to Game Over)

6
Move Snake

Collision Check |(If


,
collision go to

Game Over)

Food Check | (If


food eaten , increase
create new food)

Redraw

Game Over

Display Game Over Window

Save and Display High Scores

7
End

CODE EXPLANATION
of course, let's delve a bit deeper into each function:

1. `generate_code`: this function is responsible for generating the secret code


that the player needs to guess. it does this by randomly selecting colors from a
predefined list (`self.colors`) for a specified number of times
(`self.code_length`). the result is a list representing the secret code.

2. `check_guess`: when the player submits their guess, this function evaluates it
against the secret code. it iterates through each position in the guess and
compares it with the corresponding position in the secret code. it counts the
number of exact matches (when the color in the guess matches the color in the
same position in the secret code) and color matches (when a color in the guess
appears elsewhere in the secret code but not in the same position). the feedback
is then updated with the counts of exact and color matches. if the player's guess
matches the secret code entirely, it triggers a win message.

3. `reset_game`: this function is responsible for resetting the game state to its
initial state. it generates a new secret code for the player to guess, resets the
number of attempts to 0, resets the color selection to the default color for each
position, and clears the feedback label to provide a clean slate for the player to
start a new game.

4. `create_widgets`: this function constructs the graphical user interface (gui)


elements of the game using the tkinter library. it sets up the code frame where
the player makes their guesses, creates option menus for selecting colors for
each position in the guess, adds buttons for submitting guesses and resetting the
game, and includes a label to display feedback to the player about the accuracy
of their guesses. these gui elements are crucial for the player to interact with the
game effectively.

8
SCREENSHOTS

9
10
11
12

You might also like