-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolver.py
More file actions
1250 lines (1083 loc) · 69.8 KB
/
Solver.py
File metadata and controls
1250 lines (1083 loc) · 69.8 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from MasyuExceptions import *
from Utilities import *
from OrphanedRegions import *
# This class implements the base solver functionality.
# It is used in muliple situations:
# 1) when trying to solve a Puzzle Board
# 2) when trying to determine cells to disable (smart placement)
# 3) when trying to determine if an item placed by the user was valid (smart placement off)
#
# If the solver detects a problem with the puzzle board, then and exception is raised.
class Solver():
def __init__(self):
self.__orphanedRegionDetector = OrphanedRegions()
# This is the main solving loop. It repeatedly goes through the process of applying the
# known solving rules, until either the puzzle has been solved, or a pass was made with
# no resulting changes (implies that the base solver could not currently solve the puzzle).
def solve(self,puzzleBoard):
changed = True
# You can't terminate the solving loop when the puzzle becomes solved, because this ends up
# leaving cells enabled which should have been disabled .. but they aren't, because the
# solving loop prematurely exited because it thought the puzzle was solved .. even though
# the "solving" happened in the clone board, while trying to determine which cells needed to
# be disabled!
while (changed):
changed = False
changed = changed or self.__processSpecialCases(puzzleBoard)
changed = changed or self.__findPathwaysToBlock(puzzleBoard)
changed = changed or self.__processDeadendPaths(puzzleBoard)
changed = changed or self.__processBlackCircles(puzzleBoard)
changed = changed or self.__processWhiteCircles(puzzleBoard)
changed = changed or self.__addLines(puzzleBoard)
changed = changed or self.__processSubPaths(puzzleBoard)
if not (changed):
# After all other processing has completed, we will check for
# problems caused by orphaned regions.
changed = self.__orphanedRegionDetector.checkForOrphanedRegions(puzzleBoard)
# A cell can only have a maximum of 2 lines. Therefore, if we detect that a
# cell has 2 lines, then we know that we can block off the remaining 2 pathways (if
# not already blocked off)
def __updateBlockedPathways(self, puzzleBoard, rowNum, colNum):
lineCount, l, r, u, d = puzzleBoard.getLines(rowNum, colNum)
if (lineCount == 2):
if not(l):
puzzleBoard.markBlockedLeft(rowNum, colNum)
if not(r):
puzzleBoard.markBlockedRight(rowNum, colNum)
if not(u):
puzzleBoard.markBlockedUp(rowNum, colNum)
if not(d):
puzzleBoard.markBlockedDown(rowNum, colNum)
elif (lineCount > 2):
raise MasyuSolverException("Cell has more than 2 lines", (rowNum, colNum))
# Wrapper functions used by other classes to access the functions responsible
# for adding a line segment to a cell.
def drawLineLeftWrapper(self, puzzleBoard, rowNum, colNum):
self.__drawLineLeftWrapper(puzzleBoard, rowNum, colNum)
def drawLineRightWrapper(self, puzzleBoard, rowNum, colNum):
self.__drawLineRightWrapper(puzzleBoard, rowNum, colNum)
def drawLineUpWrapper(self, puzzleBoard, rowNum, colNum):
self.__drawLineUpWrapper(puzzleBoard, rowNum, colNum)
def drawLineDownWrapper(self, puzzleBoard, rowNum, colNum):
self.__drawLineDownWrapper(puzzleBoard, rowNum, colNum)
# The following methods are "wrappers" for the four "drawLine" methods of the PuzzleBoard
# class. They not only draw the line, but they then immediately call the method which
# blocks paths affected by the line we just drew
def __drawLineLeftWrapper(self, puzzleBoard, rowNum, colNum):
lineCount, l, r, u, d = puzzleBoard.getLines(rowNum, colNum)
if (lineCount >= 2):
raise MasyuSolverException("lineLeftWrapper: too many lines in cell", (rowNum, colNum))
elif (lineCount == 1):
# Each line segment in the puzzle is considered a "subpath". We need to check for cases
# where there is the potential for a fully closed subpath; this is an ok situation if the
# subpath visits all of the circles in the puzzle (it means that the subpath effectively is
# the puzzle solution). But it is unacceptable if it doesn't visit all the circles .. in this
# case, we need to block the pathway which would allow the subpath to become completely "closed".
endingRow, endingCol, numCirclesVisited = self.__processSubPath(puzzleBoard, rowNum, colNum)
# Check if the subpath ended in the cell to our left
if ((endingCol == (colNum - 1)) and (endingRow == rowNum)):
totalNumCircles = Utilities.getNumberOfCircles(puzzleBoard)
if not (numCirclesVisited == totalNumCircles):
raise MasyuSolverException ("lineLeftWrapper: detected potential closed subloop", (rowNum, colNum))
else:
puzzleBoard.setSolved()
# Call the PuzzleBoard drawLine method
puzzleBoard.drawLineLeft(rowNum, colNum)
# Now make sure that any paths needing to be blocked get blocked
self.__updateBlockedPathways(puzzleBoard, rowNum, colNum)
self.__updateBlockedPathways(puzzleBoard, rowNum, (colNum - 1))
def __drawLineRightWrapper(self, puzzleBoard, rowNum, colNum):
lineCount, l, r, u, d = puzzleBoard.getLines(rowNum, colNum)
if (lineCount >= 2):
raise MasyuSolverException("lineRightWrapper: too many lines in cell", (rowNum, colNum))
elif (lineCount == 1):
# Each line segment in the puzzle is considered a "subpath". We need to check for cases
# where there is the potential for a fully closed subpath; this is an ok situation if the
# subpath visits all of the circles in the puzzle (it means that the subpath effectively is
# the puzzle solution). But it is unacceptable if it doesn't visit all the circles .. in this
# case, we need to block the pathway which would allow the subpath to become completely "closed".
endingRow, endingCol, numCirclesVisited = self.__processSubPath(puzzleBoard, rowNum, colNum)
# Check if the subpath ended in the cell to our right
if ((endingCol == (colNum + 1)) and (endingRow == rowNum)):
totalNumCircles = Utilities.getNumberOfCircles(puzzleBoard)
if not (numCirclesVisited == totalNumCircles):
raise MasyuSolverException("lineRightWrapper: detected potential closed subloop", (rowNum, colNum))
else:
puzzleBoard.setSolved()
# Call the PuzzleBoard drawLine method
puzzleBoard.drawLineRight(rowNum, colNum)
# Now make sure that any paths needing to be blocked get blocked
self.__updateBlockedPathways(puzzleBoard, rowNum, colNum)
self.__updateBlockedPathways(puzzleBoard, rowNum, (colNum + 1))
def __drawLineUpWrapper(self, puzzleBoard, rowNum, colNum):
lineCount, l, r, u, d = puzzleBoard.getLines(rowNum, colNum)
if (lineCount >= 2):
raise MasyuSolverException("lineUpWrapper: too many lines in cell", (rowNum, colNum))
elif (lineCount == 1):
# Each line segment in the puzzle is considered a "subpath". We need to check for cases
# where there is the potential for a fully closed subpath; this is an ok situation if the
# subpath visits all of the circles in the puzzle (it means that the subpath effectively is
# the puzzle solution). But it is unacceptable if it doesn't visit all the circles .. in this
# case, we need to block the pathway which would allow the subpath to become completely "closed".
endingRow, endingCol, numCirclesVisited = self.__processSubPath(puzzleBoard, rowNum, colNum)
# Check if the subpath ended in the cell above
if ((endingCol == colNum) and (endingRow == (rowNum - 1))):
totalNumCircles = Utilities.getNumberOfCircles(puzzleBoard)
if not (numCirclesVisited == totalNumCircles):
raise MasyuSolverException("lineUpWrapper: detected potential closed subloop", (rowNum, colNum))
else:
puzzleBoard.setSolved()
# Call the PuzzleBoard drawLine method
puzzleBoard.drawLineUp(rowNum, colNum)
# Now make sure that any paths needing to be blocked get blocked
self.__updateBlockedPathways(puzzleBoard, rowNum, colNum)
self.__updateBlockedPathways(puzzleBoard, (rowNum - 1), colNum)
def __drawLineDownWrapper(self, puzzleBoard, rowNum, colNum):
lineCount, l, r, u, d = puzzleBoard.getLines(rowNum, colNum)
if (lineCount >= 2):
raise MasyuSolverException("lineDownWrapper: too many lines in cell", (rowNum, colNum))
elif (lineCount == 1):
# Each line segment in the puzzle is considered a "subpath". We need to check for cases
# where there is the potential for a fully closed subpath; this is an ok situation if the
# subpath visits all of the circles in the puzzle (it means that the subpath effectively is
# the puzzle solution). But it is unacceptable if it doesn't visit all the circles .. in this
# case, we need to block the pathway which would allow the subpath to become completely "closed".
endingRow, endingCol, numCirclesVisited = self.__processSubPath(puzzleBoard, rowNum, colNum)
# Check if the subpath ended in the cell below
if ((endingCol == colNum) and (endingRow == (rowNum + 1))):
totalNumCircles = Utilities.getNumberOfCircles(puzzleBoard)
if not (numCirclesVisited == totalNumCircles):
raise MasyuSolverException("lineDownWrapper: detected potential closed subloop", (rowNum, colNum))
else:
puzzleBoard.setSolved()
# Call the PuzzleBoard drawLine method
puzzleBoard.drawLineDown(rowNum, colNum)
# Now make sure that any paths needing to be blocked get blocked
self.__updateBlockedPathways(puzzleBoard, rowNum, colNum)
self.__updateBlockedPathways(puzzleBoard, (rowNum + 1), colNum)
# The following is a helper function, which will be called recursively. Each time it is called,
# it is passed the row and column number for the cell to be looked at, along with the row
# and column of the previous cell in the line we are following. The count of the number
# of circles passed through so far is also passed in.
#
# Each time this method is called, it will check the next cell to see if the line continues to
# another cell. If it does, then it recursively calls this function, specifying the next cell.
# But if it has reached the end of the line, then it returns a tuple, specifying the row and
# column numbers for the last cell in the line, along with a total of the number of circles
# the line passed through.
def __moveToNextCell(self, puzzleBoard, row, col, prevRow, prevCol, numCirclesVisited):
# If the cell we are in is a circle, then increment numCirclesVisited
if not (puzzleBoard.isDotAt(row, col)):
numCirclesVisited += 1
# If the cell only has 1 line, then we've reached the end of the line
lineCount, l, r, u, d = puzzleBoard.getLines(row, col)
if (lineCount == 1):
# We're done! Return where we stopped and the # of circles visited
return ((row, col, numCirclesVisited))
else:
# We need to figure out which cell to travel to next; we must be careful to
# not backtrack out the way we came into the cell!
# If there is a line to the left, we can go there next, unless the previous cell
# was the one to the left!
if ((l) and ((prevRow != row) or (prevCol != (col - 1)))):
return (self.__moveToNextCell(puzzleBoard, row, (col - 1), row, col, numCirclesVisited))
# If there is a line to the right, we can go there next, unless the previous cell
# was the one to the right!
if (r) and ((prevRow != row) or (prevCol != (col + 1))):
return (self.__moveToNextCell(puzzleBoard, row, (col + 1), row, col, numCirclesVisited))
# If there is a line up, we can go there next, unless the previous cell
# was the one above us!
if (u) and ((prevCol != col) or (prevRow != (row - 1))):
return (self.__moveToNextCell(puzzleBoard, (row - 1), col, row, col, numCirclesVisited))
# If there is a line down, we can go there next, unless the previous cell
# was the one below us!
if (d) and ((prevCol != col) or (prevRow != (row + 1))):
return (self.__moveToNextCell(puzzleBoard, (row + 1), col, row, col, numCirclesVisited))
# We should never drop through to here!!
# Checks if the two cells are next to each other (abut). Returns 'True' if they do; else 'False'.
def __cellsAbut(self, r1, c1, r2, c2):
# Is cell 2 to the left of cell 1?
if ((r2 == r1) and (c2 == (c1 - 1))):
return (True)
# Is cell 2 to the right of cell 1?
if ((r2 == r1) and (c2 == (c1 + 1))):
return (True)
# Is cell 2 above cell 1?
if ((r2 == (r1 - 1)) and (c2 == c1)):
return (True)
# Is cell 2 below cell 1?
if ((r2 == (r1 + 1)) and (c2 == c1)):
return (True)
# Cells do not abut
return (False)
# Draw a line between the two cells, if it isn't already there. Return 'True' if the line
# was drawn (a change was made), and 'False' if not.
def __drawLineBetweenCells(self, puzzleBoard, r1, c1, r2, c2):
# Is cell 2 to the left of cell 1?
if ((r2 == r1) and (c2 == (c1 - 1))):
if not (puzzleBoard.hasLineLeft(r1, c1)):
# Draw the line
self.__drawLineLeftWrapper(puzzleBoard, r1, c1)
return (True)
else:
# Line is already there!
return (False)
# Is cell 2 to the right of cell 1?
if ((r2 == r1) and (c2 == (c1 + 1))):
if not (puzzleBoard.hasLineRight(r1, c1)):
# Draw the line
self.__drawLineRightWrapper(puzzleBoard, r1, c1)
return (True)
else:
# Line is already there!
return (False)
# Is cell 2 above cell 1?
if ((r2 == (r1 - 1)) and (c2 == c1)):
if not (puzzleBoard.hasLineUp(r1, c1)):
# Draw the line
self.__drawLineUpWrapper(puzzleBoard, r1, c1)
return (True)
else:
# Line is already there!
return (False)
# Is cell 2 below cell 1?
if ((r2 == (r1 + 1)) and (c2 == c1)):
if not (puzzleBoard.hasLineDown(r1, c1)):
# Draw the line
self.__drawLineDownWrapper(puzzleBoard, r1, c1)
return (True)
else:
# Line is already there!
return (False)
# No line was drawn
return (False)
# Block the path between the two cells, if it isn't already blocked. Return 'True' if the block
# was added (a change was made), and 'False' if not.
def __blockPathBetweenCells(self, puzzleBoard, r1, c1, r2, c2):
# Is cell 2 to the left of cell 1?
if ((r2 == r1) and (c2 == (c1 - 1))):
if not (puzzleBoard.isBlockedLeft(r1, c1)):
# Block the path
puzzleBoard.markBlockedLeft(r1, c1)
return (True)
else:
# Path is already blocked
return (False)
# Is cell 2 to the right of cell 1?
if ((r2 == r1) and (c2 == (c1 + 1))):
if not (puzzleBoard.isBlockedRight(r1, c1)):
# Block the path
puzzleBoard.markBlockedRight(r1, c1)
return (True)
else:
# Path is already blocked
return (False)
# Is cell 2 above cell 1?
if ((r2 == (r1 - 1)) and (c2 == c1)):
if not (puzzleBoard.isBlockedUp(r1, c1)):
# Block the path
puzzleBoard.markBlockedUp(r1, c1)
return (True)
else:
# Path is already blocked
return (False)
# Is cell 2 below cell 1?
if ((r2 == (r1 + 1)) and (c2 == c1)):
if not (puzzleBoard.isBlockedDown(r1, c1)):
# Block the path
puzzleBoard.markBlockedDown(r1, c1)
return (True)
else:
# Path is already blocked
return (False)
# Path was already blocked
return (False)
def __numConsecutiveWhiteCirclesInCol(self, puzzleBoard, rowNum, colNum):
# This function starts at the specified cell, and determines the number of consecutive white
# circle cells which are in the specified column. If there is a white circle in the cell above
# this one, then iAmFirst will be set to 'false', and count will be set to -1; that is because
# this cell is not the first in the line of white circle cells. Otherwise, if this is the first
# white circle cell in the vertical line, then iAmFirst will be set to 'true', and count will be
# set to the number of consecutive white circle cells in the line.
if ((rowNum == 0) or (rowNum > 0) and not (puzzleBoard.isWhiteCircleAt((rowNum - 1), colNum))):
# The starting cell is the first one
count = 0
numRows, numCols = puzzleBoard.getDimensions()
for i in range(rowNum, numRows):
if (puzzleBoard.isWhiteCircleAt(i, colNum)):
count += 1
else:
return (True, count)
return (True, count)
else:
return (False, -1)
def __numConsecutiveWhiteCirclesInRow(self, puzzleBoard, rowNum, colNum):
# This function starts at the specified cell, and determines the number of consecutive white
# circle cells which are in the specified row. If there is a white circle in the cell to the left
# of this one, then iAmFirst will be set to 'false', and count will be set to -1; that is because
# this cell is not the first in the line of white circle cells. Otherwise, if this is the first
# white circle cell in the horizontal line, then iAmFirst will be set to 'true', and count will
# be set to the number of consecutive white circle cells in the line.
if ((colNum == 0) or (colNum > 0) and not (puzzleBoard.isWhiteCircleAt(rowNum, (colNum - 1)))):
# The starting cell is the first one
count = 0
numRows, numCols = puzzleBoard.getDimensions()
for i in range(colNum, numCols):
if (puzzleBoard.isWhiteCircleAt(rowNum, i)):
count += 1
else:
return (True, count)
return (True, count)
else:
return (False, -1)
def __processSpecialCases(self, puzzleBoard):
# Case 9
# Look for 3 or more consecutive white circles.
# If found: block the pathway between each white circle, and at both ends.
# Algorithm assumes we process the Puzzle Board going L->R, T->B order.
# Because of this, if a white circle is found which isn't the first in line of a
# group of white circles, then it is skipped because it was already processed
# when the first white circle was encountered.
numRows, numCols = puzzleBoard.getDimensions()
changesMade = False
for rowNum in range(0, numRows):
for colNum in range(0, numCols):
if(puzzleBoard.isWhiteCircleAt(rowNum, colNum)):
# Special checks for cells which have a white circle
# Case 9a: Check for vertical line of white circles
iAmFirst, count = self.__numConsecutiveWhiteCirclesInCol(puzzleBoard, rowNum, colNum)
# Skip if not the first in the series
if (iAmFirst and (count > 2)):
if ((colNum > 0) and (colNum < (numCols - 1))):
# Block paths between and at ends of white circle cells
# If any place we plan to “block” already has a line, then
# the puzzle is invalid (or our code has a bug!)
# Block above starting cell
if (puzzleBoard.hasLineUp(rowNum, colNum)):
raise MasyuSolverException("Unexpected line up in special case 9", (rowNum, colNum))
elif not (puzzleBoard.isBlockedUp(rowNum, colNum)):
puzzleBoard.markBlockedUp(rowNum, colNum)
changesMade = True
# Now block the bottom pathway of each of the consecutive cells
for i in range(rowNum, (rowNum + count)):
if (puzzleBoard.hasLineDown(i, colNum)):
raise MasyuSolverException("Unexpected line down in special case 9", (i, colNum))
elif not (puzzleBoard.isBlockedDown(i, colNum)):
puzzleBoard.markBlockedDown(i, colNum)
changesMade = True
else:
raise MasyuSolverException("Invalid white circle location in special case 9-1", (rowNum, colNum))
# else:
# Not first, or less than 3 in a row
# Do Nothing
# Case 9b: Check for horizontal line of white circles
iAmFirst, count = self.__numConsecutiveWhiteCirclesInRow(puzzleBoard, rowNum, colNum)
# Skip if not the first in the series
if (iAmFirst and (count > 2)):
if ((rowNum > 0) and (rowNum < (numRows - 1))):
# Block paths between and at ends of white circle cells
# If any place we plan to “block” already has a line, then
# the puzzle is invalid (or our code has a bug!)
# Block to the left of the starting cell
if (puzzleBoard.hasLineLeft(rowNum, colNum)):
raise MasyuSolverException("Unexpected line left in special case 9", (rowNum, colNum))
elif not (puzzleBoard.isBlockedLeft(rowNum, colNum)):
puzzleBoard.markBlockedLeft(rowNum, colNum)
changesMade = True
# Now block the right pathway of each of the consecutive cells
for i in range(colNum, (colNum + count)):
if (puzzleBoard.hasLineRight(rowNum, i)):
raise MasyuSolverException("Unexpected line right in special case 9", (rowNum, i))
elif not (puzzleBoard.isBlockedRight(rowNum, i)):
puzzleBoard.markBlockedRight(rowNum, i)
changesMade = True
else:
raise MasyuSolverException("Invalid white circle location in special case 9-2", (rowNum, colNum))
# else:
# Not first, or less than 3 in a row
# Do Nothing
# Case 14-1
# Check for 2 vertically adjacent white circles; they can't be approached
# by a straight line!
iAmFirst, count = self.__numConsecutiveWhiteCirclesInCol(puzzleBoard, rowNum, colNum)
# Skip if not the first in the series
if (iAmFirst and (count == 2)):
# Case 14-1
if (rowNum > 1):
if (puzzleBoard.hasLineUp((rowNum - 1), colNum)):
if (puzzleBoard.hasLineUp(rowNum, colNum)):
raise MasyuSolverException("Unexpected line up in Case 14-1", (rowNum, colNum))
elif (puzzleBoard.hasLineDown((rowNum + 1), colNum)):
raise MasyuSolverException("Unexpected line down in Case 14-1", ((rowNum + 1), colNum))
else:
if not (puzzleBoard.isBlockedUp(rowNum, colNum)):
puzzleBoard.markBlockedUp(rowNum, colNum)
changesMade = True
if not (puzzleBoard.isBlockedDown((rowNum + 1), colNum)):
puzzleBoard.markBlockedDown((rowNum + 1), colNum)
changesMade = True
# Case 14-3
if (rowNum < (numRows - 3)):
if (puzzleBoard.hasLineDown((rowNum + 2), colNum)):
if (puzzleBoard.hasLineDown((rowNum + 1), colNum)):
raise MasyuSolverException("Unexpected line down in Case 14-3", ((rowNum + 1), colNum))
elif (puzzleBoard.hasLineUp(rowNum, colNum)):
raise MasyuSolverException("Unexpected line up in Case 14-3",(rowNum, colNum))
else:
if not (puzzleBoard.isBlockedUp(rowNum, colNum)):
puzzleBoard.markBlockedUp(rowNum, colNum)
changesMade = True
if not (puzzleBoard.isBlockedDown((rowNum + 1), colNum)):
puzzleBoard.markBlockedDown((rowNum + 1), colNum)
changesMade = True
# Case 14-2
# Check for horizontal line of white circles
iAmFirst, count = self.__numConsecutiveWhiteCirclesInRow(puzzleBoard, rowNum, colNum)
# Skip if not the first in the series
if (iAmFirst and (count == 2)):
# Case 14-2
if (colNum > 1):
if (puzzleBoard.hasLineLeft(rowNum, (colNum - 1))):
if (puzzleBoard.hasLineLeft(rowNum, colNum)):
raise MasyuSolverException("Unexpected line left in Case 14-2",(rowNum, colNum))
elif (puzzleBoard.hasLineRight(rowNum, (colNum + 1))):
raise MasyuSolverException("Unexpected line right in Case 14-2",(rowNum, (colNum + 1)))
else:
if not (puzzleBoard.isBlockedLeft(rowNum, colNum)):
puzzleBoard.markBlockedLeft(rowNum, colNum)
changesMade = True
if not (puzzleBoard.isBlockedRight(rowNum, (colNum + 1))):
puzzleBoard.markBlockedRight(rowNum, (colNum + 1))
changesMade = True
# Case 14-4
if (colNum < (numCols - 3)):
if (puzzleBoard.hasLineRight(rowNum, (colNum + 2))):
if (puzzleBoard.hasLineRight(rowNum, (colNum + 1))):
raise MasyuSolverException("Unexpected line right in Case 14-4",(rowNum, (colNum + 1)))
elif (puzzleBoard.hasLineLeft(rowNum, colNum)):
raise MasyuSolverException("Unexpected line left in Case 14-4",(rowNum, colNum))
else:
if not (puzzleBoard.isBlockedLeft(rowNum, colNum)):
puzzleBoard.markBlockedLeft(rowNum, colNum)
changesMade = True
if not (puzzleBoard.isBlockedRight(rowNum, (colNum + 1))):
puzzleBoard.markBlockedRight(rowNum, (colNum + 1))
changesMade = True
# Case 15-1
if (1 < rowNum < (numRows - 2)):
if ((puzzleBoard.hasLineUp((rowNum - 1), colNum)) and (puzzleBoard.hasLineDown((rowNum + 1), colNum))):
if (puzzleBoard.hasLineUp(rowNum, colNum)):
raise MasyuSolverException("Unexpected line up in Case 15-1", (rowNum, colNum))
if (puzzleBoard.hasLineDown(rowNum, colNum)):
raise MasyuSolverException("Unexpected line down in Case 15-1", (rowNum, colNum))
if not (puzzleBoard.isBlockedUp(rowNum, colNum)):
puzzleBoard.markBlockedUp(rowNum, colNum)
changesMade = True
if not (puzzleBoard.isBlockedDown(rowNum, colNum)):
puzzleBoard.markBlockedDown(rowNum, colNum)
changesMade = True
# Handle case 15-2
if (1 < colNum < (numCols - 2)):
if ((puzzleBoard.hasLineLeft(rowNum, (colNum - 1)) and (puzzleBoard.hasLineRight(rowNum, (colNum + 1))))):
if (puzzleBoard.hasLineLeft(rowNum, colNum)):
raise MasyuSolverException("Unexpected line left in Case 15-2",(rowNum, colNum))
if (puzzleBoard.hasLineRight(rowNum, colNum)):
raise MasyuSolverException("Unexpected line right in Case 15-2",(rowNum, colNum))
if not (puzzleBoard.isBlockedLeft(rowNum, colNum)):
puzzleBoard.markBlockedLeft(rowNum, colNum)
changesMade = True
if not (puzzleBoard.isBlockedRight(rowNum, colNum)):
puzzleBoard.markBlockedRight(rowNum, colNum)
changesMade = True
elif (puzzleBoard.isBlackCircleAt(rowNum, colNum)):
# Special checks for cells which have a black circle
# Case 10: black circle with 2 diagonally adjacent white circles (on the same side)
# Case 10-1: both white circles are below the black circle
if ((colNum > 0) and (colNum < (numCols - 1)) and (rowNum < (numRows - 1))):
if (puzzleBoard.isWhiteCircleAt((rowNum + 1), (colNum - 1)) and
puzzleBoard.isWhiteCircleAt((rowNum + 1), (colNum + 1))):
if (rowNum > 1):
if (puzzleBoard.hasLineDown(rowNum, colNum)):
raise MasyuSolverException("Unexpected line down in case 10-1", (rowNum, colNum))
elif not (puzzleBoard.isBlockedDown(rowNum, colNum)):
puzzleBoard.markBlockedDown(rowNum, colNum)
changesMade = True
else:
raise MasyuSolverException("Illegal black circle location in case 10-1", (rowNum, colNum))
# Case 10-2: Both white circles are to the left of the black circle
if ((rowNum > 0) and (rowNum < (numRows - 1)) and (colNum > 0)):
if (puzzleBoard.isWhiteCircleAt((rowNum - 1), (colNum - 1)) and
puzzleBoard.isWhiteCircleAt((rowNum + 1), (colNum - 1))):
if (colNum < (numCols - 2)):
if (puzzleBoard.hasLineLeft(rowNum, colNum)):
raise MasyuSolverException("Unexpected line left in case 10-2", (rowNum, colNum))
elif not (puzzleBoard.isBlockedLeft(rowNum, colNum)):
puzzleBoard.markBlockedLeft(rowNum, colNum)
changesMade = True
else:
raise MasyuSolverException("Illegal black circle location in case 10-2", (rowNum, colNum))
# Case 10-3: Both white circles are above the black circle
if ((colNum > 0) and (colNum < (numCols - 1)) and (rowNum > 0)):
if (puzzleBoard.isWhiteCircleAt((rowNum - 1), (colNum - 1)) and
puzzleBoard.isWhiteCircleAt((rowNum - 1), (colNum + 1))):
if (rowNum < (numRows - 2)):
if (puzzleBoard.hasLineUp(rowNum, colNum)):
raise MasyuSolverException("Unexpected line up in case 10-3", (rowNum, colNum))
elif not (puzzleBoard.isBlockedUp(rowNum, colNum)):
puzzleBoard.markBlockedUp(rowNum, colNum)
changesMade = True
else:
raise MasyuSolverException("Illegal black circle location in case 10-3", (rowNum, colNum))
# Case 10-4: Both white circles are to the right of the black circle
if ((rowNum > 0) and (rowNum < (numRows - 1)) and (colNum < (numCols - 1))):
if (puzzleBoard.isWhiteCircleAt((rowNum - 1), (colNum + 1)) and
puzzleBoard.isWhiteCircleAt((rowNum + 1), (colNum + 1))):
if (colNum > 1):
if (puzzleBoard.hasLineRight(rowNum, colNum)):
raise MasyuSolverException("Unexpected line right in case 10-4", (rowNum, colNum))
elif not (puzzleBoard.isBlockedRight(rowNum, colNum)):
puzzleBoard.markBlockedRight(rowNum, colNum)
changesMade = True
else:
raise MasyuSolverException("Illegal black circle location in case 10-4", (rowNum, colNum))
# Case 12: black circle, empty cell, then 2 consecutive white circles
# Case 12-1: Both white circles are below the black circle
if (rowNum < (numRows - 3)):
if ((puzzleBoard.isDotAt((rowNum + 1), colNum)) and
puzzleBoard.isWhiteCircleAt((rowNum + 2), colNum) and
puzzleBoard.isWhiteCircleAt((rowNum + 3), colNum)):
if (rowNum > 1):
if (puzzleBoard.hasLineDown(rowNum, colNum)):
raise MasyuSolverException("Unexpected line down in case 12-1", (rowNum, colNum))
elif not (puzzleBoard.isBlockedDown(rowNum, colNum)):
puzzleBoard.markBlockedDown(rowNum, colNum)
else:
raise MasyuSolverException("Illegal black circle location in case 12-1", (rowNum, colNum))
# Case 12-2: Both white circles are to the left of the black circle
if (colNum > 2):
if ((puzzleBoard.isDotAt(rowNum, (colNum - 1)) and
puzzleBoard.isWhiteCircleAt(rowNum, (colNum - 2)) and
puzzleBoard.isWhiteCircleAt(rowNum, (colNum - 3)))):
if (colNum < (numCols - 2)):
if (puzzleBoard.hasLineLeft(rowNum, colNum)):
raise MasyuSolverException("Unexpected line left in case 12-2", (rowNum, colNum))
elif not (puzzleBoard.isBlockedLeft(rowNum, colNum)):
puzzleBoard.markBlockedLeft(rowNum, colNum)
else:
raise MasyuSolverException("Illegal black circle location in case 12-2", (rowNum, colNum))
# Case 12-3: Both white circles are above the black circle
if (rowNum > 2):
if ((puzzleBoard.isDotAt((rowNum - 1), colNum)) and
puzzleBoard.isWhiteCircleAt((rowNum - 2), colNum) and
puzzleBoard.isWhiteCircleAt((rowNum - 3), colNum)):
if (rowNum < (numRows - 2)):
if (puzzleBoard.hasLineUp(rowNum, colNum)):
raise MasyuSolverException("Unexpected line up in case 12-3", (rowNum, colNum))
elif not (puzzleBoard.isBlockedUp(rowNum, colNum)):
puzzleBoard.markBlockedUp(rowNum, colNum)
else:
raise MasyuSolverException("Illegal black circle location in case 12-3", (rowNum, colNum))
# Case 12-4: Both white circles are to the right of the black circle
if (colNum < (numCols - 3)):
if ((puzzleBoard.isDotAt(rowNum, (colNum + 1)) and
puzzleBoard.isWhiteCircleAt(rowNum, (colNum + 2)) and
puzzleBoard.isWhiteCircleAt(rowNum, (colNum + 3)))):
if (colNum > 1):
if (puzzleBoard.hasLineRight(rowNum, colNum)):
raise MasyuSolverException("Unexpected line right in case 12-4", (rowNum, colNum))
elif not (puzzleBoard.isBlockedRight(rowNum, colNum)):
puzzleBoard.markBlockedRight(rowNum, colNum)
else:
raise MasyuSolverException("Illegal black circle location in case 12-4", (rowNum, colNum))
# Use the base solver rules to determine pathways which must be blocked.
def __findPathwaysToBlock(self, puzzleBoard):
numRows, numCols = puzzleBoard.getDimensions()
changesMade = False
# Case 1
for rowNum in range(0, numRows):
for colNum in range(0, numCols):
if (puzzleBoard.isWhiteCircleAt(rowNum, colNum)):
numLines, lineLeft, lineRight, lineUp, lineDown = puzzleBoard.getLines(rowNum, colNum)
# Case 1a
if (lineLeft and lineRight and (colNum > 1)):
if (puzzleBoard.hasLineLeft(rowNum, (colNum - 1))):
if not (puzzleBoard.isBlockedRight(rowNum, (colNum + 1))):
puzzleBoard.markBlockedRight(rowNum, (colNum + 1))
changesMade = True
# Case 1b
if (lineLeft and lineRight and (colNum < (numCols - 2))):
if (puzzleBoard.hasLineRight(rowNum, (colNum + 1))):
if not (puzzleBoard.isBlockedLeft(rowNum, (colNum - 1))):
puzzleBoard.markBlockedLeft(rowNum, (colNum - 1))
changesMade = True
# Case 1c
if (lineUp and lineDown and (rowNum > 1)):
if (puzzleBoard.hasLineUp((rowNum - 1), colNum)):
if not (puzzleBoard.isBlockedDown((rowNum + 1), colNum)):
puzzleBoard.markBlockedDown((rowNum + 1), colNum)
changesMade = True
# Case 1d
if (lineUp and lineDown and (rowNum < (numRows - 2))):
if (puzzleBoard.hasLineDown((rowNum + 1), colNum)):
if not (puzzleBoard.isBlockedUp((rowNum - 1), colNum)):
puzzleBoard.markBlockedUp((rowNum - 1), colNum)
changesMade = True
# Case 2
for rowNum in range(0, numRows):
for colNum in range(0, numCols):
if (puzzleBoard.isWhiteCircleAt(rowNum, colNum)):
numLines, lineLeft, lineRight, lineUp, lineDown = puzzleBoard.getLines(rowNum, colNum)
# Case 2a
if (lineUp or lineDown):
if not (puzzleBoard.isBlockedLeft(rowNum, colNum)):
puzzleBoard.markBlockedLeft(rowNum, colNum)
changesMade = True
if not (puzzleBoard.isBlockedRight(rowNum, colNum)):
puzzleBoard.markBlockedRight(rowNum, colNum)
changesMade = True
# Case 2b
if (lineLeft or lineRight):
if not (puzzleBoard.isBlockedUp(rowNum, colNum)):
puzzleBoard.markBlockedUp(rowNum, colNum)
changesMade = True
if not (puzzleBoard.isBlockedDown(rowNum, colNum)):
puzzleBoard.markBlockedDown(rowNum, colNum)
changesMade = True
# Case 3
for rowNum in range(0, numRows):
for colNum in range(0, numCols):
# Any cell which has two lines can have the other two pathways blocked.
numLines, lineLeft, lineRight, lineUp, lineDown = puzzleBoard.getLines(rowNum, colNum)
if (puzzleBoard.isDotAt(rowNum, colNum)):
if (numLines == 2):
if not (lineLeft):
if not (puzzleBoard.isBlockedLeft(rowNum, colNum)):
puzzleBoard.markBlockedLeft(rowNum, colNum)
changesMade = True
if not (lineUp):
if not (puzzleBoard.isBlockedUp(rowNum, colNum)):
puzzleBoard.markBlockedUp(rowNum, colNum)
changesMade = True
if not (lineRight):
if not (puzzleBoard.isBlockedRight(rowNum, colNum)):
puzzleBoard.markBlockedRight(rowNum, colNum)
changesMade = True
if not (lineDown):
if not (puzzleBoard.isBlockedDown(rowNum, colNum)):
puzzleBoard.markBlockedDown(rowNum, colNum)
changesMade = True
elif (puzzleBoard.isWhiteCircleAt(rowNum, colNum)):
if (numLines == 2):
if (lineLeft and lineRight):
if not (puzzleBoard.isBlockedUp(rowNum, colNum)):
puzzleBoard.markBlockedUp(rowNum, colNum)
changesMade = True
if not (puzzleBoard.isBlockedDown(rowNum, colNum)):
puzzleBoard.markBlockedDown(rowNum, colNum)
changesMade = True
elif (lineUp and lineDown):
if not (puzzleBoard.isBlockedLeft(rowNum, colNum)):
puzzleBoard.markBlockedLeft(rowNum, colNum)
changesMade = True
if not (puzzleBoard.isBlockedRight(rowNum, colNum)):
puzzleBoard.markBlockedRight(rowNum, colNum)
changesMade = True
else:
raise MasyuSolverException("Invalid turn in white circle", (rowNum, colNum))
elif (puzzleBoard.isBlackCircleAt(rowNum, colNum)):
if (numLines == 2):
if (lineLeft and lineUp):
if not (puzzleBoard.isBlockedRight(rowNum, colNum)):
puzzleBoard.markBlockedRight(rowNum, colNum)
changesMade = True
if not (puzzleBoard.isBlockedDown(rowNum, colNum)):
puzzleBoard.markBlockedDown(rowNum, colNum)
changesMade = True
elif (lineUp and lineRight):
if not (puzzleBoard.isBlockedLeft(rowNum, colNum)):
puzzleBoard.markBlockedLeft(rowNum, colNum)
changesMade = True
if not (puzzleBoard.isBlockedDown(rowNum, colNum)):
puzzleBoard.markBlockedDown(rowNum, colNum)
changesMade = True
elif (lineRight and lineDown):
if not (puzzleBoard.isBlockedLeft(rowNum, colNum)):
puzzleBoard.markBlockedLeft(rowNum, colNum)
changesMade = True
if not (puzzleBoard.isBlockedUp(rowNum, colNum)):
puzzleBoard.markBlockedUp(rowNum, colNum)
changesMade = True
elif (lineLeft and lineDown):
if not (puzzleBoard.isBlockedUp(rowNum, colNum)):
puzzleBoard.markBlockedUp(rowNum, colNum)
changesMade = True
if not (puzzleBoard.isBlockedRight(rowNum, colNum)):
puzzleBoard.markBlockedRight(rowNum, colNum)
changesMade = True
else:
raise MasyuSolverException("Missing turn in black circle", (rowNum, colNum))
# Case 4
for rowNum in range(0, numRows):
for colNum in range(0, numCols):
if (puzzleBoard.isBlackCircleAt(rowNum, colNum)):
# Checking cell to the left
if (colNum > 0):
if (puzzleBoard.hasLineUp(rowNum, (colNum - 1)) or puzzleBoard.hasLineDown(rowNum, (colNum - 1))):
if not (puzzleBoard.isBlockedLeft(rowNum, colNum)):
puzzleBoard.markBlockedLeft(rowNum, colNum)
changesMade = True
# Checking cell to the right
if (colNum < (numCols - 1)):
if (puzzleBoard.hasLineUp(rowNum, (colNum + 1)) or puzzleBoard.hasLineDown(rowNum, (colNum + 1))):
if not (puzzleBoard.isBlockedRight(rowNum, colNum)):
puzzleBoard.markBlockedRight(rowNum, colNum)
changesMade = True
# Checking cell above
if (rowNum > 0):
if (puzzleBoard.hasLineLeft((rowNum - 1), colNum) or puzzleBoard.hasLineRight((rowNum - 1), colNum)):
if not (puzzleBoard.isBlockedUp(rowNum, colNum)):
puzzleBoard.markBlockedUp(rowNum, colNum)
changesMade = True
# Checking cell below
if (rowNum < (numRows - 1)):
if (puzzleBoard.hasLineLeft((rowNum + 1), colNum) or puzzleBoard.hasLineRight((rowNum + 1), colNum)):
if not (puzzleBoard.isBlockedDown(rowNum, colNum)):
puzzleBoard.markBlockedDown(rowNum, colNum)
changesMade = True
return (changesMade)
# Analyze the current solution work, and block off any pathways which are effectively
# deadends.
def __processDeadendPaths(self, puzzleBoard):
numRows, numCols = puzzleBoard.getDimensions()
changesMade = False
deadEndFound = True
while deadEndFound:
deadEndFound = False
for rowNum in range(0, numRows):
for colNum in range(0, numCols):
if (puzzleBoard.isDotAt(rowNum, colNum)):
count, l, r, u, d = puzzleBoard.getBlockedPaths(rowNum, colNum)
# Any 'dot' cell which has 3 of it's pathways already blocked, is a deadend,
# so the fourth pathway can be blocked.
if (count == 3):
numLines, lineLeft, lineRight, lineUp, lineDown = puzzleBoard.getLines(rowNum, colNum)
if (numLines != 0):
raise MasyuSolverException("Unexpected line at dead-end", (rowNum, colNum))
else:
if not (l):
deadEndFound = True
puzzleBoard.markBlockedLeft(rowNum, colNum)
changesMade = True
elif not (r):
deadEndFound = True
puzzleBoard.markBlockedRight(rowNum, colNum)
changesMade = True
elif not (u):
deadEndFound = True
puzzleBoard.markBlockedUp(rowNum, colNum)
changesMade = True
elif not (d):
deadEndFound = True
puzzleBoard.markBlockedDown(rowNum, colNum)
changesMade = True
changesMade = changesMade | deadEndFound
return (changesMade)
# Locate the black circles in the puzzle, and apply the basic solving rules.
def __processBlackCircles(self, puzzleBoard):
numRows, numCols = puzzleBoard.getDimensions()
changesMade = False
for rowNum in range(0, numRows):
for colNum in range(0, numCols):
if (puzzleBoard.isBlackCircleAt(rowNum, colNum)):
if (puzzleBoard.isBlockedLeft(rowNum, colNum) or
((colNum > 0) and puzzleBoard.isBlockedLeft(rowNum, (colNum - 1)))):
if (colNum > (numCols - 3)) or \
puzzleBoard.isBlockedRight(rowNum, colNum) or \
puzzleBoard.isBlockedRight(rowNum, (colNum + 1)):
raise MasyuSolverException("Black Circle Blocked L/R", (rowNum, colNum))
else:
if not (puzzleBoard.hasLineRight(rowNum, colNum)):
changesMade = True
# puzzleBoard.drawLineRight(rowNum, colNum)
self.__drawLineRightWrapper(puzzleBoard, rowNum, colNum)
if not (puzzleBoard.hasLineRight(rowNum, (colNum + 1))):
changesMade = True
# puzzleBoard.drawLineRight(rowNum, (colNum + 1))
self.__drawLineRightWrapper(puzzleBoard, rowNum, (colNum + 1))
if (puzzleBoard.isBlockedRight(rowNum, colNum) or
((colNum < (numCols - 1) and puzzleBoard.isBlockedRight(rowNum, (colNum + 1))))):
if ((colNum < 2) or \
puzzleBoard.isBlockedLeft(rowNum, colNum) or \
puzzleBoard.isBlockedLeft(rowNum, (colNum - 1))):
raise MasyuSolverException("Black Circle Blocked L/R - 2", (rowNum, colNum))
else:
if not (puzzleBoard.hasLineLeft(rowNum, colNum)):
changesMade = True
# puzzleBoard.drawLineLeft(rowNum, colNum)
self.__drawLineLeftWrapper(puzzleBoard, rowNum, colNum)
if not (puzzleBoard.hasLineLeft(rowNum, (colNum - 1))):
changesMade = True
# puzzleBoard.drawLineLeft(rowNum, (colNum - 1))
self.__drawLineLeftWrapper(puzzleBoard, rowNum, (colNum - 1))
if (puzzleBoard.isBlockedUp(rowNum, colNum) or
((rowNum > 0) and puzzleBoard.isBlockedUp((rowNum - 1), colNum))):
if (rowNum > (numRows - 3)) or \
puzzleBoard.isBlockedDown(rowNum, colNum) or \
puzzleBoard.isBlockedDown((rowNum + 1), colNum):
raise MasyuSolverException("Black Circle Blocked U/D", (rowNum, colNum))
else:
if not (puzzleBoard.hasLineDown(rowNum, colNum)):
changesMade = True
# puzzleBoard.drawLineDown(rowNum, colNum)
self.__drawLineDownWrapper(puzzleBoard, rowNum, colNum)
if not (puzzleBoard.hasLineDown((rowNum + 1), colNum)):
changesMade = True
# puzzleBoard.drawLineDown((rowNum + 1), colNum)
self.__drawLineDownWrapper(puzzleBoard, (rowNum + 1), colNum)
if (puzzleBoard.isBlockedDown(rowNum, colNum) or
((rowNum < (numRows - 1)) and puzzleBoard.isBlockedDown((rowNum + 1), colNum))):
if (rowNum < 2) or \
puzzleBoard.isBlockedUp(rowNum, colNum) or \
puzzleBoard.isBlockedUp((rowNum - 1), colNum):
raise MasyuSolverException("Black Circle Blocked U/D - 2", (rowNum, colNum))
else:
if not (puzzleBoard.hasLineUp(rowNum, colNum)):
changesMade = True
# puzzleBoard.drawLineUp(rowNum, colNum)
self.__drawLineUpWrapper(puzzleBoard, rowNum, colNum)
if not (puzzleBoard.hasLineUp((rowNum - 1), colNum)):
changesMade = True
# puzzleBoard.drawLineUp((rowNum - 1), colNum)
self.__drawLineUpWrapper(puzzleBoard, (rowNum - 1), colNum)
# After drawing lines, we need to see if anything can be blocked for the
# cell being processed, or for the adjacent cell (since the line for a black
# circle extends for 2 cells!
count, l, r, u, d = puzzleBoard.getLines(rowNum, colNum)
# If line to the left from c[r,c], then block to the right of c[r,c] and also block up
# and down for c[r, c-1] (the cell to the left, where the first line segment goes)
if (l):
if not (puzzleBoard.isBlockedRight(rowNum, colNum)):
changesMade = True
puzzleBoard.markBlockedRight(rowNum, colNum)
if not (puzzleBoard.isBlockedUp(rowNum, (colNum - 1))):
changesMade = True
puzzleBoard.markBlockedUp(rowNum, (colNum - 1))
if not (puzzleBoard.isBlockedDown(rowNum, (colNum - 1))):
changesMade = True
puzzleBoard.markBlockedDown(rowNum, (colNum - 1))
# If line to the right from c[r,c], then block to the left of c[r,c] and also block up