def computer_move(self): try: self.start_time = time.time() self.src = 0 self.thinking = True # disable some functionality while the computer is thinking Gdk.threads_enter() self.gui.disable_menu_items() Gdk.threads_leave() Gdk.threads_enter() self.gui.set_status_bar_msg(self.get_side_to_move_msg() + '...') Gdk.threads_leave() GObject.timeout_add(200, self.running_display) #GObject.idle_add(self.running_display) pre_board = self.board.get_board_position() self.pre_board = pre_board[:] # Call the engine to make computers move engine.setcomputercolour(self.side_to_move) board_position = engine.cmove(str(self.side_to_move)) self.board.set_board_position(board_position) post_board = self.board.get_board_position() self.post_board = post_board[:] # enable functionality previously disabled Gdk.threads_enter() self.gui.enable_menu_items() Gdk.threads_leave() self.thinking = False # display updated board GObject.timeout_add(200, self.show_computer_move) except: traceback.print_exc()
def computer_move(self): try: self.thinking = True while True: self.thinking = True self.stm = self.get_side_to_move() # update time for last move Gdk.threads_enter() # gv.tc.update_clock() gv.gui.set_side_to_move(self.stm) # set clock ready for move to come # gv.tc.start_clock(self.stm) Gdk.threads_leave() if gv.verbose: print "#" print("# " + self.get_side_to_move_string(self.stm) + " to move") print "#" if self.player[self.stm] == "Human": Gdk.threads_enter() gv.gui.apply_drag_and_drop_settings( self.player[self.stm], self.stm) Gdk.threads_leave() self.thinking = False gv.tc.start_clock(self.stm) return t_start = time.time() if self.player[self.stm] != "gshogi": if self.stm == BLACK: self.usi = gv.usib # usi_opponent = self.usiw # opponent_stm = WHITE else: self.usi = gv.usiw # usi_opponent = self.usib # opponent_stm = BLACK ponder_enabled = gv.engine_manager.get_ponder() if not ponder_enabled: # get engines move self.cmove, self.pondermove[self.stm] = self.usi.cmove( self.movelist, self.stm) else: # pondering ponderhit = False if (self.pondermove[self.stm] is not None and len(self.movelist) > 0): if (self.movelist[-1] == self.pondermove[self.stm] ): ponderhit = True if ponderhit: # ponderhit, wait for return of bestmove and # pondermove self.cmove, self.pondermove[self.stm] = \ self.usi.send_ponderhit(self.stm) else: if self.pondermove[self.stm] is not None: # stop ponder, wait for return of bestmove and # pondermove from ponder bm, pm = self.usi.stop_ponder() # get engines move self.cmove, self.pondermove[self.stm] = \ self.usi.cmove(self.movelist, self.stm) # start pondering if self.pondermove[self.stm] is not None: # send position and ponder command, # return immediately self.usi.start_ponder(self.pondermove[self.stm], self.movelist, self.cmove) if self.stopped or self.cmove is None: self.thinking = False return if gv.verbose: print "computer move is", self.cmove # check if player resigned if self.cmove == "resign": if gv.verbose: print "computer resigned" self.gameover = True self.thinking = False colour = self.get_side_to_move_string(self.stm) msg = "game over - " + colour + " resigned" Gdk.threads_enter() self.stop() gv.gui.set_status_bar_msg(msg) Gdk.threads_leave() self.thinking = False return # engine.setplayer(WHITE) engine.setplayer(self.stm) validmove = engine.hmove(self.cmove) if (not validmove): Gdk.threads_enter() self.stop() gv.gui.set_status_bar_msg( self.cmove + " - computer made illegal Move!") Gdk.threads_leave() self.gameover = True self.thinking = False return if gv.verbose: engine.command("bd") else: if gv.verbose: print "using gshogi builtin engine" # # We are using the builtin gshogi engine (not a USI engine) # if self.player[self.stm ^ 1] == "Human": Gdk.threads_enter() gv.gui.set_status_bar_msg("Thinking ...") Gdk.threads_leave() # set the computer to black or white engine.setplayer(self.stm ^ 1) # start the clock # print "starting clock from gshogi.py" Gdk.threads_enter() gv.tc.start_clock(self.stm) Gdk.threads_leave() # set time limit/level for move in gshogi engine gv.tc.set_gshogi_time_limit(self.stm) # call the gshogi engine to do the move self.cmove = engine.cmove() # update time for last move Gdk.threads_enter() # print "updating clock from gshogi.py" gv.tc.update_clock() gv.gui.set_side_to_move(self.stm) Gdk.threads_leave() if self.quitting: return if self.stopped: self.thinking = False Gdk.threads_enter() gv.gui.set_status_bar_msg("stopped") Gdk.threads_leave() engine.command("undo") return if self.cmove != "": self.movelist.append(self.cmove) self.redolist = [] else: # empty move is returned by gshogi engine when it is in # checkmate if gv.verbose: print "empty move returned by engine" # if the engine moved very fast then wait a bit # before displaying the move. The time to wait # is in MIN_MOVETIME in constants.py t_end = time.time() move_t = t_end - t_start if move_t < MIN_MOVETIME: diff = MIN_MOVETIME - move_t time.sleep(diff) # if program is exitting then quit this thread asap if self.quitting: return # gv.board.save_board(len(self.movelist)) # show computer move Gdk.threads_enter() gv.board.update() self.move_list.update() # highlight the move by changing square colours self.hilite_move(self.cmove) Gdk.threads_leave() # if self.player[self.stm] != "gshogi" # and gv.engine_manager.get_ponder(): # self.usi.send_ponder() # # self.ctp= thread.start_new_thread( # self.usi.send_ponder, () ) if gv.verbose: engine.command("bd") if gv.verbose: print "move=", self.cmove msg = self.cmove self.gameover, gmsg = self.check_for_gameover() if (self.gameover): if (msg == ""): msg = gmsg else: msg = self.get_side_to_move_string( self.stm) + ": " + msg msg = msg + ". " + gmsg self.thinking = False self.stm = self.get_side_to_move() Gdk.threads_enter() self.stop() gv.gui.set_side_to_move(self.stm) gv.gui.set_status_bar_msg(msg) Gdk.threads_leave() return msg = self.get_side_to_move_string(self.stm) + ": " + msg Gdk.threads_enter() gv.gui.set_status_bar_msg(msg) Gdk.threads_leave() self.thinking = False except: traceback.print_exc() return
def computer_move(self): try: self.thinking = True while True: self.thinking = True self.stm = self.get_side_to_move() # update time for last move Gdk.threads_enter() # gv.tc.update_clock() gv.gui.set_side_to_move(self.stm) # set clock ready for move to come # gv.tc.start_clock(self.stm) Gdk.threads_leave() if gv.verbose: print "#" print("# " + self.get_side_to_move_string(self.stm) + " to move") print "#" if self.player[self.stm] == "Human": Gdk.threads_enter() gv.gui.apply_drag_and_drop_settings( self.player[self.stm], self.stm) Gdk.threads_leave() self.thinking = False gv.tc.start_clock(self.stm) return t_start = time.time() if self.player[self.stm] != "gshogi": if self.stm == BLACK: self.usi = gv.usib # usi_opponent = self.usiw # opponent_stm = WHITE else: self.usi = gv.usiw # usi_opponent = self.usib # opponent_stm = BLACK ponder_enabled = gv.engine_manager.get_ponder() if not ponder_enabled: # get engines move self.cmove, self.pondermove[self.stm] = self.usi.cmove( self.movelist, self.stm) else: # pondering ponderhit = False if (self.pondermove[self.stm] is not None and len(self.movelist) > 0): if (self.movelist[-1] == self.pondermove[self.stm]): ponderhit = True if ponderhit: # ponderhit, wait for return of bestmove and # pondermove self.cmove, self.pondermove[self.stm] = \ self.usi.send_ponderhit(self.stm) else: if self.pondermove[self.stm] is not None: # stop ponder, wait for return of bestmove and # pondermove from ponder bm, pm = self.usi.stop_ponder() # get engines move self.cmove, self.pondermove[self.stm] = \ self.usi.cmove(self.movelist, self.stm) # start pondering if self.pondermove[self.stm] is not None: # send position and ponder command, # return immediately self.usi.start_ponder( self.pondermove[self.stm], self.movelist, self.cmove) if self.stopped or self.cmove is None: self.thinking = False return if gv.verbose: print "computer move is", self.cmove # check if player resigned if self.cmove == "resign": if gv.verbose: print "computer resigned" self.gameover = True self.thinking = False colour = self.get_side_to_move_string(self.stm) msg = "game over - " + colour + " resigned" Gdk.threads_enter() self.stop() gv.gui.set_status_bar_msg(msg) Gdk.threads_leave() self.thinking = False return # engine.setplayer(WHITE) engine.setplayer(self.stm) validmove = engine.hmove(self.cmove) if (not validmove): Gdk.threads_enter() self.stop() gv.gui.set_status_bar_msg( self.cmove + " - computer made illegal Move!") Gdk.threads_leave() self.gameover = True self.thinking = False return if gv.verbose: engine.command("bd") else: if gv.verbose: print "using gshogi builtin engine" # # We are using the builtin gshogi engine (not a USI engine) # if self.player[self.stm ^ 1] == "Human": Gdk.threads_enter() gv.gui.set_status_bar_msg("Thinking ...") Gdk.threads_leave() # set the computer to black or white engine.setplayer(self.stm ^ 1) # start the clock # print "starting clock from gshogi.py" Gdk.threads_enter() gv.tc.start_clock(self.stm) Gdk.threads_leave() # set time limit/level for move in gshogi engine gv.tc.set_gshogi_time_limit(self.stm) # call the gshogi engine to do the move self.cmove = engine.cmove() # update time for last move Gdk.threads_enter() # print "updating clock from gshogi.py" gv.tc.update_clock() gv.gui.set_side_to_move(self.stm) Gdk.threads_leave() if self.quitting: return if self.stopped: self.thinking = False Gdk.threads_enter() gv.gui.set_status_bar_msg("stopped") Gdk.threads_leave() engine.command("undo") return if self.cmove != "": self.movelist.append(self.cmove) self.redolist = [] else: # empty move is returned by gshogi engine when it is in # checkmate if gv.verbose: print "empty move returned by engine" # if the engine moved very fast then wait a bit # before displaying the move. The time to wait # is in MIN_MOVETIME in constants.py t_end = time.time() move_t = t_end - t_start if move_t < MIN_MOVETIME: diff = MIN_MOVETIME - move_t time.sleep(diff) # if program is exitting then quit this thread asap if self.quitting: return # gv.board.save_board(len(self.movelist)) # show computer move Gdk.threads_enter() gv.board.update() self.move_list.update() # highlight the move by changing square colours self.hilite_move(self.cmove) Gdk.threads_leave() # if self.player[self.stm] != "gshogi" # and gv.engine_manager.get_ponder(): # self.usi.send_ponder() # # self.ctp= thread.start_new_thread( # self.usi.send_ponder, () ) if gv.verbose: engine.command("bd") if gv.verbose: print "move=", self.cmove msg = self.cmove self.gameover, gmsg = self.check_for_gameover() if (self.gameover): if (msg == ""): msg = gmsg else: msg = self.get_side_to_move_string( self.stm) + ": " + msg msg = msg + ". " + gmsg self.thinking = False self.stm = self.get_side_to_move() Gdk.threads_enter() self.stop() gv.gui.set_side_to_move(self.stm) gv.gui.set_status_bar_msg(msg) Gdk.threads_leave() return msg = self.get_side_to_move_string(self.stm) + ": " + msg Gdk.threads_enter() gv.gui.set_status_bar_msg(msg) Gdk.threads_leave() self.thinking = False except: traceback.print_exc() return