@@ -98,19 +98,19 @@ def allCellsDone(self):
98
98
return False
99
99
return True
100
100
101
+ # Driver function to check all cells
101
102
def checkAllCells (self ):
102
103
self .checkRows ()
103
104
self .checkCols ()
104
105
self .checkSmallGrid ()
105
106
107
+ # To check that there are only numbers from 1-9 only once in the specific square
106
108
def checkSmallGrid (self ):
107
109
for x in range (3 ):
108
110
for y in range (3 ):
109
111
possibles = [1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ]
110
- # print("re-setting possibles")
111
112
for i in range (3 ):
112
113
for j in range (3 ):
113
- # print(x*3+i, y*3+j)
114
114
xidx = x * 3 + i
115
115
yidx = y * 3 + j
116
116
if self .grid [yidx ][xidx ] in possibles :
@@ -126,6 +126,7 @@ def checkSmallGrid(self):
126
126
if self .grid [yidx2 ][xidx2 ] == self .grid [yidx ][xidx ] and [xidx2 , yidx2 ] not in self .lockedCells :
127
127
self .incorrectCells .append ([xidx2 , yidx2 ])
128
128
129
+ # To check that there are only numbers from 1-9 only once in the specific row
129
130
def checkRows (self ):
130
131
for yidx , row in enumerate (self .grid ):
131
132
possibles = [1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ]
@@ -141,6 +142,7 @@ def checkRows(self):
141
142
self .incorrectCells .append ([k , yidx ])
142
143
143
144
145
+ # To check that there are only numbers from 1-9 only once in the specific column
144
146
def checkCols (self ):
145
147
for xidx in range (9 ):
146
148
possibles = [1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ]
@@ -156,6 +158,8 @@ def checkCols(self):
156
158
self .incorrectCells .append ([xidx , k ])
157
159
158
160
##### HELPER FUNCTIONS #####
161
+
162
+ # To get sudoku board details from 'nine.websudoku.com'
159
163
def getPuzzle (self , difficulty ):
160
164
html_doc = requests .get ("https://nine.websudoku.com/?level={}" .format (difficulty )).content
161
165
soup = BeautifulSoup (html_doc )
@@ -179,38 +183,45 @@ def getPuzzle(self, difficulty):
179
183
self .grid = board
180
184
self .load ()
181
185
186
+ # When checked, if cells are incorrect, colour them red
182
187
def shadeIncorrectCells (self , window , incorrect ):
183
188
for cell in incorrect :
184
189
pygame .draw .rect (window , INCORRECTCELLCOLOUR , (cell [0 ]* cellSize + gridPos [0 ], cell [1 ]* cellSize + gridPos [1 ], cellSize , cellSize ))
185
190
186
-
191
+ # To shade cells available beforehand to avoid confusion
187
192
def shadeLockedCells (self , window , locked ):
188
193
for cell in locked :
189
194
pygame .draw .rect (window , LOCKEDCELLCOLOUR , (cell [0 ]* cellSize + gridPos [0 ], cell [1 ]* cellSize + gridPos [1 ], cellSize , cellSize ))
190
195
196
+ # To add number to the selected cell
197
+ # only between 1-9, 0 won't be printed
191
198
def drawNumbers (self , window ):
192
199
for yidx , row in enumerate (self .grid ):
193
200
for xidx , num in enumerate (row ):
194
201
if num != 0 :
195
202
pos = [(xidx * cellSize )+ gridPos [0 ], (yidx * cellSize )+ gridPos [1 ]]
196
203
self .textToScreen (window , str (num ), pos )
197
204
205
+ # To change the shade of the selected cell
198
206
def drawSelection (self , window , pos ):
199
207
pygame .draw .rect (window , LIGHTBLUE , ((pos [0 ]* cellSize )+ gridPos [0 ], (pos [1 ]* cellSize )+ gridPos [1 ], cellSize , cellSize ))
200
208
209
+ # To make the sudoku grid of 9x9
201
210
def drawGrid (self , window ):
202
211
pygame .draw .rect (window , BLACK , (gridPos [0 ], gridPos [1 ], WIDTH - 150 , HEIGHT - 150 ), 2 )
203
212
for x in range (9 ):
204
213
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 )
205
214
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 )
206
215
216
+ # To select different cells on board
207
217
def mouseOnGrid (self ):
208
218
if self .mousePos [0 ] < gridPos [0 ] or self .mousePos [1 ] < gridPos [1 ]:
209
219
return False
210
220
if self .mousePos [0 ] > gridPos [0 ]+ gridSize or self .mousePos [1 ] > gridPos [1 ]+ gridSize :
211
221
return False
212
222
return ((self .mousePos [0 ]- gridPos [0 ])// cellSize , (self .mousePos [1 ]- gridPos [1 ])// cellSize )
213
223
224
+ # To add level and check buttons
214
225
def loadButtons (self ):
215
226
self .playingButtons .append (Button ( 20 , 40 , WIDTH // 7 , 40 ,
216
227
function = self .checkAllCells ,
@@ -237,6 +248,7 @@ def loadButtons(self):
237
248
params = "4" ,
238
249
text = "Evil" ))
239
250
251
+ # Helper function to add text to screen
240
252
def textToScreen (self , window , text , pos ):
241
253
font = self .font .render (text , False , BLACK )
242
254
fontWidth = font .get_width ()
@@ -258,9 +270,10 @@ def load(self):
258
270
if num != 0 :
259
271
self .lockedCells .append ([xidx , yidx ])
260
272
273
+ # To avoid typing alphabets on board
261
274
def isInt (self , string ):
262
275
try :
263
276
int (string )
264
277
return True
265
278
except :
266
- return False
279
+ return False
0 commit comments