Sudoku - Main Loop - Naive version
# States
NEW_CELL = 0
FIND_NEXT_CELL = 1
BACKTRACK = 2
AllVals = set([1,2,3,4,5,6,7,8,9])
def main(argv=None):
if not argv:
argv = sys.argv
name,board = getBoard(argv)
mystack = MyStack()
makeNeighbors()
nback = 0
ntrials = 0
cell = nextOpenCell(board,-1)
state = NEW_CELL
while True:
ntrials += 1
#if ntrials % 10000 == 0: print ('ntrials,nback',ntrials,nback)
# we're on a new open cell
if state == NEW_CELL:
guess,forced = nextValidGuess(board,cell,1)
#print ("NEW_CELL,cell,guess,forced",cell,guess,forced)
if not guess:
# failed to find a valid guess for this cell, backtrack
state = BACKTRACK
else:
board[cell] = guess
if not forced:
mystack.push([cell,board[:]])
state = FIND_NEXT_CELL
continue
# find a new open cell
if state == FIND_NEXT_CELL:
cell = nextOpenCell(board,cell)
if not cell:
# Solution!
break
state = NEW_CELL
continue
# backtrack
if state == BACKTRACK:
nback += 1
cell,board = mystack.pop()
old_guess = board[cell]
guess,forced = nextValidGuess(board,cell,old_guess+1) # note: state cannot be forced
#print('BACKTRACK,cell,guess,forced',cell,guess,forced)
if not guess:
state = BACKTRACK
else:
board[cell] = guess
mystack.push([cell,board[:]])
state = FIND_NEXT_CELL
continue
print ('Solution!, with ntrials, backtracks: ', ntrials,nback)
printBoard(board)
writeBoard(argv,name,board)