def on_player_resigns(self): display_mbox("The computer thanks you.","Better luck next time!") if(self.model.gamestate.mode == MODE_PLAY_WHITE): self.model.gamestate.current.root().headers["Result"] = "0-1" elif(self.model.gamestate.mode == MODE_PLAY_BLACK): self.model.gamestate.current.root().headers["Result"] = "1-0" self.on_enter_moves_mode() self.mainAppWindow.moves_edit_view.update_san()
def on_player_resigns(self): display_mbox(self.mainAppWindow.trUtf8("The computer thanks you."), self.mainAppWindow.trUtf8("Better luck next time!")) if (self.model.gamestate.mode == MODE_PLAY_WHITE): self.model.gamestate.current.root().headers["Result"] = "0-1" elif (self.model.gamestate.mode == MODE_PLAY_BLACK): self.model.gamestate.current.root().headers["Result"] = "1-0" self.on_enter_moves_mode() self.mainAppWindow.moves_edit_view.update_san()
def handle_offered_draw(self): if( (not self.model.gamestate.score == None) and ( (self.model.gamestate.mode == MODE_PLAY_WHITE and self.model.gamestate.score > 1.1) or (self.model.gamestate.mode == MODE_PLAY_BLACK and self.model.gamestate.score < -1.1)) and self.count_moves(self.model.gamestate.current) > 40): self.model.gamestate.current.root().headers["Result"] = "1/2-1/2" display_mbox("The computer accepts.","The game ends in a draw.") self.on_enter_moves_mode() self.mainAppWindow.moves_edit_view.update_san() else: display_mbox("The computer rejects your offer.","The game continues.")
def handle_offered_draw(self): if ((not self.model.gamestate.score == None) and ((self.model.gamestate.mode == MODE_PLAY_WHITE and self.model.gamestate.score > 1.1) or (self.model.gamestate.mode == MODE_PLAY_BLACK and self.model.gamestate.score < -1.1)) and self.count_moves(self.model.gamestate.current) > 40): self.model.gamestate.current.root().headers["Result"] = "1/2-1/2" display_mbox(self.mainAppWindow.trUtf8("The computer accepts."), self.mainAppWindow.trUtf8("The game ends in a draw.")) self.on_enter_moves_mode() self.mainAppWindow.moves_edit_view.update_san() else: display_mbox( self.mainAppWindow.trUtf8("The computer rejects your offer."), self.mainAppWindow.trUtf8("The game continues."))
def executeMove(self, uci): temp = self.gs.current self.gs.current.invalidate = True move = chess.Move.from_uci(uci) # check if move already exists variation_moves = [ x.move for x in self.gs.current.variations ] if(move in variation_moves): for node in self.gs.current.variations: if(node.move == move): self.gs.current = node # otherwise create a new node else: self.gs.current.add_variation(move) # enforce repainting main variation if exists if(len(self.gs.current.variations)>0 and len(self.gs.current.variations[0].variations)>0): self.gs.current.variations[0].variations[0].invalidate = True self.gs.current = self.gs.current.variation(move) self.emit(SIGNAL("unsaved_changes()")) #"+str(( self.gs.board().turn + 1)%2)) #new_node = chess.pgn.GameNode() #new_node.parent = temp #new_node.move = chess.Move.from_uci(uci) self.moveSrc = None self.grabbedPiece = None self.drawGrabbedPiece = False #self.movesEdit = MovesEdit(self) #self.movesEdit.update_san() # check if game is drawn, or checkmate due to various conditions if(self.gs.current.board().is_checkmate()): # due to checkmate display_mbox(self.trUtf8("Checkmate"),self.trUtf8("The game is over!")) self.emit(SIGNAL("checkmate")) elif(self.gs.current.board().is_stalemate()): # due to stalemate display_mbox(self.trUtf8("Stalemate"),self.trUtf8("The game is drawn!")) self.emit(SIGNAL("drawn")) elif(self.gs.current.board().is_insufficient_material() and not self.gs.mode == MODE_ENTER_MOVES): # due to insufficient material display_mbox(self.trUtf8("Insufficient material to win."),self.trUtf8("The game is drawn!")) self.emit(SIGNAL("drawn")) elif(self.gs.current.board().can_claim_threefold_repetition()): # due to threefold repetition display_mbox(self.trUtf8("Threefold repetition."),self.trUtf8("The game is drawn!")) self.emit(SIGNAL("drawn")) if(self.gs.mode == MODE_ANALYSIS): fen, uci_string = self.gs.printer.to_uci(self.gs.current) self.engine.send_fen(fen) self.engine.uci_send_position(uci_string) self.engine.uci_go_infinite() #self.update() if((self.gs.mode == MODE_PLAY_WHITE and self.gs.current.board().turn == chess.BLACK) or (self.gs.mode == MODE_PLAY_BLACK and self.gs.current.board().turn == chess.WHITE)): book_move = None s = (str(os.listdir("."))) #msgBox = QMessageBox() #msgBox.setText(s) #msgBox.exec_() with open_reader("./books/varied.bin") as reader: #self.debug_msg("ok, openend file") entries = reader.find_all(self.gs.current.board()) moves = [] for entry in entries: move = entry.move().uci() moves.append(move) #self.emit(SIGNAL("bestmove(QString)"),move) l = len(moves) if(l > 0): book_move = True n = random.randint(0,l-1) book_move = moves[n] if(book_move != None): #self.debug_msg("ok, sending book move") self.emit(SIGNAL("bestmove(QString)"),book_move) else: fen, uci_string = self.gs.printer.to_uci(self.gs.current) self.engine.send_fen(fen) self.engine.uci_send_position(uci_string) self.engine.uci_go_movetime(self.gs.computer_think_time) elif(self.gs.mode == MODE_PLAY_WHITE or self.gs.mode == MODE_PLAY_BLACK): pass #self.engine.uci_go_infinite() elif(self.gs.mode == MODE_PLAYOUT_POS): fen, uci_string = self.gs.printer.to_uci(self.gs.current) self.engine.send_fen(fen) self.engine.uci_send_position(uci_string) self.engine.uci_go_movetime(self.gs.computer_think_time) self.update() self.emit(SIGNAL("statechanged()"))
def on_bestmove(self, move): gs = self.model.gamestate mode = self.model.gamestate.mode # handling a best move in playing mode (either human vs comp or comp vs comp) if((mode == MODE_PLAY_BLACK and gs.current.board().turn == chess.WHITE) or (mode == MODE_PLAY_WHITE and gs.current.board().turn == chess.BLACK) or (mode == MODE_PLAYOUT_POS)): if(self.is_lost_by_comp(gs)): display_mbox("The computer resigns.","Congratulations!") self.give_up_game() else: # continue normal play uci = move legal_moves = gs.current.board().legal_moves # test if engine move is actually legal (this should # always be true, unless there is some serious sync # issue between engine and views) if (len([x for x in legal_moves if x.uci() == uci]) > 0): self.mainAppWindow.chessboard_view.executeMove(move) self.mainAppWindow.chessboard_view.on_statechanged() # handling bestmove command if in game analysis mode if(mode == MODE_GAME_ANALYSIS): if(self.exists_better_line(gs) and ((gs.game_analysis_white and gs.current.board().turn == chess.WHITE) or (gs.game_analysis_black and gs.current.board().turn == chess.BLACK))): self.add_variant_from_pv(gs.current,gs.pv) if(gs.next_mate_threat != None): gs.current.variations[0].comment = "#"+str(gs.next_mate_threat) else: gs.current.variations[0].comment = str(gs.best_score) gs.current.variations[1].comment = str(gs.score) # record current evaluations gs.best_score = gs.score gs.pv = [] gs.next_mate_threat = gs.mate_threat gs.mate_threat = None self.mainAppWindow.chessboard_view.on_statechanged() self.mainAppWindow.moves_edit_view.on_statechanged() # continue in that mode, unless we reached the root of # the game, or the parent position is in the book if(gs.current.parent): if((gs.is_current_position_in_book(gs.current.parent))): gs.current.parent.comment = "last book move" gs.current.parent.invalidate = True display_mbox(self.mainAppWindow.trUtf8("Game Analysis Finished"),\ self.mainAppWindow.trUtf8("The analysis is finished.")) self.on_enter_moves_mode() else: gs.current = gs.current.parent # send uci best move command self.on_statechanged() else: gs.mode = MODE_ENTER_MOVES display_mbox(self.mainAppWindow.trUtf8("Game Analysis Finished"),\ self.mainAppWindow.trUtf8("The analysis is finished.")) self.on_enter_moves_mode() # (finished, display messagebox) self.mainAppWindow.moves_edit_view.update_san() self.mainAppWindow.update()
def on_bestmove(self, move): gs = self.model.gamestate mode = self.model.gamestate.mode # handling a best move in playing mode (either human vs comp or comp vs comp) if ((mode == MODE_PLAY_BLACK and gs.current.board().turn == chess.WHITE) or (mode == MODE_PLAY_WHITE and gs.current.board().turn == chess.BLACK) or (mode == MODE_PLAYOUT_POS)): if (self.is_lost_by_comp(gs)): display_mbox( self.mainAppWindow.trUtf8("The computer resigns.", "Congratulations!")) self.give_up_game() else: # continue normal play uci = move legal_moves = gs.current.board().legal_moves # test if engine move is actually legal (this should # always be true, unless there is some serious sync # issue between engine and views) if (len([x for x in legal_moves if x.uci() == uci]) > 0): self.mainAppWindow.chessboard_view.executeMove(move) self.mainAppWindow.chessboard_view.on_statechanged() # handling bestmove command if in game analysis mode if (mode == MODE_GAME_ANALYSIS): if (self.exists_better_line(gs) and ((gs.game_analysis_white and gs.current.board().turn == chess.WHITE) or (gs.game_analysis_black and gs.current.board().turn == chess.BLACK))): self.add_variant_from_pv(gs.current, gs.pv) if (gs.next_mate_threat != None): gs.current.variations[0].comment = "#" + str( gs.next_mate_threat) else: gs.current.variations[0].comment = str(gs.best_score) gs.current.variations[1].comment = str(gs.score) # record current evaluations gs.best_score = gs.score gs.pv = [] gs.next_mate_threat = gs.mate_threat gs.mate_threat = None self.mainAppWindow.chessboard_view.on_statechanged() self.mainAppWindow.moves_edit_view.on_statechanged() # continue in that mode, unless we reached the root of # the game, or the parent position is in the book if (gs.current.parent): if ((gs.is_current_position_in_book(gs.current.parent))): gs.current.parent.comment = "last book move" gs.current.parent.invalidate = True display_mbox(self.mainAppWindow.trUtf8("Game Analysis Finished"),\ self.mainAppWindow.trUtf8("The analysis is finished.")) self.on_enter_moves_mode() else: gs.current = gs.current.parent # send uci best move command self.on_statechanged() else: gs.mode = MODE_ENTER_MOVES display_mbox(self.mainAppWindow.trUtf8("Game Analysis Finished"),\ self.mainAppWindow.trUtf8("The analysis is finished.")) self.on_enter_moves_mode() # (finished, display messagebox) self.mainAppWindow.moves_edit_view.update_san() self.mainAppWindow.update()