-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_class.py
249 lines (206 loc) · 9.42 KB
/
app_class.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import pygame, sys
import requests
from bs4 import BeautifulSoup
from settings import *
from buttonClass import *
class App:
def __init__(self):
pygame.init()
self.window = pygame.display.set_mode((WIDTH, HEIGHT))
self.running = True
#self.grid = finishedBoard
self.grid = self.getPuzzle("1")
self.selected = None
self.mousePos = None
self.state = "playing"
self.finished = False
self.cellChanged = False
self.playingButtons = []
self.menuButtons = []
self.endButtons = []
self.lockedCells = []
self.incorrectCells = []
self.font = pygame.font.SysFont("arial", cellSize//2)
self.load()
#board = self.getPuzzle("1")
#print(board)
def run(self):
while self.running:
if self.state == "playing":
self.playing_events()
self.playing_update()
self.playing_draw()
pygame.quit()
sys.exit()
##### playing state functions ###########
def playing_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
#User Clicks
if event.type == pygame.MOUSEBUTTONDOWN:
selected = self.mouseOnGrid()
if selected:
self.selected = selected
else:
print("not on the board")
self.selected = None
#user types a key
if event.type == pygame.KEYDOWN:
if self.selected != None and self.selected not in self.lockedCells:
if self.isInt(event.unicode):
#cell changed
self.grid[self.selected[1]][self.selected[0]] = int(event.unicode)
self.cellChanged = True
def playing_update(self):
self.mousePos = pygame.mouse.get_pos()
for button in self.playingButtons:
button.update(self.mousePos)
if self.cellChanged:
self.incorrectCells=[]
if self.allCellsDone():
#Check if board is correct
self.checkAllCells()
if len(self.incorrectCells) == 0:
print("congrat!!")
def playing_draw(self):
self.window.fill(WHITE)
for button in self.playingButtons:
button.draw(self.window)
if self.selected:
self.drawSelection(self.window, self.selected)
self.shadeLockedCells(self.window, self.lockedCells)
self.shadeIncorrectCells(self.window, self.incorrectCells)
self.drawNumbers(self.window)
self.drawGrid(self.window)
pygame.display.update()
self.cellChanged = False
###### Board checking functions #####
def allCellsDone(self):
for row in self.grid:
for number in row:
if number == 0:
return False
return True
def checkAllCells(self):
self.checkRows()
self.checkCols()
self.checkSmallGrid()
def checkSmallGrid(self):
for x in range(3):
for y in range(3):
possibles = [1,2,3,4,5,6,7,8,9]
print("re-settings possibles")
for i in range(3):
for j in range(3):
#print(x*3+i, y*3+j)
xidx = x*3+i
yidx = y*3+j
if self.grid[yidx][xidx] in possibles:
possibles.remove(self.grid[yidx][xidx])
else:
if [xidx, yidx] not in self.lockedCells and [xidx, yidx] not in self.incorrectCells:
self.incorrectCells.append([xidx, yidx])
if [xidx, yidx] in self.lockedCells:
for k in range(3):
for l in range(3):
xidx2 = x*3+k
yidx2 = y*3+l
if self.grid[yidx2][xidx2] == self.grid[yidx][xidx] and [xidx2, yidx2] not in self.lockedCells:
self.incorrectCells.append([xidx2, yidx2])
def checkRows(self):
for yidx in range(9):
possibles = [1,2,3,4,5,6,7,8,9]
for xidx, row in enumerate(self.grid):
if self.grid[yidx][xidx] in possibles:
possibles.remove(self.grid[yidx][xidx])
else:
if[xidx, yidx] not in self.lockedCells and [xidx, yidx] not in self.incorrectCells:
self.incorrectCells.append([xidx, yidx])
if [xidx, yidx] in self.lockedCells:
for k in range(9):
if self.grid[yidx][k] == self.grid[yidx][xidx] and [k, yidx] not in self.lockedCells:
self.incorrectCells.append([k, yidx])
def checkCols(self):
for xidx, row in enumerate(self.grid):
possibles = [1,2,3,4,5,6,7,8,9]
for yidx in range(9):
if self.grid[yidx][xidx] in possibles:
possibles.remove(self.grid[yidx][xidx])
else:
if[xidx, yidx] not in self.lockedCells and [xidx, yidx] not in self.incorrectCells:
self.incorrectCells.append([xidx, yidx])
if [xidx, yidx] in self.lockedCells:
for k, row in enumerate(self.grid):
if self.grid[k][xidx] == self.grid[yidx][xidx] and[xidx, k] not in self.lockedCells:
self.incorrectCells.append([xidx, k])
##### Helper functions ######
def getPuzzle(self, difficutly):
### difficutly passed in as string with one digit. (1-4)
html_doc = requests.get("https://nine.websudoku.com/?level={}".format(difficutly)).content
soup = BeautifulSoup(html_doc)
ids = ['f00', 'f01', 'f02', 'f03', 'f04', 'f05', 'f06', 'f07', 'f08', 'f10', 'f11',
'f12', 'f13', 'f14', 'f15', 'f16', 'f17', 'f18', 'f20', 'f21', 'f22', 'f23',
'f24', 'f25', 'f26', 'f27', 'f28', 'f30', 'f31', 'f32', 'f33', 'f34', 'f35',
'f36', 'f37', 'f38', 'f40', 'f41', 'f42', 'f43', 'f44', 'f45', 'f46', 'f47',
'f48', 'f50', 'f51', 'f52', 'f53', 'f54', 'f55', 'f56', 'f57', 'f58', 'f60',
'f61', 'f62', 'f63', 'f64', 'f65', 'f66', 'f67', 'f68', 'f70', 'f71', 'f72',
'f73', 'f74', 'f75', 'f76', 'f77', 'f78', 'f80', 'f81', 'f82', 'f83', 'f84',
'f85', 'f86', 'f87', 'f88']
data = []
for cid in ids:
data.append(soup.find('input', id=cid))
board = [[0 for x in range(9)] for x in range(9)]
for index , cell in enumerate(data):
try:
board[index//9][index%9] = int(cell['value'])
except:
pass
return board
def shadeIncorrectCells(self, window, incorrect):
for cell in incorrect:
pygame.draw.rect(window, INCORRECTCELLCOLOUR, (cell[0]*cellSize+gridPos[0], cell[1]*cellSize+gridPos[1], cellSize, cellSize))
def shadeLockedCells(self, window, locked):
for cell in locked:
pygame.draw.rect(window, LOCKEDCELLCOLOUR, (cell[0]*cellSize+gridPos[0], cell[1]*cellSize+gridPos[1], cellSize, cellSize))
def drawNumbers(self, window):
for yidx, row in enumerate(self.grid):
for xidx, num in enumerate(row):
if num != 0:
pos = [xidx*cellSize+gridPos[0], yidx*cellSize+gridPos[1]]
self.textToScreen(window, str(num), pos)
def drawSelection(self, window, pos):
pygame.draw.rect(window, LIGHTBLUE, ((pos[0]*cellSize)+gridPos[0], (pos[1]*cellSize)+gridPos[1], cellSize, cellSize))
def drawGrid(self, window):
pygame.draw.rect(window, BLACK, (gridPos[0], gridPos[1], WIDTH-150, HEIGHT-150), 2)
for x in range(9):
pygame.draw.line(window, BLACK, (gridPos[0], gridPos[1]+(x*cellSize)), (gridPos[0]+450, gridPos[1]+(x*cellSize)), 2 if x % 3 == 0 else 1)
pygame.draw.line(window, BLACK, (gridPos[0]+(x*cellSize), gridPos[1]), (gridPos[0]+(x*cellSize), gridPos[1]+450), 2 if x % 3 == 0 else 1)
def mouseOnGrid(self):
if self.mousePos[0] < gridPos[0] or self.mousePos[1] < gridPos[1]:
return False
if self.mousePos[0] > gridPos[0]+gridSize or self.mousePos[1] > gridPos[1]+gridSize:
return False
return ((self.mousePos[0]-gridPos[0])//cellSize, (self.mousePos[1]-gridPos[1])//cellSize)
def loadButtons(self):
self.playingButtons.append(Button(20,40, 100, 40))
def textToScreen(self, window, text, pos):
font = self.font.render(text, False, BLACK)
fontWidth = font.get_width()
fontHeight = font.get_height()
pos[0] += (cellSize - fontWidth)//2
pos[1] += (cellSize - fontHeight)//2
window.blit(font, pos)
def load(self):
self.loadButtons()
#### setting lockedcells from orginal board
for yidx, row in enumerate(self.grid):
for xidx, num in enumerate(row):
if num != 0:
self.lockedCells.append([xidx, yidx])
def isInt(self, string):
try:
int(string)
return True
except:
return False