def gen_field(): possis = set((1,2,3,4,5,6,7,8,9)) rows = [possis] for i in range(0,8): rows.append(lscp(possis)) feld = [rows] for i in range(0,8): feld.append(lscp(rows)) return feld
def pr_simp(feld):#a simple display of the result so far if(type(feld) == int):#amount of the possibilities print(feld) elif (type(feld) == bin):#several possibilities for f in feld.solut: pr_simp(f) print("--") else:#list single possibility fake = lscp(feld) for x in range(0,9): for y in range(0,9): if type(fake[x][y]) == set: fake[x][y] = 0 if "-l" in sys.argv: out = "" for x in str(feld): if x in "123456789": out += x # out = [x for x in str(feld) if x in "123456789"] # for l in fake: # out.extend(l) print(out) else: for l in fake: print (l)
def guess(feld): #find optimal position to guess original = lscp(feld) for n in range(2,9): pos = find_easiest(feld) if type(pos) == list: break #now take the first of these possibilities possibs = list(feld[pos[0]][pos[1]]) bi = bin() bi.solut = [] while(len(possibs) != 0): if verbose: print("Guessing: ["+str(pos[0])+","+str(pos[1])+"] = "+str(possibs[-1])) feld[pos[0]][pos[1]] = possibs.pop() #now try to solve it result = solve(feld) if result == 20:#fail so try another possibility feld = original continue else: if type(result) == bin:#got into guessing so need to bi.solut.extend(result.solut) else: bi.solut.append(result) return bi