
- 14th Dec 2023
- 20:15 pm
A memory puzzle game typically involves revealing pairs of matching cards within a grid and testing the player's memory to match all pairs. Let's create a simple command-line version of a memory puzzle game using Python.
import random
def create_board(size):
# Create a board with pairs of numbers
numbers = list(range(1, size // 2 + 1)) * 2
random.shuffle(numbers)
board = [numbers[i:i+size // 2] for i in range(0, size, size // 2)]
return board
def display_board(board, revealed):
for row in range(len(board)):
for col in range(len(board[0])):
if revealed[row][col]:
print(board[row][col], end=' ')
else:
print('*', end=' ')
print()
def play_game():
size = 4 # Adjust the grid size here (even numbers for pairs)
board = create_board(size)
revealed = [[False for _ in range(size // 2)] for _ in range(size // 2)]
attempts = 0
matched_pairs = 0
while matched_pairs < size // 2:
print("\nMemory Puzzle Game")
display_board(board, revealed)
guess1_row, guess1_col = map(int, input("\nEnter row and column of first card (e.g., 1 2): ").split())
guess2_row, guess2_col = map(int, input("Enter row and column of second card (e.g., 2 3): ").split())
if (guess1_row == guess2_row and guess1_col == guess2_col) or \
revealed[guess1_row-1][guess1_col-1] or revealed[guess2_row-1][guess2_col-1]:
print("Invalid input or card already revealed. Try again.")
continue
if board[guess1_row-1][guess1_col-1] == board[guess2_row-1][guess2_col-1]:
print("You found a match!")
revealed[guess1_row-1][guess1_col-1] = True
revealed[guess2_row-1][guess2_col-1] = True
matched_pairs += 1
else:
print("Try again!")
attempts += 1
print(f"\nCongratulations! You've completed the game in {attempts} attempts.")
if __name__ == "__main__":
play_game()
This basic example creates a memory puzzle game using a 4x4 grid. Players input coordinates of the cards they want to reveal. If the cards match, they remain revealed; otherwise, they are hidden again. The game continues until all pairs are found.
About the Author - Jane Austin
Jane Austin is a 24-year-old programmer specializing in Java and Python. With a strong foundation in these programming languages, she possesses expertise in software development and application design. Jane has a knack for problem-solving and thrives in collaborative environments, where she contributes her skills to develop efficient and innovative solutions.