forked from souravjain540/Basic-Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory-puzzle.py
More file actions
66 lines (53 loc) · 2.08 KB
/
Memory-puzzle.py
File metadata and controls
66 lines (53 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import random
def generate_board(size=4):
if size % 2 != 0:
raise ValueError("Size must be even")
letters = [chr(i) for i in range(65, 65 + (size * size // 2))]
board = letters + letters
random.shuffle(board)
return [board[i:i + size] for i in range(0, len(board), size)]
def print_board(board, revealed):
for row_idx, row in enumerate(board):
for col_idx, cell in enumerate(row):
if revealed[row_idx][col_idx]:
print(cell, end=' ')
else:
print('_', end=' ')
print()
def get_choice(board):
while True:
try:
row, col = map(int, input("Enter row and column (e.g. '1 2'): ").split())
if row >= 1 and row <= len(board) and col >= 1 and col <= len(board[0]):
return row - 1, col - 1
else:
print("Invalid choice. Try again.")
except ValueError:
print("Invalid input. Enter row and column numbers separated by space.")
def memory_puzzle():
board = generate_board()
revealed = [[False] * len(board) for _ in range(len(board))]
moves = 0
while not all(all(row) for row in revealed):
print_board(board, revealed)
print("Choose the first card to flip.")
row1, col1 = get_choice(board)
revealed[row1][col1] = True
print_board(board, revealed)
print("Choose the second card to flip.")
row2, col2 = get_choice(board)
if (row1, col1) == (row2, col2):
print("You chose the same card. Choose a different second card.")
continue
revealed[row2][col2] = True
print_board(board, revealed)
if board[row1][col1] != board[row2][col2]:
print("Not a match.")
revealed[row1][col1] = False
revealed[row2][col2] = False
else:
print("It's a match!")
moves += 1
print(f"Congratulations! You completed the game in {moves} moves.")
if __name__ == "__main__":
memory_puzzle()