def emit_move_signal(self, cord0, cord1, promotion=None): # Game end can change cord0 to None while dragging a piece if cord0 is None: return color = self.view.model.boards[-1].color board = self.view.model.getBoardAtPly(self.view.shown, self.view.shownVariationIdx) # Ask player for which piece to promote into. If this move does not # include a promotion, QUEEN will be sent as a dummy value, but not used if promotion is None and board[cord0].sign == PAWN and cord1.y in ( 0, self.RANKS - 1): promotion = self.getPromotion() if promotion is None: # Put back pawn moved be d'n'd self.view.runAnimation(redrawMisc=False) return if cord0.x < 0 or cord0.x > self.FILES - 1: move = Move(lmovegen.newMove(board[cord0].piece, cord1.cord, DROP)) else: move = Move(cord0, cord1, board, promotion) if self.view.model.curplayer.__type__ == LOCAL and self.view.shownIsMainLine() and \ self.view.model.boards[-1] == board and self.view.model.status == RUNNING: self.emit("piece_moved", move, color) else: if board.board.next is None and not self.view.shownIsMainLine(): self.view.model.add_move2variation(board, move, self.view.shownVariationIdx) self.view.shown += 1 else: new_vari = self.view.model.add_variation(board, (move, )) self.view.setShownBoard(new_vari[-1])
def validate (self, cord0, cord1): if cord0 is None or cord1 is None: return False # prevent accidental NULL_MOVE creation if cord0 == cord1: return False if self.getBoard()[cord0] == None: return False if self.parent.setup_position: to_piece = self.getBoard()[cord1] # prevent moving pieces inside holding if (cord0.x < 0 or cord0.x > self.FILES-1) and \ (cord1.x < 0 or cord1.x > self.FILES-1): return False # prevent moving kings off board elif self.getBoard()[cord0].piece == KING and \ (cord1.x < 0 or cord1.x > self.FILES-1): return False # prevent taking enemy king elif to_piece is not None and to_piece.piece == KING: return False else: return True if cord1.x < 0 or cord1.x > self.FILES-1: return False if cord0.x < 0 or cord0.x > self.FILES-1: # drop return validate(self.getBoard(), Move(lmovegen.newMove(self.getBoard()[cord0].piece, cord1.cord, DROP))) else: return validate(self.getBoard(), Move(cord0, cord1, self.getBoard()))
def testAttackedAndNotProtected(self): """ Testing what recognize the algorithm """ board = Board(setup=True) dsa = DecisionSupportAlgorithm() dsa.set_foe_as_bot() dsa.enableDisableAlgo(True) moves = ((cordDic["e2"], cordDic["e4"]), (cordDic["d7"], cordDic["d5"])) for cord0, cord1 in moves: board = board.move(Move(Cord(cord0), Cord(cord1), board)) coordinate_attacked = dsa._DecisionSupportAlgorithm__apply_algorithm( board, WHITE, dsa._DecisionSupportAlgorithm__attacked_and_not_protected) # Not protected self.assertEqual([Cord("e4", color="R")], coordinate_attacked) coordinate_attacked = dsa._DecisionSupportAlgorithm__apply_algorithm( board, BLACK, dsa._DecisionSupportAlgorithm__attacked_and_not_protected) # protected by Queen self.assertEqual([], coordinate_attacked) board = board.move(Move(Cord(E4), Cord(E5), board)) coordinate_attacked = dsa._DecisionSupportAlgorithm__apply_algorithm( board, WHITE, dsa._DecisionSupportAlgorithm__attacked_and_not_protected) self.assertEqual([], coordinate_attacked)
def validate(self, cord0, cord1): if cord0 is None or cord1 is None: return False # prevent accidental NULL_MOVE creation if cord0 == cord1 and self.parent.variant.variant != SITTUYINCHESS: return False if self.getBoard()[cord0] is None: return False if self.parent.setup_position: # prevent moving pieces inside holding if (cord0.x < 0 or cord0.x > self.FILES - 1) and \ (cord1.x < 0 or cord1.x > self.FILES - 1): return False else: return True if cord1.x < 0 or cord1.x > self.FILES - 1: return False if cord0.x < 0 or cord0.x > self.FILES - 1: # drop return validate( self.getBoard(), Move( lmovegen.newMove(self.getBoard()[cord0].piece, cord1.cord, DROP))) else: return validate(self.getBoard(), Move(cord0, cord1, self.getBoard()))
def on_players_changed(game): # fill fools mate moves to players move queue p0 = game.players[0] p0.move_queue.put_nowait(Move(newMove(F2, F3))) p0.move_queue.put_nowait(Move(newMove(G2, G4))) p1 = gamemodel.players[1] p1.move_queue.put_nowait(Move(newMove(E7, E5))) p1.move_queue.put_nowait(Move(newMove(D8, H4)))
def emit_move_signal(self, cord0, cord1, promotion=None): # Game end can change cord0 to None while dragging a piece if cord0 is None: return color = self.view.model.boards[-1].color board = self.view.model.getBoardAtPly(self.view.shown, self.view.shown_variation_idx) # Ask player for which piece to promote into. If this move does not # include a promotion, QUEEN will be sent as a dummy value, but not used if promotion is None and board[ cord0].sign == PAWN and cord1.cord in board.PROMOTION_ZONE[ color]: if self.variant.variant == SITTUYINCHESS: # no promotion allowed if we have queen if board.board.boards[color][QUEEN]: promotion = None else: # promotion is always optional promotion = self.getPromotion() if promotion is None and cord0 == cord1: # if don't want in place promotion return elif len(self.variant.PROMOTIONS) == 1: promotion = lmove.PROMOTE_PIECE(self.variant.PROMOTIONS[0]) else: if conf.get("autoPromote", False): promotion = lmove.PROMOTE_PIECE(QUEEN_PROMOTION) else: promotion = self.getPromotion() if promotion is None: # Put back pawn moved be d'n'd self.view.runAnimation(redraw_misc=False) return if cord0.x < 0 or cord0.x > self.FILES - 1: move = Move(lmovegen.newMove(board[cord0].piece, cord1.cord, DROP)) else: move = Move(cord0, cord1, board, promotion) if (self.view.model.curplayer.__type__ == LOCAL or self.view.model.examined) and \ self.view.shownIsMainLine() and \ self.view.model.boards[-1] == board and \ self.view.model.status == RUNNING: if self.setup_position: self.emit("piece_moved", (cord0, cord1), board[cord0].color) else: self.emit("piece_moved", move, color) if self.view.model.examined: self.view.model.connection.bm.sendMove(toAN(board, move)) else: if board.board.next is None and not self.view.shownIsMainLine(): self.view.model.add_move2variation( board, move, self.view.shown_variation_idx) self.view.shown += 1 else: new_vari = self.view.model.add_variation(board, (move, )) self.view.setShownBoard(new_vari[-1])
def testAll(self): board = Board(setup=True) dsa = DecisionSupportAlgorithm() dsa.set_foe_as_bot() dsa.enableDisableAlgo(True) moves = ((cordDic["e2"], cordDic["e4"]), (cordDic["d7"], cordDic["d5"])) for cord0, cord1 in moves: board = board.move(Move(Cord(cord0), Cord(cord1), board)) board.printPieces() # Not protected self.assertEqual( set([ Cord("a1", color="Y"), Cord("h1", color="Y"), Cord("e4", color="R") ]), set(dsa.calculate_coordinate_in_danger(board, WHITE))) # protected by Queen, so no danger self.assertEqual(set([Cord("a8", color="Y"), Cord("h8", color="Y")]), set(dsa.calculate_coordinate_in_danger(board, BLACK))) # pawn go forward, no danger board = board.move(Move(Cord(E4), Cord(E5), board)) self.assertEqual( set([ Cord("a1", color="Y"), Cord("h1", color="Y"), Cord("e5", color="Y") ]), set(dsa.calculate_coordinate_in_danger(board, WHITE))) # Should not recognize king board_king = Board(setup=True) dsa_king = DecisionSupportAlgorithm() dsa_king.set_foe_as_bot() dsa_king.enableDisableAlgo(True) moves = ((cordDic["e2"], cordDic["e4"]), (cordDic["f7"], cordDic["f5"]), (cordDic["d1"], cordDic["h5"])) for cord0, cord1 in moves: board_king = board_king.move( Move(Cord(cord0), Cord(cord1), board_king)) # board_king.printPieces() self.assertEqual( set([ Cord("a8", color="Y"), Cord("h8", color="Y"), Cord("f5", color="Y") ]), set(dsa.calculate_coordinate_in_danger(board_king, BLACK)))
def emit_move_signal(self, cord0, cord1, promotion=None): # Game end can change cord0 to None while dragging a piece if cord0 is None: return board = self.getBoard() color = board.color # Ask player for which piece to promote into. If this move does not # include a promotion, QUEEN will be sent as a dummy value, but not used if promotion is None and board[cord0].sign == PAWN and \ cord1.cord in board.PROMOTION_ZONE[color] and \ self.variant.variant != SITTUYINCHESS: if len(self.variant.PROMOTIONS) == 1: promotion = lmove.PROMOTE_PIECE(self.variant.PROMOTIONS[0]) else: if conf.get("autoPromote"): promotion = lmove.PROMOTE_PIECE(QUEEN_PROMOTION) else: promotion = self.getPromotion() if promotion is None: # Put back pawn moved be d'n'd self.view.runAnimation(redraw_misc=False) return if promotion is None and board[cord0].sign == PAWN and \ cord0.cord in board.PROMOTION_ZONE[color] and \ self.variant.variant == SITTUYINCHESS: # no promotion allowed if we have queen if board.board.boards[color][QUEEN]: promotion = None # in place promotion elif cord1.cord in board.PROMOTION_ZONE[color]: promotion = lmove.PROMOTE_PIECE(QUEEN_PROMOTION) # queen move promotion (but not a pawn capture!) elif board[cord1] is None and (cord0.cord + cord1.cord) % 2 == 1: promotion = lmove.PROMOTE_PIECE(QUEEN_PROMOTION) if cord0.x < 0 or cord0.x > self.FILES - 1: move = Move(lmovegen.newMove(board[cord0].piece, cord1.cord, DROP)) else: move = Move(cord0, cord1, board, promotion) if (self.view.model.curplayer.__type__ == LOCAL or self.view.model.examined) and \ self.view.shownIsMainLine() and \ self.view.model.boards[-1] == board and \ self.view.model.status == RUNNING: # emit move if self.setup_position: self.emit("piece_moved", (cord0, cord1), board[cord0].color) else: self.emit("piece_moved", move, color) if self.view.model.examined: self.view.model.connection.bm.sendMove(toAN(board, move)) else: self.play_or_add_move(board, move)
def shownChanged(self, boardview, shown): m = boardview.model if m is None or m.isPlayingICSGame(): return b = m.getBoardAtPly(shown, boardview.shown_variation_idx) parent = self.empty_parent() openings = getOpenings(b.board) openings.sort(key=lambda t: t[1], reverse=True) if not openings: return totalWeight = 0.0 # Polyglot-formatted books have space for learning data. # See version ac31dc37ec89 for an attempt to parse it. # In this version, we simply ignore it. (Most books don't have it.) for move, weight, learn in openings: totalWeight += weight self.opening_names = [] for move, weight, learn in openings: if totalWeight != 0: weight /= totalWeight goodness = min(float(weight * len(openings)), 1.0) weight = "%0.1f%%" % (100 * weight) opening = get_eco(b.move(Move(move)).board.hash) if opening is None: eco = "" # self.opening_names.append("") else: eco = "%s - %s %s" % (opening[0], opening[1], opening[2]) # self.opening_names.append("%s %s" % (opening[1], opening[2])) self.store.append( parent, [ (b, Move(move), None), (weight, 1, goodness), 0, False, eco, False, False, ], ) tp = Gtk.TreePath(self.path) self.tv.expand_row(tp, False)
def makeMove(self, board1, move, board2): log.debug("Human.makeMove: move=%s, board1=%s board2=%s" % (move, board1, board2)) if self.board.view.premove_piece and self.board.view.premove0 and \ self.board.view.premove1 and \ self.color == self.board.view.premove_piece.color: if validate( board1, Move(self.board.view.premove0, self.board.view.premove1, board1, promotion=self.board.view.premove_promotion)): log.debug("Human.makeMove: Setting move to premove %s %s" % (self.board.view.premove0, self.board.view.premove1)) self.board.emit_move_signal( self.board.view.premove0, self.board.view.premove1, promotion=self.board.view.premove_promotion) # reset premove self.board.view.setPremove(None, None, None, None) self.gmwidg.setLocked(False) item = self.queue.get(block=True) self.gmwidg.setLocked(True) if item == "del": raise PlayerIsDead("Killed by foreign forces") if item == "int": log.debug("Human.makeMove: %s: raise TurnInterrupt" % self) raise TurnInterrupt return item
def walk(model, node, path): if node.prev is None: # initial game board board = model.variant(setup=node.asFen(), lboard=node) else: move = Move(node.lastMove) try: board = node.prev.pieceBoard.move(move, lboard=node) except: raise LoadingError( _("Invalid move."), "%s%s" % (move_count(node, black_periods=True), move)) if node.next is None: model.variations.append(path + [board]) else: walk(model, node.next, path + [board]) for child in node.children: if isinstance(child, list): if len(child) > 1: # non empty variation, go walk walk(model, child[1], list(path)) else: if not self.has_emt: self.has_emt = child.find("%emt") >= 0 if not self.has_eval: self.has_eval = child.find("%eval") >= 0
def makeMove(self, board1, move, board2): log.debug("Human.makeMove: move=%s, board1=%s board2=%s" % (move, board1, board2)) if self.board.view.premove_piece and self.board.view.premove0 and \ self.board.view.premove1 and \ self.color == self.board.view.premove_piece.color: if validate( board1, Move(self.board.view.premove0, self.board.view.premove1, board1, promotion=self.board.view.premove_promotion)): log.debug("Human.makeMove: Setting move to premove %s %s" % (self.board.view.premove0, self.board.view.premove1)) self.board.emit_move_signal( self.board.view.premove0, self.board.view.premove1, promotion=self.board.view.premove_promotion) # reset premove self.board.view.setPremove(None, None, None, None) self.gmwidg.setLocked(False) item = yield from self.move_queue.get() self.gmwidg.setLocked(True) if item == "del": log.debug("Human.makeMove got: del") raise PlayerIsDead elif item == "int": log.debug("Human.makeMove got: int") raise TurnInterrupt elif item == "pass": log.debug("Human.makeMove got: pass") raise PassInterrupt return item
def row_activated(self, iter, model): if self.mode == HINT and self.store.get_path(iter) != Gtk.TreePath( self.path): moves = self.store[iter][0][2] if moves is not None: score = self.store[iter][1][0] model.add_variation(self.engine.board, moves, comment="", score=score) if self.mode == SPY and self.store.get_path(iter) != Gtk.TreePath( self.path): moves = self.store[iter][0][2] if moves is not None: score = self.store[iter][1][0] board = self.engine.board.board # SPY analyzer has inverted color boards # we need to chage it to get the board in gamemodel variations board list later board.setColor(1 - board.color) king = board.kings[board.color] null_move = Move(newMove(king, king, NULL_MOVE)) model.add_variation(self.engine.board, [null_move] + moves, comment="", score=score)
def test_apply_pop(self): """Testing Atomic applyMove popMove""" board = LBoard(variant=ATOMICCHESS) board.applyFen(FEN) print(board) hist_exploding_around0 = [a[:] for a in board.hist_exploding_around] print_apply_pop = False for lmove1 in genAllMoves(board): board.applyMove(lmove1) if board.opIsChecked(): if print_apply_pop: print("popMove1 (invalid)", Move(lmove1)) board.popMove() continue hist_exploding_around1 = [ a[:] for a in board.hist_exploding_around ] for lmove2 in genAllMoves(board): board.applyMove(lmove2) if print_apply_pop: print(" applyMove2", Move(lmove2)) if board.opIsChecked(): if print_apply_pop: print(" popMove2 (invalid)", Move(lmove2)) board.popMove() continue hist_exploding_around2 = [ a[:] for a in board.hist_exploding_around ] for lmove3 in genAllMoves(board): board.applyMove(lmove3) if print_apply_pop: print(" applyMove3", Move(lmove3)) if board.opIsChecked(): if print_apply_pop: print(" popMove3 (invalid)", Move(lmove3)) board.popMove() continue board.popMove() if print_apply_pop: print(" popMove3", Move(lmove3)) self.assertEqual(hist_exploding_around2, board.hist_exploding_around) board.popMove() if print_apply_pop: print(" popMove2", Move(lmove2)) self.assertEqual(hist_exploding_around1, board.hist_exploding_around) board.popMove() if print_apply_pop: print("popMove1", Move(lmove1)) self.assertEqual(hist_exploding_around0, board.hist_exploding_around)
def _genPossibleBoards(self, ply): possibleBoards = [] curboard = self.view.model.getBoardAtPly(ply) for lmove in lmovegen.genAllMoves(curboard.board): move = Move(lmove) board = curboard.move(move) possibleBoards.append(board) return possibleBoards
def validate(self, cord0, cord1): if cord0 is None or cord1 is None: return False if self.getBoard()[cord0] == None: return False if cord1.x < 0 or cord1.x > self.FILES - 1: return False if cord0.x < 0 or cord0.x > self.FILES - 1: # drop return validate( self.getBoard(), Move( lmovegen.newMove(self.getBoard()[cord0].piece, cord1.cord, DROP))) else: return validate(self.getBoard(), Move(cord0, cord1, self.getBoard()))
def getMoveAtPly(self, ply, variation=0): try: return Move(self.variations[variation][self._plyToIndex(ply) + 1].board.lastMove) except IndexError: log.error("%d\t%d\t%d\t%d\t%d" % (self.lowply, ply, self.ply, variation, len(self.variations))) raise
def getMoveKillingKing (board): """ Returns a move from the current color, able to capture the opponent king """ lboard = board.board color = lboard.color opking = lboard.kings[1-color] for cord in iterBits (getAttacks(lboard, opking, color)): return Move(Cord(cord), Cord(opking), board)
def validate(self, cord0, cord1): if cord0 is None or cord1 is None: return False # prevent accidental NULL_MOVE creation if cord0 == cord1: return False if self.getBoard()[cord0] == None: return False if cord1.x < 0 or cord1.x > self.FILES - 1: return False if cord0.x < 0 or cord0.x > self.FILES - 1: # drop return validate( self.getBoard(), Move( lmovegen.newMove(self.getBoard()[cord0].piece, cord1.cord, DROP))) else: return validate(self.getBoard(), Move(cord0, cord1, self.getBoard()))
def _genPossibleBoards(self, ply): possibleBoards = [] if self.setup_position: return possibleBoards if len(self.view.model.players) == 2 and self.view.model.isEngine2EngineGame(): return possibleBoards curboard = self.view.model.getBoardAtPly(ply, self.view.shownVariationIdx) for lmove in lmovegen.genAllMoves(curboard.board.clone()): move = Move(lmove) board = curboard.move(move) possibleBoards.append(board) return possibleBoards
def get_all_moves(request: WSGIRequest) -> JsonResponse: board = _get_board(request) from_coord = int(request.GET.get("index")) potential_moves = _get_potential_board_moves(from_coord, board) all_legal_moves = [Move(move) for move in genAllMoves(board.board)] legal_destinations = _get_legal_destinations(potential_moves, all_legal_moves) return JsonResponse({"moves": legal_destinations})
def testNotProtected(self): board = Board(setup=True) dsa = DecisionSupportAlgorithm() dsa.set_foe_as_bot() dsa.enableDisableAlgo(True) # at the start of the game, only the two towers are not protected by other pieces coordinate_not_protected = dsa._DecisionSupportAlgorithm__apply_algorithm( board, WHITE, dsa._DecisionSupportAlgorithm__not_protected) self.assertEqual(set([Cord("a1", color="Y"), Cord("h1", color="Y")]), set(coordinate_not_protected)) board = board.move( Move(Cord(cordDic["e2"]), Cord(cordDic["e4"]), board)) coordinate_not_protected = dsa._DecisionSupportAlgorithm__apply_algorithm( board, WHITE, dsa._DecisionSupportAlgorithm__not_protected) # the pawn moved to e4 is now not protected self.assertEqual( set([ Cord("a1", color="Y"), Cord("h1", color="Y"), Cord("e4", color="Y") ]), set(coordinate_not_protected)) board = board.move( Move(Cord(cordDic["d7"]), Cord(cordDic["d5"]), board)) # the black pawn attack the white pawn, it is not notProtected that will detect this case, # only the two towers are not protected coordinate_not_protected = dsa._DecisionSupportAlgorithm__apply_algorithm( board, WHITE, dsa._DecisionSupportAlgorithm__not_protected) self.assertEqual(set([Cord("a1", color="Y"), Cord("h1", color="Y")]), set(coordinate_not_protected))
def _create_board(model, node): if node.prev is None: # initial game board board = model.variant(setup=node.asFen(), lboard=node) else: move = Move(node.lastMove) try: board = node.prev.pieceBoard.move(move, lboard=node) except Exception: raise LoadingError( _("Invalid move."), "%s%s" % (move_count(node, black_periods=True), move)) return board
def add_variation(self, board, moves, comment="", score="", emit=True): if board.board.next is None: # If we are in the latest played board, and want to add a variation # we have to add the latest move first if board.board.lastMove is None or board.board.prev is None: return moves = [Move(board.board.lastMove)] + moves board = board.board.prev.pieceBoard board0 = board board = board0.clone() board.board.prev = None # this prevents annotation panel node searches to find this instead of board0 board.board.hash = -1 if comment: board.board.children.append(comment) variation = [board] for move in moves: new = board.move(move) if len(variation) == 1: new.board.prev = board0.board variation[0].board.next = new.board else: new.board.prev = board.board board.board.next = new.board variation.append(new) board = new board0.board.next.children.append( [vboard.board for vboard in variation]) if score: variation[-1].board.children.append(score) head = None for vari in self.variations: if board0 in vari: head = vari break variation[0] = board0 self.variations.append(head[:board0.ply - self.lowply] + variation) self.needsSave = True if emit: self.emit("variation_added", board0.board.next.children[-1], board0.board.next) return self.variations[-1]
def isAPotentiallyLegalNextMove(self, cord0, cord1): """ Determines whether the given move is at all legally possible as the next move after the player who's turn it is makes their move Note: This doesn't always return the correct value, such as when BoardControl.setLocked() has been called and we've begun a drag, but view.shown and BoardControl.lockedPly haven't been updated yet """ if cord0 == None or cord1 == None: return False if not self.parent.lockedPly in self.parent.possibleBoards: return False for board in self.parent.possibleBoards[self.parent.lockedPly]: if not board[cord0]: return False if validate(board, Move(cord0, cord1, board)): return True return False
def test1(self): """ Testing Board.move() on frc castling in non frc game """ board = Board(setup=True) moves = ((D2, D4), (G8, F6), (C2, C4), (G7, G6), (G2, G3), (F8, G7), (F1, G2), (E8, H8)) for cord0, cord1 in moves: print(cord0, cord1) board = board.move(Move(Cord(cord0), Cord(cord1), board)) board.printPieces() self.assertIsNone(board[Cord(E8)]) self.assertIsNone(board[Cord(H8)]) self.assertEqual(board[Cord(G8)].piece, Piece(BLACK, KING).piece) self.assertEqual(board[Cord(F8)].piece, Piece(BLACK, ROOK).piece)
def emit_move_signal(self, cord0, cord1): color = self.view.model.boards[-1].color board = self.view.model.getBoardAtPly(self.view.shown) # Ask player for which piece to promote into. If this move does not # include a promotion, QUEEN will be sent as a dummy value, but not used promotion = QUEEN if board[cord0].sign == PAWN and cord1.y in (0, 7): res = self.promotionDialog.runAndHide(color) if res != gtk.RESPONSE_DELETE_EVENT: promotion = res else: # Put back pawn moved be d'n'd self.view.runAnimation(redrawMisc=False) return move = Move(cord0, cord1, self.view.model.boards[-1], promotion) self.emit("piece_moved", move, color)
def walk(node, path): if node.prev is None: # initial game board if variant == "Fischerandom": board = FRCBoard(setup=node.asFen(), lboard=node) elif variant == "Atomic": board = AtomicBoard(setup=node.asFen(), lboard=node) elif variant == "Crazyhouse": board = CrazyhouseBoard(setup=node.asFen(), lboard=node) elif variant == "Wildcastle": board = WildcastleBoard(setup=node.asFen(), lboard=node) elif variant == "Suicide": board = SuicideBoard(setup=node.asFen(), lboard=node) elif variant == "Losers": board = LosersBoard(setup=node.asFen(), lboard=node) elif variant == "Kingofthehill": board = KingOfTheHillBoard(setup=node.asFen(), lboard=node) else: board = Board(setup=node.asFen(), lboard=node) else: move = Move(node.lastMove) try: board = node.prev.pieceBoard.move(move, lboard=node) except: raise LoadingError( _("Invalid move."), "%s%s" % (move_count(node, black_periods=True), move)) if node.next is None: model.variations.append(path + [board]) else: walk(node.next, path + [board]) for child in node.children: if isinstance(child, list): if len(child) > 1: # non empty variation, go walk walk(child[1], list(path)) else: if not self.has_emt: self.has_emt = child.find("%emt") >= 0 if not self.has_eval: self.has_eval = child.find("%eval") >= 0
def scoreAllMoves(self, lBoard): """ Return each move's result and depth to mate. lBoard: A low-level board structure Return value: a list, with best moves first, of: move: A high-level move structure game_result: Either WHITEWON, DRAW, BLACKWON depth: Depth to mate """ pc = self._pieceCounts(lBoard) if self.provider.supports(pc): results = provider.scoreAllMoves(lBoard) if results: ret = [] for lMove, result, depth in results: ret.append((Move(lMove), result, depth)) return ret return []
def get_book_move(self): openings = getOpenings(self.boards[-1].board) openings.sort(key=lambda t: t[1], reverse=True) if not openings: return None total_weights = 0 for move, weight, learn in openings: total_weights += weight if total_weights < 1: return None choice = random.randint(0, total_weights - 1) current_sum = 0 for move, weight, learn in openings: current_sum += weight if current_sum > choice: return Move(move)