-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDetermineCellsToDisableWorkThread.py
More file actions
69 lines (61 loc) · 3.37 KB
/
DetermineCellsToDisableWorkThread.py
File metadata and controls
69 lines (61 loc) · 3.37 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
67
68
69
from WorkThread import *
from Cell import *
from MasyuExceptions import *
class DetermineCellsToDisableWorkThread(WorkThread):
def __init__(self, solver, puzzleBoard, itemType):
super().__init__(solver, puzzleBoard)
# Save the item type (black circle or white circle), which
# will be used by the thread code to determine which cells
# need to be disabled
self.__itemType = itemType
# This is the processing-intensive code which is run in the thread.
# It attempts to replace each cell in the puzzle (one at a time) with
# the indicated item (black circle or white circle). After replacing
# a cell, it runs the solver; if the solver raises and exception, then
# the item cannot be placed into that cell .. so it will be disabled.
# If no exception is raised, then the cell is enabled.
#
# This is a slow process, but it does allow the user to visually see
# which cells the current item type can be placed in.
def codeToRunInThread(self):
clonedPuzzleBoard = self.pb.cloneBoardOnly()
numRows, numCols = clonedPuzzleBoard.getDimensions()
# Cycle through each cell, and if it isn't already set to the indicated
# item type, then save the current cell setting, and force it to the
# indicated item type (black circle or white circle). Then run the
# solver to see if an exception is thrown .. indicating that the item
# cannot be placed into that cell, so the cell must be disabled.
for rowNum in range(0, numRows):
for colNum in range(0, numCols):
# Save the current cell type
if (clonedPuzzleBoard.isBlackCircleAt(rowNum, colNum)):
currentCell = Cell.TYPE_BLACK_CIRCLE
elif (clonedPuzzleBoard.isWhiteCircleAt(rowNum, colNum)):
currentCell = Cell.TYPE_WHITE_CIRCLE
else:
currentCell = Cell.TYPE_DOT
# Set cell to active item
if (self.__itemType == Cell.TYPE_BLACK_CIRCLE):
clonedPuzzleBoard.setBlackCircleAt(rowNum, colNum)
elif (self.__itemType == Cell.TYPE_WHITE_CIRCLE):
clonedPuzzleBoard.setWhiteCircleAt(rowNum, colNum)
else: # Never should fall through to here!
clonedPuzzleBoard.setDotAt(rowNum, colNum)
try:
self.solver.solve(clonedPuzzleBoard)
except (MasyuSolverException, MasyuOrphanedRegionException) as e:
# An exception was raised, so we need to disable the cell
self.pb.setCellDisabled(rowNum, colNum)
else:
# Solver was happy .. so enable the cell
self.pb.setCellEnabled(rowNum, colNum)
finally:
# Restore the cell back to it's original item
if (currentCell == Cell.TYPE_BLACK_CIRCLE):
clonedPuzzleBoard.setBlackCircleAt(rowNum, colNum)
elif (currentCell == Cell.TYPE_WHITE_CIRCLE):
clonedPuzzleBoard.setWhiteCircleAt(rowNum, colNum)
else:
clonedPuzzleBoard.setDotAt(rowNum, colNum)
# Clean up, in preparation for the next pass
clonedPuzzleBoard.clearSolution()