Skip to content

Commit beb8c13

Browse files
committed
Implemented placefinder alg, partial crook alg
1 parent 2038798 commit beb8c13

8 files changed

+361
-9
lines changed

__pycache__/board.cpython-38.pyc

0 Bytes
Binary file not shown.

__pycache__/candidates.cpython-38.pyc

56 Bytes
Binary file not shown.

__pycache__/crook.cpython-38.pyc

825 Bytes
Binary file not shown.
5.5 KB
Binary file not shown.

candidates.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
'''
2+
Authored by Duncan Krammer
3+
2020-11-26
4+
'''
5+
#===============================================================================
6+
17
def initial_candidates(board):
28
'''
39
Go through cells in order and determine
@@ -27,6 +33,8 @@ def initial_candidates(board):
2733

2834
return candidates
2935

36+
#===============================================================================
37+
3038
def check_candidates(board, candidates):
3139
'''
3240
Iterate through candidates dict. If only
@@ -55,6 +63,7 @@ def check_candidates(board, candidates):
5563

5664
return board_changed
5765

66+
#===============================================================================
5867

5968
def update_candidates(board, candidates):
6069
'''
@@ -81,7 +90,8 @@ def update_candidates(board, candidates):
8190
updated_bool = not updated_candidates == candidates #True if no updates made
8291

8392
return updated_candidates, updated_bool
84-
93+
94+
#===============================================================================
8595

8696
def exhaust_candidates(board):
8797
'''
@@ -107,10 +117,9 @@ def exhaust_candidates(board):
107117
#board_updates += 1
108118
#print("Board updates: ", board_updates)
109119

110-
111120
return candidates
112121

113-
122+
#===============================================================================
114123

115124

116125

crook.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'''
2+
Authored by Duncan Krammer
3+
2020-11-26
4+
'''
5+
#===============================================================================
6+
7+
import candidates
8+
import place_finder
9+
10+
def crook_incomplete(board):
11+
'''
12+
Uses the candidate method and place finder method
13+
alternately to fill in a sudoku puzzle. Stops either
14+
when a solution is found or no more possible values
15+
can be filled with these two methods.
16+
Args:
17+
board - the puzzle to be checked
18+
'''
19+
20+
while(1):
21+
old_board_state = board.cells
22+
23+
#Use candidate method to place possible values
24+
candidates.exhaust_candidates(board)
25+
cand_board_state = board.cells
26+
27+
#Check if all cells are filled
28+
if '*' in cand_board_state: break
29+
30+
#Use place finder method to place possible values
31+
place_finder.place_finder(board)
32+
pf_board_state = board.cells
33+
34+
#Check if all cells are filled
35+
if '*' in pf_board_state: break
36+
37+
#Break if no changes were made
38+
if (old_board_state == cand_board_state) and (old_board_state == pf_board_state):
39+
break

main.py

+27-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from board import Board
22
from brute import Brute
33
import time
4-
import candidates
4+
import crook
55

66
'''
77
Candidate method is generally faster, especially in cases
@@ -14,19 +14,40 @@
1414
***Candidate method cannot find solutions to the hard or
1515
insane boards***
1616
'''
17-
'''
17+
1818
board = Board()
1919

20-
board.parse_board("boards/easy.txt")
20+
board.parse_board("boards/insane.txt")
2121
board.print()
2222

23-
cand = candidates.exhaust_candidates(board)
23+
crook.crook_incomplete(board)
2424

25-
#board.print()
25+
board.print()
2626
'''
27+
row_changed = [True, True, True, True, True, True, True, True, True]
28+
row_false = [False, False, False, False, False, False, False, False, False]
29+
while row_changed != row_false:
30+
for row in range(9):
31+
row_changed[row] = place_finder.check_row(board, row)
32+
#board.print()
33+
34+
#cand = candidates.exhaust_candidates(board)
2735
36+
board.print()
37+
38+
col_changed = [True, True, True, True, True, True, True, True, True]
39+
col_false = [False, False, False, False, False, False, False, False, False]
40+
while col_changed != col_false:
41+
for col in range(9):
42+
col_changed[col] = place_finder.check_col(board, col)
43+
#board.print()
44+
45+
#cand = candidates.exhaust_candidates(board)
46+
'''
47+
#board.print()
2848

2949

50+
'''
3051
board = Board()
3152
puzzles = ["boards/easy.txt", "boards/medium.txt", "boards/hard.txt", "boards/insane.txt"]
3253
@@ -55,7 +76,7 @@
5576
print("Brute time: {:f} seconds".format(brute_time))
5677
print("Candidate time: {:f} seconds".format(cand_time))
5778
58-
79+
'''
5980

6081

6182

0 commit comments

Comments
 (0)