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 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 executeMove(self): whatMove = self.moveEntry.get() print "Executing: " + whatMove self.boardMap = CrazyLogic.nextStateInternal(self.boardMap, whatMove) self.cb.setBoardMap(self.boardMap) self.cb.draw()