Skip to content

Commit 426988a

Browse files
Add files via upload
1 parent 8e6ad3f commit 426988a

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed

Memory Game Python/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
###
2+
3+
# Memory Game Python Script Breakdown
4+
5+
## 1. Importing Necessary Modules
6+
- **random**: for generating random numbers.
7+
- **tabulate**: for formatting the grid display.
8+
- **os**: for clearing the console.
9+
10+
## 2. Defining Global Variables
11+
- **values**: a dictionary mapping letters to their corresponding indices in the grid.
12+
- **temp_found**: a list to store the numbers temporarily found during each turn.
13+
- **user_values**: a list to store the user's input for each turn.
14+
- **user_choices**: a list to keep track of the tiles already uncovered by the user.
15+
- **progress**: a boolean flag to indicate whether the game is still in progress.
16+
- **competed**: a boolean flag to indicate whether the game has been completed.
17+
18+
## 3. Taking User Input for the Grid Size
19+
- The user is prompted to enter a choice for the grid size (e.g., 2 for a 2x2 grid, 4 for a 4x4 grid).
20+
- The input is validated to ensure it is an even number.
21+
22+
## 4. Defining Functions
23+
- **game_elements(num)**: a function that generates the grid elements. It creates a list of random numbers and shuffles them to create pairs.
24+
- **game_process(num)**: a function that handles the game logic. It displays the grid, prompts the user for input, and updates the grid based on the user's choices. It also checks for matches and updates the game status accordingly.
25+
26+
## 5. The Main Game Loop
27+
- The main list is populated with the grid elements using the `game_elements` function.
28+
- The `game_process` function is called to start the game.
29+
- After each game, the user is prompted to choose whether to play again.
30+
- If the user chooses to quit, the loop breaks.
31+
32+
**Overall, the selected code provides a complete implementation of the memory game with user interaction and grid display.**
33+
34+
###

