Skip to content

Commit 7897780

Browse files
Commented the code
1 parent 3c4c2c4 commit 7897780

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

PyGamesScripts/Sudoku/app_class.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,19 @@ def allCellsDone(self):
9898
return False
9999
return True
100100

101+
# Driver function to check all cells
101102
def checkAllCells(self):
102103
self.checkRows()
103104
self.checkCols()
104105
self.checkSmallGrid()
105106

107+
# To check that there are only numbers from 1-9 only once in the specific square
106108
def checkSmallGrid(self):
107109
for x in range(3):
108110
for y in range(3):
109111
possibles = [1,2,3,4,5,6,7,8,9]
110-
# print("re-setting possibles")
111112
for i in range(3):
112113
for j in range(3):
113-
# print(x*3+i, y*3+j)
114114
xidx = x*3+i
115115
yidx = y*3+j
116116
if self.grid[yidx][xidx] in possibles:
@@ -126,6 +126,7 @@ def checkSmallGrid(self):
126126
if self.grid[yidx2][xidx2] == self.grid[yidx][xidx] and [xidx2, yidx2] not in self.lockedCells:
127127
self.incorrectCells.append([xidx2, yidx2])
128128

129+
# To check that there are only numbers from 1-9 only once in the specific row
129130
def checkRows(self):
130131
for yidx, row in enumerate(self.grid):
131132
possibles = [1,2,3,4,5,6,7,8,9]
@@ -141,6 +142,7 @@ def checkRows(self):
141142
self.incorrectCells.append([k, yidx])
142143

143144

145+
# To check that there are only numbers from 1-9 only once in the specific column
144146
def checkCols(self):
145147
for xidx in range(9):
146148
possibles = [1,2,3,4,5,6,7,8,9]
@@ -156,6 +158,8 @@ def checkCols(self):
156158
self.incorrectCells.append([xidx, k])
157159

158160
##### HELPER FUNCTIONS #####
161+
162+
# To get sudoku board details from 'nine.websudoku.com'
159163
def getPuzzle(self, difficulty):
160164
html_doc = requests.get("https://nine.websudoku.com/?level={}".format(difficulty)).content
161165
soup = BeautifulSoup(html_doc)
@@ -179,38 +183,45 @@ def getPuzzle(self, difficulty):
179183
self.grid = board
180184
self.load()
181185

186+
# When checked, if cells are incorrect, colour them red
182187
def shadeIncorrectCells(self, window, incorrect):
183188
for cell in incorrect:
184189
pygame.draw.rect(window, INCORRECTCELLCOLOUR, (cell[0]*cellSize+gridPos[0], cell[1]*cellSize+gridPos[1], cellSize, cellSize))
185190

186-
191+
# To shade cells available beforehand to avoid confusion
187192
def shadeLockedCells(self, window, locked):
188193
for cell in locked:
189194
pygame.draw.rect(window, LOCKEDCELLCOLOUR, (cell[0]*cellSize+gridPos[0], cell[1]*cellSize+gridPos[1], cellSize, cellSize))
190195

196+
# To add number to the selected cell
197+
# only between 1-9, 0 won't be printed
191198
def drawNumbers(self, window):
192199
for yidx, row in enumerate(self.grid):
193200
for xidx, num in enumerate(row):
194201
if num != 0:
195202
pos = [(xidx*cellSize)+gridPos[0], (yidx*cellSize)+gridPos[1]]
196203
self.textToScreen(window, str(num), pos)
197204

205+
# To change the shade of the selected cell
198206
def drawSelection(self, window, pos):
199207
pygame.draw.rect(window, LIGHTBLUE, ((pos[0]*cellSize)+gridPos[0], (pos[1]*cellSize)+gridPos[1], cellSize, cellSize))
200208

209+
# To make the sudoku grid of 9x9
201210
def drawGrid(self, window):
202211
pygame.draw.rect(window, BLACK, (gridPos[0], gridPos[1], WIDTH-150, HEIGHT-150), 2)
203212
for x in range(9):
204213
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)
205214
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)
206215

216+
# To select different cells on board
207217
def mouseOnGrid(self):
208218
if self.mousePos[0] < gridPos[0] or self.mousePos[1] < gridPos[1]:
209219
return False
210220
if self.mousePos[0] > gridPos[0]+gridSize or self.mousePos[1] > gridPos[1]+gridSize:
211221
return False
212222
return ((self.mousePos[0]-gridPos[0])//cellSize, (self.mousePos[1]-gridPos[1])//cellSize)
213223

224+
# To add level and check buttons
214225
def loadButtons(self):
215226
self.playingButtons.append(Button( 20, 40, WIDTH//7, 40,
216227
function=self.checkAllCells,
@@ -237,6 +248,7 @@ def loadButtons(self):
237248
params="4",
238249
text="Evil"))
239250

251+
# Helper function to add text to screen
240252
def textToScreen(self, window, text, pos):
241253
font = self.font.render(text, False, BLACK)
242254
fontWidth = font.get_width()
@@ -258,9 +270,10 @@ def load(self):
258270
if num != 0:
259271
self.lockedCells.append([xidx, yidx])
260272

273+
# To avoid typing alphabets on board
261274
def isInt(self, string):
262275
try:
263276
int(string)
264277
return True
265278
except:
266-
return False
279+
return False

0 commit comments

Comments
 (0)