Skip to content

Commit

Permalink
Merge pull request #322 from Manavalan2517/main
Browse files Browse the repository at this point in the history
Add files via upload
  • Loading branch information
kishanrajput23 authored Jun 10, 2024
2 parents 8e6ad3f + 426988a commit d0163ac
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Memory Game Python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
###

# Memory Game Python Script Breakdown

## 1. Importing Necessary Modules
- **random**: for generating random numbers.
- **tabulate**: for formatting the grid display.
- **os**: for clearing the console.

## 2. Defining Global Variables
- **values**: a dictionary mapping letters to their corresponding indices in the grid.
- **temp_found**: a list to store the numbers temporarily found during each turn.
- **user_values**: a list to store the user's input for each turn.
- **user_choices**: a list to keep track of the tiles already uncovered by the user.
- **progress**: a boolean flag to indicate whether the game is still in progress.
- **competed**: a boolean flag to indicate whether the game has been completed.

## 3. Taking User Input for the Grid Size
- The user is prompted to enter a choice for the grid size (e.g., 2 for a 2x2 grid, 4 for a 4x4 grid).
- The input is validated to ensure it is an even number.

## 4. Defining Functions
- **game_elements(num)**: a function that generates the grid elements. It creates a list of random numbers and shuffles them to create pairs.
- **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.

## 5. The Main Game Loop
- The main list is populated with the grid elements using the `game_elements` function.
- The `game_process` function is called to start the game.
- After each game, the user is prompted to choose whether to play again.
- If the user chooses to quit, the loop breaks.

**Overall, the selected code provides a complete implementation of the memory game with user interaction and grid display.**

###
154 changes: 154 additions & 0 deletions Memory Game Python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import random
import os

values = {'a' : 0, 'b' : 1, 'c' : 2, 'd' : 3, 'e' : 4, 'f' : 5, 'g' : 6, 'h' : 7}
table_heading = {65:"A", 66:"B", 67:"C", 68:"D", 69:"E", 70:"F", 71:"G", 72:"H"}
temp_found = []
user_values = []
user_choices = []
progress = True
competed = False

user_choice_data = input("""
For '2x2' enter "EASY"
For '4x4' enter "MEDIUM"
For '6x6' enter "HARD"
For '8x8' enter "EXTREME"
Enter your choice: """).lower()

while user_choice_data not in ["easy", "medium", "hard", "extreme"]:
os.system("cls")
user_choice_data = input("""
For '2x2' enter "EASY"
For '4x4' enter "MEDIUM"
For '6x6' enter "HARD"
For '8x8' enter "EXTREME"
Enter your choice: """).lower()

def game_elements(num):

lst = []
while len(lst) < num:
row = []
while len(row) < num:
random_number = random.randint(1,10)
if random_number not in row:
row.append(random_number)
lst.append(row)
element = row.copy()
random.shuffle(element)
lst.append(element)
return lst

def game_process(num):
global user_values
global temp_found
global competed

def game_table(list_with_spaces):
char_count = 65
if user_choice_data == 2:
print(" 0 1 ")
print(" |---------|---------|")
for i in range(2):
print(f"{table_heading[char_count]} | {list_with_spaces[i][0]} | {list_with_spaces[i][1]} |")
print(" |---------|---------|")
char_count += 1

if user_choice_data == 4:
print(" 0 1 2 3 ")
print(" |---------|---------|---------|---------|")
for i in range(4):
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]} |")
print(" |---------|---------|---------|---------|")
char_count += 1


if user_choice_data == 6:
print(" 0 1 2 3 4 5 ")
print(" |-------|-------|-------|-------|-------|-------|")
for i in range(6):
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]} |")
print(" |-------|-------|-------|-------|-------|-------|")
char_count += 1


if user_choice_data == 8:
print(" 0 1 2 3 4 5 6 7 ")
print(" |-------|-------|-------|-------|-------|-------|-------|-------|")
for i in range(8):
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]} |")
print(" |-------|-------|-------|-------|-------|-------|-------|-------|")
char_count += 1


list_with_spaces = [[' ' for _ in sublist] for sublist in main]
while progress:
os.system('cls')
game_table(list_with_spaces)

trial = 0
for i in range(2):
if len(user_choices) == (num**2):
competed = True
break

opn = input("\nEnter the tile to uncover: ")
while opn in user_choices:
print("\n===========>>>You have already completed this tile!")
opn = input("\nEnter the new tile to uncover: ")
user_values.append(opn)

index_1 = values[opn[0]]
index_2 = int(opn[1])

temp_found.append(main[index_1][index_2])

list_with_spaces[index_1][index_2] = main[index_1][index_2]
game_table(list_with_spaces)

trial += 1
if trial == 2:
user_input = input("\n===========>>> Press (Enter) to Continue: ")
trial = 0

if competed:
print("\n===========>>> You Won! <<<===========")
break
elif temp_found[0] == temp_found[1]:
for i in range(2):
user_choices.append(user_values[i])
index_1 = values[user_values[i][0]]
index_2 = int(user_values[i][1])
list_with_spaces[index_1][index_2] = "✔️"
user_values = []
temp_found = []
else:
for i in range(2):
index_1 = values[user_values[i][0]]
index_2 = int(user_values[i][1])
list_with_spaces[index_1][index_2] = " "
user_values = []
temp_found = []
while True:
if user_choice_data == "easy":
user_choice_data = 2
if user_choice_data == "medium":
user_choice_data = 4
if user_choice_data == "hard":
user_choice_data = 6
if user_choice_data == "extreme":
user_choice_data = 8
main = game_elements(user_choice_data)
process = game_process(user_choice_data)

choice = input("Do you want to play the game again? (y/n): ").lower()

while choice not in ["y", "n"]:
print("\n Invalid choice")
choice = input("Do you want to play the game again? (y/n): ")

if choice == "n":
break

0 comments on commit d0163ac

Please sign in to comment.