-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.py
161 lines (133 loc) · 5.67 KB
/
grid.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import random
import sys # for sys.exit()
import os
import time
class PeekABooGrid:
def __init__(self, grid_size):
self.grid_size = grid_size
self.grid, self.pairs = self.createInitialGrid()
self.num_pairs = grid_size * grid_size // 2
self.uncovered_coordinates = set()
self.found_pairs = 0
self.total_guesses = 0
self.revealed_elements = 0
def createInitialGrid(self):
grid = [] # empty list to hold the rows
pairs = list(range(self.grid_size * self.grid_size // 2)) * 2
random.shuffle(pairs)
for _ in range(self.grid_size):
row = ['X'] * self.grid_size
grid.append(row)
return grid, pairs
def check_winning_condition(self):
for i in range(self.grid_size):
for j in range(self.grid_size):
if self.grid[i][j] == 'X':
return False
return True
def option1(self):
row1, col1 = self.selectPair()
row2, col2 = self.selectPair()
if self.pairs[row1 * self.grid_size + col1] == self.pairs[row2 * self.grid_size + col2]:
self.found_pairs += 1
self.grid[row1][col1] = str(self.pairs[row1 * self.grid_size + col1])
self.grid[row2][col2] = str(self.pairs[row2 * self.grid_size + col2])
os.system('clear')
print("Correct pair!")
self.printGrid()
else:
print("Incorrect pair!")
self.grid[row1][col1] = str(self.pairs[row1 * self.grid_size + col1])
self.grid[row2][col2] = str(self.pairs[row2 * self.grid_size + col2])
# os.system('cls')
self.printGrid()
time.sleep(2)
self.grid[row1][col1] = 'X'
self.grid[row2][col2] = 'X'
os.system('clear')
self.printGrid()
print("Try Again!")
self.total_guesses += 1
return self.check_winning_condition() , self.determineScore()
def printGrid(self):
size = len(self.grid)
print(" ", end=" ")
for col in range(size):
print(f"[{chr(ord('A') + col)}]", end=" ")
print()
for row in range(size):
print(f"[{row+1}] ", end="")
for col in range(size):
print(f"{self.grid[row][col]} ", end=" ")
print()
def selectPair(self):
size = len(self.grid)
while True:
try:
cell = input("Enter the cell coordinates (e.g., a0): ").upper()
if self.validate_coordinates(cell, size):
row, col = self.parseCell(cell)
return row, col
except ValueError:
print("Invalid cell format. Please enter cells in the format 'A1', 'B2', etc.")
def parseCell(self, cell):
col = ord(cell[0].upper()) - ord('A')
row = int(cell[1:]) - 1
return row, col
def revealGrid(self):
size = len(self.grid)
for i in range(size):
for j in range(size):
if self.grid[i][j] == 'X':
self.grid[i][j] = str(self.pairs[i * self.grid_size + j])
def determineScore(self):
minimum_possible_guesses = (self.grid_size * self.grid_size) // 2
score = (minimum_possible_guesses / self.total_guesses) * 100
return round(score , 2)
def validate_coordinates(self, cell, size):
if len(cell) != 2 or not cell[0].isalpha() or not cell[1:].isdigit():
print("Invalid cell format. Please enter cells in the format 'A1', 'B2', etc.")
return False
col = ord(cell[0].upper()) - ord('A')
row = int(cell[1:]) - 1
if not (0 <= col < size):
print("Input Error: column entry is out of range. Please try again.")
return False
if not (0 <= row < size):
print("Input Error: row entry is out of range. Please try again.")
return False
if self.grid[row][col] != 'X':
print("Cell already revealed. Please try again.")
return False
return True
def uncoverOneElement(self):
size = len(self.grid)
while True:
try:
cell = input("Enter the cell coordinates: ")
row, col = self.parseCell(cell)
if self.validate_coordinates(cell, size):
self.grid[row][col] = str(self.pairs[row * self.grid_size + col])
self.revealed_elements += 1
self.total_guesses += 2
os.system('clear')
self.printGrid()
self.uncovered_coordinates.add(cell.upper())
isWin = self.check_winning_condition()
isCheated = len(self.uncovered_coordinates) == self.grid_size * self.grid_size
return isWin, isCheated, self.determineScore()
except ValueError:
print("Invalid cell format. Please enter cells in the format 'A1', 'B2', etc.")
def display_menu(self):
print("1. Let me select 2 elements")
print("2. Uncover one element for me")
print("3. I gave up - reveal all elements")
print("4. New Game")
print("5. Exit")
def get_user_choice(self):
while True:
choice = input("Select: ")
if choice.isdigit() and 1 <= int(choice) <= 5:
return int(choice)
else:
print("Invalid choice. Please enter a number between 1 and 5.")