def backtrackingMRVfwd(filename): ### # use backtracking +MRV + forward propogation # to solve sudoku puzzle here, # return the solution in the form of list of # list as describe in the PDF with # of consistency # checks done ### board, emptyCells = readGame.readGameState(filename) if (False == checkGivenBoardState(board)): return (" ERROR --> Board Not Solvable", 0 ) # Each element in the remainingConstraints is a [list of of remaininig constraints, flag to indicate whether its been touched for # that particular iteration] remainingConstraints = [[[range(1, board.dimension + 1), 0] for x in range(board.dimension)] for x in range(board.dimension)] # Update the neighbour constraints values. for i in range(board.dimension): for j in range(board.dimension): updateNeighbourConstraints(board, remainingConstraints, (i, j), REMOVE) if (False == solveSudokuBacktrackingMRVfwd(board, remainingConstraints, 0, emptyCells)): return (" ERROR --> Board Not Solvable", 0 ) return (board.gameState, board.NoOfChecks)
def is_validMove(oldPos, dir): newPos = is_newPos(oldPos, dir) a, b = newPos p1 = readGame.readGameState("./game.txt") if p1[a][b] == 1: print("Valid Move") else: print("Invalid Move")
def __init__(self, filePath): self.gameState = readGame.readGameState(filePath) self.nodesExpanded = 0 self.trace = [] self.goldHolder = [[-1, -1, 0, 0, 0, -1, -1], [-1, -1, 0, 0, 0, -1, -1], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [-1, -1, 0, 0, 0, -1, -1], [-1, -1, 0, 0, 0, -1, -1]]
def backtracking(filename): ### # use backtracking to solve sudoku puzzle here, # return the solution in the form of list of # list as describe in the PDF with # of consistency # checks done ### board, emptyCells = readGame.readGameState(filename) if (False == checkGivenBoardState(board) or False == solveSudokuBacktracking(board, 0, 0, 0, int(emptyCells))): return (" ERROR --> Board Not Solvable", 0 ) return (board.gameState, board.NoOfChecks)
def __init__(self, filePath): self.gameState = readGame.readGameState(filePath) self.nodesExpanded = 0 self.trace = [] # Define a goal state to keep it handy for comparison self.goalState = [[0 for x in range(7)] for x in range(7)] self.goalState[3][3] = 1 for x in range(7): for y in range(7): if (x < 2 and (y < 2 or y > 4)) or (x > 4 and (y < 2 or y> 4)): self.goalState[x][y] = -1
def __init__(self, filePath): self.gameState = readGame.readGameState(filePath) self.nodesExpanded = 0 self.trace = [] self.myDestinationState = [[0 for x in range(7)] for x in range(7)] for i in range(7): for j in range(7): if self.gameState[i][j] == -1: self.myDestinationState[i][j] = -1 elif self.gameState[i][j] == 1: self.myDestinationState[i][j] = 0 elif self.gameState[i][j] == 0: self.myDestinationState[i][j] = 0 self.myDestinationState[3][3] = 1
def __init__(self, filePath): self.gameState = readGame.readGameState(filePath) self.nodesExpanded = 0 self.trace = []
def __init__(self, filePath): self.gameState = readGame.readGameState(filePath) self.startState = readGame.readGameState(filePath) self.numberOfPegs = self.computeNumberOfPegs() self.nodesExpanded = 0 self.trace = []
def __init__(self,filename): (self.sudokuBoard,self.N,self.M,self.K) = readGame.readGameState(filename) self.consistencyChecks = 0
def minConflict(filename): ### # use minConflict to solve sudoku puzzle here, # return the solution in the form of list of # list as describe in the PDF with # of consistency # checks done ### board, emptyCells = readGame.readGameState(filename) # check if given board doesnot contain any conflicts if (False == checkGivenBoardState(board)): return (" ERROR --> Board Not Solvable", 0 ) # randomnly assign values to empty cells in given board board, originalGameState = randomAssignmentOfValues(board) # max number of allowed consistency checks maxSteps = 100000 #math.pow(board.dimension, emptyCells) # conflict board store number of conflicts caused by each cell conflict_list = [] conflictBoard = [[0 for x in range(board.dimension)] for x in range(board.dimension)] # Count number of conflicts and updates conflict board noOfConflicts, conflict_list = calculateNoOfConflicts(board, originalGameState, conflictBoard) steps = 0 # Randomnly selects cell causing conflicts and changes value of cell to # the value that causes mininmum number of conflicts while(noOfConflicts != 0): steps += 1 if(steps > maxSteps): break; # randomnly select cell num = random.randint(0, len(conflict_list)-1) row = conflict_list[num][0] col = conflict_list[num][1] minConflict = conflictBoard[row][col] val = board.gameState[row][col] val_list = [] val_list.append(val) # find value with minimum conflicts if(originalGameState[row][col] == 0 and conflictBoard[row][col]!= 0 ): for i in range(1, board.dimension + 1): if i != board.gameState[row][col]: curConflict = checkNeighborConflicts(board, row, col, i) if (minConflict > curConflict): minConflict = curConflict val = i del val_list[:] val_list.append(i) elif (minConflict == curConflict): val_list.append(i) if(board.gameState[row][col] in val_list): val_list.remove(board.gameState[row][col]) if(len(val_list) != 0): board.gameState[row][col] = random.choice(val_list) else: board.gameState[row][col] = val noOfConflicts, conflict_list = calculateNoOfConflicts(board, originalGameState, conflictBoard) # if number of conflicts are more than allowed limit then return that min conflict failed if(steps > maxSteps): return ("Failed to solve the game within given limit", steps) return (board.gameState, steps)
def __init__(self, filePath): self.gameState = readGame.readGameState(filePath) self.nodesExpanded = 0 self.trace = [] self.goldHolder = [[-1,-1, 0, 0, 0,-1,-1], [-1,-1, 0, 0, 0,-1,-1], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 1, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [-1,-1, 0, 0, 0,-1,-1], [-1,-1, 0, 0, 0,-1,-1]]
def __init__(self, filePath): self.gameState = readGame.readGameState(filePath) self.nodesExpanded = 0 self.initState = self.copyGameState(self.gameState) self.trace = [] self.trace_dir = [] # direction of the move, to restore last gamestate
def __init__(self, filePath): """Initialize a game from a text file.""" self.gameState = readGame.readGameState(filePath) self.nodesExpanded = 0 self.trace = []