def nakedSingle(self): eliminate(self) for grid in self.gridList: if grid.tempNum == 0 and len(grid.candidates) == 1: grid.setTempNum(grid.candidates[0]) print grid.ind print "Found 1 at (", grid.row + 1, ",", grid.col + 1, ")" return True print "Found none..." return False
def hiddenSingle(self, getOne = False): ifFound = False eliminate(self) for unit in self.row: tempList = [] for i in unit: tempList = tempList + self.gridList[i].candidates for num in range(1,10): if tempList.count(num) == 1: for i in unit: if num in self.gridList[i].candidates: self.hints.append(hint('Hidden Single', 1, [self.gridList[i].row, self.gridList[i].col], num)) ifFound = True print 'Found 1 at (',self.gridList[i].row+1,',',self.gridList[i].col+1,')' if getOne: return ifFound for unit in self.col: tempList = [] for i in unit: tempList = tempList + self.gridList[i].candidates for num in range(1,10): if tempList.count(num) == 1: for i in unit: if num in self.gridList[i].candidates: self.hints.append(hint('Hidden Single', 1, [self.gridList[i].row, self.gridList[i].col], num)) ifFound = True print 'Found 1 at (',self.gridList[i].row+1,',',self.gridList[i].col+1,')' if getOne: return ifFound for unit in self.box: tempList = [] for i in unit: tempList = tempList + self.gridList[i].candidates for num in range(1,10): if tempList.count(num) == 1: for i in unit: if num in self.gridList[i].candidates: self.hints.append(hint('Hidden Single', 1, [self.gridList[i].row, self.gridList[i].col], num)) ifFound = True print 'Found 1 at (',self.gridList[i].row+1,',',self.gridList[i].col+1,')' if getOne: return ifFound return ifFound
def auto_solve_clicked(event): for i in range(size.rows): for j in range(size.columns): if textboxes[(i,j)].type==BoxType.Filled and textboxes[(i,j)].DownValue!=0: count=0 for k in range(size.rows-i-1): if textboxes[(i+k+1,j)].type==BoxType.Question: count+=1 else: break textboxes[(i,j)].downlist=getSum.getSubsets(textboxes[(i,j)].DownValue,count) if textboxes[(i,j)].type==BoxType.Filled and textboxes[(i,j)].AcrossValue!=0: count=0 for k in range(size.columns-j-1): if textboxes[(i,j+k+1)].type==BoxType.Question: count+=1 else: break textboxes[(i,j)].acrosslist=getSum.getSubsets(textboxes[(i,j)].AcrossValue,count) if textboxes[(i,j)].type==BoxType.Question: x=1 y=1 while textboxes[(i,j-y)].type==BoxType.Question: y=y+1 while textboxes[(i-x,j)].type==BoxType.Question: x=x+1 textboxes[(i,j-y)].acrosslist=eliminate.eliminate(textboxes[(i,j-y)].acrosslist,textboxes[(i-x,j)].downlist) textboxes[(i-x,j)].downlist=eliminate.eliminate(textboxes[(i-x,j)].downlist,textboxes[(i,j-y)].acrosslist) Arrange()#optimize by using fixed numbers from AllCombinations while(AssignValues()): Rearrange() for i in range(size.rows): for j in range(size.columns): textboxes[(i,j)].count=len(textboxes[(i,j)].AllDownCombinations) print textboxes[(i,j)].AllDownCombinations,textboxes[(i,j)].count BackTrack([0,0])
def reduce_puzzle(dict_grid): stalled = False while not stalled: # Check how many boxes have a determined value solved_values_before = len( [box for box in dict_grid.keys() if len(dict_grid[box]) == 1]) # Your code here: Use the Eliminate Strategy dict_grid = eliminate(dict_grid) # Your code here: Use the Only Choice Strategy dict_grid = only_choice(dict_grid) # Check how many boxes have a determined value, to compare solved_values_after = len( [box for box in dict_grid.keys() if len(dict_grid[box]) == 1]) # If no new values were added, stop the loop. stalled = solved_values_before == solved_values_after # Sanity check, return False if there is a box with zero available values: if len([box for box in dict_grid.keys() if len(dict_grid[box]) == 0]): return False return dict_grid
from net import BayesNetwork # declare all variables in the bn a = Variable('a', 2) # p1 b = Variable('b', 2) # p2 c = Variable('c', 2) # p3 d = Variable('d', 2) # p4 # set evidence variable to unhidden and give it a value c.hidden = False c.value = 0 evidence = [c] # set query variable to unhidden b.hidden = False # create phis and factors for those phis phi_a = [0.6, 0.4] phi_b = [0.5, 0.5, 0.2, 0.8] phi_c = [0.7, 0.3, 0.8, 0.2] phi_d = [0.5, 0.5, 0.2, 0.8] f_a = Factor('a', [a], phi_a) f_b = Factor('b', [b, a], phi_b) f_c = Factor('c', [c, b], phi_c) f_d = Factor('d', [d, b], phi_d) bn = BayesNetwork([a,b,c,d], [f_a, f_b, f_c, f_d]) eliminate(b, evidence, bn)
def boardEvents(self): events = pygame.event.get() for event in events: #print event if event.type == MOUSEBUTTONDOWN: grid_pos = event.pos if event.button == 3 or grid_pos[1] > 540: if self.focus is not -1: self.gridList[self.focus].selected = 0 self.focus = -1 self.displayBoard() elif event.button == 1: (g_row,g_col) = (grid_pos[1]/self.gridSize,grid_pos[0]/self.gridSize) tempPos = 9*g_row + g_col if self.focus is not -1: self.gridList[self.focus].selected = 0 self.focus = tempPos self.gridList[self.focus].selected = 1 else: print event self.displayBoard() elif event.type == KEYDOWN: key = event.key #print key if key >= 49 and key <= 57: #numKey setNum = key - 48 if self.focus is not -1: self.gridList[self.focus].setTempNum(setNum) self.displayBoard() # C = show/hide Candidates elif key == 99: if self.gridList[0].showCandidates == True: print 'Hide candidates' self.setShowCandidates(False) self.displayBoard() else: print 'Show candidates' self.setShowCandidates(True) self.displayBoard() # H = hints elif key == 104: for hint in self.hints: print hint.name, hint.row, hint.col, hint.num # E = eliminate candidates elif key == 101: eliminate(self) self.displayBoard() # U = Naked Single elif key == 117: nakedSingle(self) self.displayBoard() # V = Hidden Single elif key == 118: self.hints = [] hiddenSingle(self) self.displayBoard() # D or 0 = delete elif key == 100 or key == 48: if self.focus is not -1 and self.gridList[self.focus].fixed == False: self.gridList[self.focus].setTempNum(0) self.displayBoard() # Ctrl+S = Save Board elif key == 115: pygame.key.get_mods() # Ctrl+L = Load Board elif key == 108: if pygame.key.get_mods() & KMOD_CTRL: self.name = raw_input('The filename of the puzzle: ') fn_puzzle = 'puzzle/q' + self.name + '.txt' self.loadBoard(fn_puzzle) # Ctrl+N = New Board elif key == 110: if pygame.key.get_mods() & KMOD_CTRL: self.loadBoard() # ESC/Ctrl+Q = Exit elif key == 27 or (key == 113 and (pygame.key.get_mods() & KMOD_CTRL)): print 'Exit game. Godd bye!' self.gameOn = False break else: print key continue
for box in unit: box_vals = dict_grid[box] for possible_digit in box_vals: counts[possible_digit] += 1 for key, value in counts.items(): if value == 1: for box in unit: if key in dict_grid[box]: dict_grid[box] = key return dict_grid # Credit to the course def course_solution(values): for unit in unitlist: for digit in '123456789': dplaces = [box for box in unit if digit in values[box]] if len(dplaces) == 1: values[dplaces[0]] = digit return values if __name__ == '__main__': string_grid = '..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3..' dict_grid = grid_values(string_grid, boxes) dict_grid = eliminate(dict_grid) only_choice(dict_grid)