I am having trouble figuring out how to read from a text file for my Sudoku solver. So I can get it to read the board from a text file, and I can get my code to solve the board when it's in the code, but I can't bet them to read and solve together. This is what I have and I've tried a few methods to get it to possibly work, but I'm not understanding it. If anyone could help, I'd really appreciate it! Thanks!
board = []
with open('project.txt','r') as file:
for line in file:
board.append(line.strip('\n').split(','))
if line != '':
(board)
#backtracking
def solve(pr):
find = find_empty(pr)
if not find:
return True
else:
row, col = find
for r in range(1,10):
if valid(pr, r, (row, col)):
pr[row][col] = r
if solve(pr):
return True
pr[row][col] = 0
return False
def valid(pr, num, pos):
# Check row
for r in range(len(pr[0])):
if pr[pos[0]][r] == num and pos[1] != r:
return False
# Check column
for r in range(len(pr)):
if pr[r][pos[1]] == num and pos[0] != r:
return False
# Check box
box_x = pos[1] // 3
box_y = pos[0] // 3
for r in range(box_y*3, box_y*3 3):
for c in range(box_x * 3, box_x*3 3):
if pr[r][c] == num and (r,c) != pos:
return False
return True
#formatting
def print_board(pr):
for r in range(len(pr)):
if r % 3 == 0 and r != 0:
print("- - - - - - - - - - - - - ")
for c in range(len(pr[0])):
if c % 3 == 0 and c != 0:
print(" | ", end="")
if c == 8:
print(pr[r][c])
else:
print(str(pr[r][c]) " ", end="")
def find_empty(pr):
for r in range(len(pr)):
for c in range(len(pr[0])):
if pr[r][c] == 0:
return (r, c) # row, col
return None
solve(board)
print_board(board)
my txt file looks like this:
003020600
900305001
001806400
008102900
700000008
006708200
002609500
800203009
005010300
CodePudding user response:
With this one-line change, your code solves the sudoku, even after I fill in 0s for the missing row.
board = []
with open('project.txt','r') as file:
for line in file:
board.append(list(map(int,line.strip())))
I added a print_board
before solving. That's why you see it twice.
Output:
9 0 0 | 3 0 5 | 0 0 1
0 0 1 | 8 0 6 | 4 0 0
0 0 8 | 1 0 2 | 9 0 0
- - - - - - - - - - - - -
7 0 0 | 0 0 0 | 0 0 8
0 0 6 | 7 0 8 | 2 0 0
0 0 2 | 6 0 9 | 5 0 0
- - - - - - - - - - - - -
8 0 0 | 2 0 3 | 0 0 9
0 0 5 | 0 1 0 | 3 0 0
0 0 0 | 0 0 0 | 0 0 0
9 2 4 | 3 7 5 | 8 6 1
5 3 1 | 8 9 6 | 4 7 2
6 7 8 | 1 4 2 | 9 3 5
- - - - - - - - - - - - -
7 5 3 | 4 2 1 | 6 9 8
1 9 6 | 7 5 8 | 2 4 3
4 8 2 | 6 3 9 | 5 1 7
- - - - - - - - - - - - -
8 4 7 | 2 6 3 | 1 5 9
2 6 5 | 9 1 7 | 3 8 4
3 1 9 | 5 8 4 | 7 2 6
CodePudding user response:
how is your input file supposed to look? Sth like this?:
1 x 3 x 5
23 x 5 65
12 23 x 54
...
If so, You should just read line-by-line and split that. I just need to know what your input file is supposed to look like.
CodePudding user response:
I would do something like this.
def loadBoard(fname):
board = []
with open(fname, 'r') as f:
for line in f:
board.append([int(x) for x in line.strip('\n')])
return board
This is based on the what you said the input is in your comment to Mike_S's answer, in other words just rows of numbers with no separators (unless you include the newlines of course). It seems to me like you're using the input as ints, so you would want to convert them to ints as I do in this example. Then to get the board, you just do:
board = loadBoard('project.txt')
I hope this helps. If I misunderstood something and you let me know, I will do my best to come back and correct it.
Edited because I forgot to put
board = []
inside the function.