Memory Game Python/main.py

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import random
2+
import os
3+
4+
values = {'a' : 0, 'b' : 1, 'c' : 2, 'd' : 3, 'e' : 4, 'f' : 5, 'g' : 6, 'h' : 7}
5+
table_heading = {65:"A", 66:"B", 67:"C", 68:"D", 69:"E", 70:"F", 71:"G", 72:"H"}
6+
temp_found = []
7+
user_values = []
8+
user_choices = []
9+
progress = True
10+
competed = False
11+
12+
user_choice_data = input("""
13+
For '2x2' enter "EASY"
14+
For '4x4' enter "MEDIUM"
15+
For '6x6' enter "HARD"
16+
For '8x8' enter "EXTREME"
17+
18+
Enter your choice: """).lower()
19+
20+
while user_choice_data not in ["easy", "medium", "hard", "extreme"]:
21+
os.system("cls")
22+
user_choice_data = input("""
23+
For '2x2' enter "EASY"
24+
For '4x4' enter "MEDIUM"
25+
For '6x6' enter "HARD"
26+
For '8x8' enter "EXTREME"
27+
28+
Enter your choice: """).lower()
29+
30+
def game_elements(num):
31+
32+
lst = []
33+
while len(lst) < num:
34+
row = []
35+
while len(row) < num:
36+
random_number = random.randint(1,10)
37+
if random_number not in row:
38+
row.append(random_number)
39+
lst.append(row)
40+
element = row.copy()
41+
random.shuffle(element)
42+
lst.append(element)
43+
return lst
44+
45+
def game_process(num):
46+
global user_values
47+
global temp_found
48+
global competed
49+
50+
def game_table(list_with_spaces):
51+
char_count = 65
52+
if user_choice_data == 2:
53+
print(" 0 1 ")
54+
print(" |---------|---------|")
55+
for i in range(2):
56+
print(f"{table_heading[char_count]} | {list_with_spaces[i][0]} | {list_with_spaces[i][1]} |")
57+
print(" |---------|---------|")
58+
char_count += 1
59+
60+
if user_choice_data == 4:
61+
print(" 0 1 2 3 ")
62+
print(" |---------|---------|---------|---------|")
63+
for i in range(4):
64+
print(f"{table_heading[char_count]} | {list_with_spaces[i][0]} | {list_with_spaces[i][1]} | {list_with_spaces[i][2]} | {list_with_spaces[i][3]} |")
65+
print(" |---------|---------|---------|---------|")
66+
char_count += 1
67+
68+
69+
if user_choice_data == 6:
70+
print(" 0 1 2 3 4 5 ")
71+
print(" |-------|-------|-------|-------|-------|-------|")
72+
for i in range(6):
73+
print(f"{table_heading[char_count]} | {list_with_spaces[i][0]} | {list_with_spaces[i][1]} | {list_with_spaces[i][2]} | {list_with_spaces[i][3]} | {list_with_spaces[i][4]} | {list_with_spaces[i][5]} |")
74+
print(" |-------|-------|-------|-------|-------|-------|")
75+
char_count += 1
76+
77+
78+
if user_choice_data == 8:
79+
print(" 0 1 2 3 4 5 6 7 ")
80+
print(" |-------|-------|-------|-------|-------|-------|-------|-------|")
81+
for i in range(8):
82+
print(f"{table_heading[char_count]} | {list_with_spaces[i][0]} | {list_with_spaces[i][1]} | {list_with_spaces[i][2]} | {list_with_spaces[i][3]} | {list_with_spaces[i][4]} | {list_with_spaces[i][5]} | {list_with_spaces[i][6]} | {list_with_spaces[i][7]} |")
83+
print(" |-------|-------|-------|-------|-------|-------|-------|-------|")
84+
char_count += 1
85+
86+
87+
list_with_spaces = [[' ' for _ in sublist] for sublist in main]
88+
while progress:
89+
os.system('cls')
90+
game_table(list_with_spaces)
91+
92+
trial = 0
93+
for i in range(2):
94+
if len(user_choices) == (num**2):
95+
competed = True
96+
break
97+
98+
opn = input("\nEnter the tile to uncover: ")
99+
while opn in user_choices:
100+
print("\n===========>>>You have already completed this tile!")
101+
opn = input("\nEnter the new tile to uncover: ")
102+
user_values.append(opn)
103+
104+
index_1 = values[opn[0]]
105+
index_2 = int(opn[1])
106+
107+
temp_found.append(main[index_1][index_2])
108+
109+
list_with_spaces[index_1][index_2] = main[index_1][index_2]
110+
game_table(list_with_spaces)
111+
112+
trial += 1
113+
if trial == 2:
114+
user_input = input("\n===========>>> Press (Enter) to Continue: ")
115+
trial = 0
116+
117+
if competed:
118+
print("\n===========>>> You Won! <<<===========")
119+
break
120+
elif temp_found[0] == temp_found[1]:
121+
for i in range(2):
122+
user_choices.append(user_values[i])
123+
index_1 = values[user_values[i][0]]
124+
index_2 = int(user_values[i][1])
125+
list_with_spaces[index_1][index_2] = "✔️"
126+
user_values = []
127+
temp_found = []
128+
else:
129+
for i in range(2):
130+
index_1 = values[user_values[i][0]]
131+
index_2 = int(user_values[i][1])
132+
list_with_spaces[index_1][index_2] = " "
133+
user_values = []
134+
temp_found = []
135+
while True:
136+
if user_choice_data == "easy":
137+
user_choice_data = 2
138+
if user_choice_data == "medium":
139+
user_choice_data = 4
140+
if user_choice_data == "hard":
141+
user_choice_data = 6
142+
if user_choice_data == "extreme":
143+
user_choice_data = 8
144+
main = game_elements(user_choice_data)
145+
process = game_process(user_choice_data)
146+
147+
choice = input("Do you want to play the game again? (y/n): ").lower()
148+
149+
while choice not in ["y", "n"]:
150+
print("\n Invalid choice")
151+
choice = input("Do you want to play the game again? (y/n): ")
152+
153+
if choice == "n":
154+
break

0 commit comments

Comments
 (0)