def fenToBoardMap(fen): [fenA, fenB] = fen.split(' | ') mapping = {} mapping['boardA'] = CrazyLogic.fenToBoardMap(fenA) mapping['boardB'] = CrazyLogic.fenToBoardMap(fenB) return mapping
def genCards(currentLine, depth): if depth == 12: return #-------- # get the branches totalBranches = getTotalBranches(g_curs, currentLine) possibleNextPly = getPossibleNextPly(g_curs, currentLine) # calculate the popularity of each candidate move possibleNextPlyPopularity = {} for candidate in possibleNextPly: cTotalBranches = 1.0 * getTotalBranches(curs, currentLine + [candidate]) possibleNextPlyPopularity[candidate] = (cTotalBranches / totalBranches) * 100 # sort the possibilities possibleNextPly = sorted(possibleNextPly, key=lambda x: possibleNextPlyPopularity[x], reverse=True) # and filter the bad ones possibleNextPly = filter(lambda x: possibleNextPlyPopularity[x] >= 10.0, possibleNextPly) #-------- # play the moves to get the board state boardMap = CrazyLogic.fenToBoardMap(Common.initCrazyFEN) for move in currentLine: boardMap = CrazyLogic.nextStateInternal(boardMap, move, 0, 1) # draw the board, get the html # if black to play, flip board so we see from black's angle doFlip = len(currentLine) % 2 html = '' html += boardMapToHtml(boardMap, doFlip) html = re.sub('\n', '', html) html = re.sub('\.\/images\/', '', html) # draw the answers html += ';' for index, thing in enumerate(possibleNextPly): html += ("%s (%.02f%%)" % (possibleNextPly[index], possibleNextPlyPopularity[possibleNextPly[index]])) html += '<br />' # draw the line html += '<br />' html += '(line here is: ' + ','.join(currentLine) + ')' print html #-------- # now recur for ply in possibleNextPly: genCards(currentLine + [ply], depth + 1)
def __init__(self, parent, pieceWidth=48, pieceHeight=48): Tkinter.Frame.__init__(self, parent) self.parent = parent self.boardMap = CrazyLogic.fenToBoardMap(Common.initCrazyFEN) self.cb = CrazyBoard(self) self.cb.setBoardMap(self.boardMap) self.cb.draw() self.cb.pack() self.b = Tkinter.Button(self, text="flip", command=self.flipIt) self.b.pack() self.moveEntry = Tkinter.Entry(self) self.moveEntry.pack() self.execMove = Tkinter.Button(self, text="execute move", command=self.executeMove) self.execMove.pack() self.fenEntry = Tkinter.Entry(self) self.fenEntry.pack() self.loadFen = Tkinter.Button(self, text="load fen", command=self.loadFen) self.loadFen.pack()
def nextStateInternal(bm, player, move): bm = bm.copy() playerToBoard = { 'a': bm['boardA'], 'A': bm['boardA'], 'b': bm['boardB'], 'B': bm['boardB'] } playerToBoardOpp = { 'a': bm['boardB'], 'A': bm['boardB'], 'b': bm['boardA'], 'B': bm['boardA'] } # parse move m = re.match(Common.regexSan, move) if not m: raise Exception("cannot parse move: %s" % move) # but for captures, we've turned off CrazyBoard's transfer and instead # transfer across the table (board A <-> board B) # and unlike CrazyBoard's flip of the color, the captured piece's color is preserved in bug m = re.search('x([a-h][1-8])', move) if m: pieceCode = playerToBoard[player][m.group(1)] # look for en-passant if pieceCode == ' ': # then is it the en-passant target square? if m.group(1) != playerToBoard[player]['enPassTarget']: raise Exception("capturing onto empty square!") pieceCode = {'a': 'P', 'b': 'P', 'A': 'p', 'B': 'p'}[player] # promoted pieces back to pawns if re.match(r'^(.*)~$', pieceCode): pieceCode = {'a': 'P', 'b': 'P', 'A': 'p', 'B': 'p'}[player] playerToBoardOpp[player]['holdings'] += pieceCode # CrazyBoard handles removal from holdings during piece drop temp = CrazyLogic.nextStateInternal(playerToBoard[player], move, False) if player in 'Aa': bm['boardA'] = temp else: bm['boardB'] = temp return bm
def nextStateInternal(bm, player, move): bm = bm.copy() playerToBoard = {'a':bm['boardA'], 'A':bm['boardA'], 'b':bm['boardB'], 'B':bm['boardB']} playerToBoardOpp = {'a':bm['boardB'], 'A':bm['boardB'], 'b':bm['boardA'], 'B':bm['boardA']} # parse move m = re.match(Common.regexSan, move) if not m: raise Exception("cannot parse move: %s" % move) # but for captures, we've turned off CrazyBoard's transfer and instead # transfer across the table (board A <-> board B) # and unlike CrazyBoard's flip of the color, the captured piece's color is preserved in bug m = re.search('x([a-h][1-8])', move) if m: pieceCode = playerToBoard[player][m.group(1)] # look for en-passant if pieceCode == ' ': # then is it the en-passant target square? if m.group(1) != playerToBoard[player]['enPassTarget']: raise Exception("capturing onto empty square!") pieceCode = {'a':'P', 'b':'P', 'A':'p', 'B':'p'}[player] # promoted pieces back to pawns if re.match(r'^(.*)~$', pieceCode): pieceCode = {'a':'P', 'b':'P', 'A':'p', 'B':'p'}[player] playerToBoardOpp[player]['holdings'] += pieceCode # CrazyBoard handles removal from holdings during piece drop temp = CrazyLogic.nextStateInternal(playerToBoard[player], move, False) if player in 'Aa': bm['boardA'] = temp else: bm['boardB'] = temp return bm
def loadFen(self): whatFen = self.fenEntry.get() print "Loading fen: " + whatFen self.boardMap = CrazyLogic.fenToBoardMap(whatFen) self.cb.setBoardMap(self.boardMap) self.cb.draw()
def executeMove(self): whatMove = self.moveEntry.get() print "Executing: " + whatMove self.boardMap = CrazyLogic.nextStateInternal(self.boardMap, whatMove) self.cb.setBoardMap(self.boardMap) self.cb.draw()
def boardMapToFen(bm): return ' | '.join([ \ CrazyLogic.boardMapToFen(bm['boardA']), \ CrazyLogic.boardMapToFen(bm['boardB']) \ ])
conn = sqlite3.connect("MineMissedMates.db") curs = conn.cursor() bpgns = curs.execute("select position, mate from data where mate != ''") for rowIdx, row in enumerate(bpgns.fetchall()): (bfen, line) = map(lambda x: str(x), row) m = re.match('^(.*?) ', line) if not m: raise Exception("WTF? can't get first move from %s" % line) hint = m.group(1) boardMap = CrazyLogic.fenToBoardMap(bfen) flipped = 0 if boardMap['activePlayer'] == 'b': flipped = 1 html = GenTools.boardMapToHtml(boardMap, flipped, imgPath) html += "<h2>Puzzle %d</h2>\n" % rowIdx html += "<hr>\n" if anki: html = re.sub('\n', '', html) html = '%s;figure it out?' % html puzzleArray.append(html)