예제 #1
0
파일: gshogi.py 프로젝트: gbtami/gshogi
    def undo_single_move(self, b):
        engine.command("undo")
        move = None
        try:
            move = self.movelist.pop()
            self.redolist.append(move)
            self.stm = self.get_side_to_move()
            gv.gui.set_side_to_move(self.stm)
        except IndexError:
            pass

        try:
            gv.usib.stop_engine()
            gv.usiw.stop_engine()
        except:
            pass
        self.gameover = False
        gv.board.update()
        # set move list window to last move
        self.move_list.set_move(len(self.movelist))
        if move is not None:
            gv.gui.set_status_bar_msg("(" + move + ")")
            # highlight the move by changing square colours
            self.hilite_move(move)
        else:
            gv.gui.unhilite_squares()
예제 #2
0
파일: gshogi.py 프로젝트: gbtami/gshogi
    def undo_single_move(self, b):
        engine.command("undo")
        move = None
        try:
            move = self.movelist.pop()
            self.redolist.append(move)
            self.stm = self.get_side_to_move()
            gv.gui.set_side_to_move(self.stm)
        except IndexError:
            pass

        try:
            gv.usib.stop_engine()
            gv.usiw.stop_engine()
        except:
            pass
        self.gameover = False
        gv.board.update()
        # set move list window to last move
        self.move_list.set_move(len(self.movelist))
        if move is not None:
            gv.gui.set_status_bar_msg("(" + move + ")")
            # highlight the move by changing square colours
            self.hilite_move(move)
        else:
            gv.gui.unhilite_squares()
예제 #3
0
    def human_move(self, move):
        self.movelist.append(move)
        self.redolist = []
        #self.board.save_board(len(self.movelist))

        # highlight the move by changing square colours
        self.hilite_move(move)

        self.board.update()
        # update move list in move list window
        self.move_list.update()

        if self.verbose: engine.command('bd')
        self.src = ''

        self.gameover, msg = self.check_for_gameover()
        if (self.gameover):
            self.stop()
            self.gui.set_status_bar_msg(msg)
            self.thinking = False
            return

        #self.gui.set_status_bar_msg('Thinking ...')
        #if self.verbose: print "--------------------------------------------------------------------------------"
        #print "whites move"
        self.stm = self.get_side_to_move()
        self.gui.set_side_to_move(self.stm)
        if self.verbose:
            print "#"
            print "# " + self.get_side_to_move_string(self.stm) + " to move"
            print "#"
        self.gui.set_status_bar_msg(" ")

        self.src = ''

        # update time for last move
        self.tc.update_clock()

        self.gui.apply_drag_and_drop_settings(self.player[self.stm], self.stm)

        if self.player[self.stm] == "Human":

            # set clock ready for move to come
            self.tc.start_clock(self.stm)

            if not self.timer_active:
                gobject.timeout_add(1000, self.tc.show_time)

            return

        self.thinking = True
        # disable some functionality while the computer is thinking
        #self.gui.disable_menu_items()

        # It's the computers turn to move
        # kick off a separate thread for computers move so that gui is still useable
        self.ct = thread.start_new_thread(self.computer_move, ())

        return
예제 #4
0
파일: gshogi.py 프로젝트: nestukh/gshogi
    def human_move(self, move):        
        self.movelist.append(move)
        self.redolist = []        
        #self.board.save_board(len(self.movelist))

        # highlight the move by changing square colours
        self.hilite_move(move)       
        
        self.board.update()
        # update move list in move list window
        self.move_list.update()

        if self.verbose: engine.command('bd')
        self.src = ''

        self.gameover, msg = self.check_for_gameover() 
        if (self.gameover):
            self.stop()               
            self.gui.set_status_bar_msg(msg)                
            self.thinking = False  
            return                   
                
        #self.gui.set_status_bar_msg('Thinking ...')  
        #if self.verbose: print "--------------------------------------------------------------------------------"
        #print "whites move"
        self.stm = self.get_side_to_move()
        self.gui.set_side_to_move(self.stm)
        if self.verbose:
            print "#"
            print "# " + self.get_side_to_move_string(self.stm) + " to move"
            print "#"                
        self.gui.set_status_bar_msg(" ")                
                
        self.src = ''

        # update time for last move
        self.tc.update_clock()

        self.gui.apply_drag_and_drop_settings(self.player[self.stm], self.stm)   

        if self.player[self.stm] == "Human":                     

            # set clock ready for move to come   
            self.tc.start_clock(self.stm)            

            if not self.timer_active:
                gobject.timeout_add(1000, self.tc.show_time)            

            return
            
        self.thinking = True
        # disable some functionality while the computer is thinking
        #self.gui.disable_menu_items()   

        # It's the computers turn to move
        # kick off a separate thread for computers move so that gui is still useable                                      
        self.ct= thread.start_new_thread( self.computer_move, () )
                                        
        return
예제 #5
0
파일: gshogi.py 프로젝트: gbtami/gshogi
 def undo_move(self):
     engine.command("undo")
     move = None
     try:
         move = self.movelist.pop()
         self.redolist.append(move)
     except IndexError:
         pass
예제 #6
0
def parser(command):
    #Going to be replaced with the actual game logic
    global imgfile
    global image
    engine.command(command)
    if command == 'quit':
        return False
    return True
예제 #7
0
파일: gshogi.py 프로젝트: gbtami/gshogi
 def undo_move(self):
     engine.command("undo")
     move = None
     try:
         move = self.movelist.pop()
         self.redolist.append(move)
     except IndexError:
         pass
예제 #8
0
파일: gshogi.py 프로젝트: nestukh/gshogi
    def new_game(self, menu_name):
        
        self.gameover = False
        engine.command('new')                

        if menu_name == 'NewGame':
            # Normal Game (No handicap)
            self.startpos = 'startpos'
            self.start_stm = BLACK
        else:
            # Handicap Game
            self.start_stm = WHITE
            if menu_name == 'LanceHandicap':           
                sfen = 'lnsgkgsn1/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'                  
            elif menu_name == 'BishopHandicap':            
                sfen = 'lnsgkgsnl/1r7/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'                  
            elif menu_name == 'RookHandicap':           
                sfen = 'lnsgkgsnl/7b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'                   
            elif menu_name == 'RookandLanceHandicap':            
                sfen = 'lnsgkgsn1/7b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'                  
            elif menu_name == 'TwoPieceHandicap':            
                sfen = 'lnsgkgsnl/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'                  
            elif menu_name == 'FourPieceHandicap':            
                sfen = '1nsgkgsn1/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'                  
            elif menu_name == 'SixPieceHandicap':            
                sfen = '2sgkgs2/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'                 
            elif menu_name == 'EightPieceHandicap':            
                sfen = '3gkg3/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'                 
            elif menu_name == 'TenPieceHandicap':           
                sfen = '4k4/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'               
            else:
                self.gui.info_box('Error. Invalid menu name:' + menu_name)               
                return
            
            engine.setfen(sfen)
            self.startpos = sfen 
 
        self.board.update()
        # update move list in move list window
        self.move_list.update()
        if not BEEP:
            engine.command('beep')        

        self.usib.set_newgame()
        self.usiw.set_newgame()        
        self.movelist = []
        self.redolist = [] 
        self.gui.set_status_bar_msg('') 
        self.stm = self.get_side_to_move()
        self.gui.set_side_to_move(self.stm)
        self.gui.unhilite_squares()        
                               
        self.tc.reset_clock()
예제 #9
0
    def new_game(self, menu_name):

        self.gameover = False
        engine.command('new')

        if menu_name == 'NewGame':
            # Normal Game (No handicap)
            self.startpos = 'startpos'
            self.start_stm = BLACK
        else:
            # Handicap Game
            self.start_stm = WHITE
            if menu_name == 'LanceHandicap':
                sfen = 'lnsgkgsn1/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'
            elif menu_name == 'BishopHandicap':
                sfen = 'lnsgkgsnl/1r7/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'
            elif menu_name == 'RookHandicap':
                sfen = 'lnsgkgsnl/7b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'
            elif menu_name == 'RookandLanceHandicap':
                sfen = 'lnsgkgsn1/7b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'
            elif menu_name == 'TwoPieceHandicap':
                sfen = 'lnsgkgsnl/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'
            elif menu_name == 'FourPieceHandicap':
                sfen = '1nsgkgsn1/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'
            elif menu_name == 'SixPieceHandicap':
                sfen = '2sgkgs2/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'
            elif menu_name == 'EightPieceHandicap':
                sfen = '3gkg3/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'
            elif menu_name == 'TenPieceHandicap':
                sfen = '4k4/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'
            else:
                self.gui.info_box('Error. Invalid menu name:' + menu_name)
                return

            engine.setfen(sfen)
            self.startpos = sfen

        self.board.update()
        # update move list in move list window
        self.move_list.update()
        if not BEEP:
            engine.command('beep')

        self.usib.set_newgame()
        self.usiw.set_newgame()
        self.movelist = []
        self.redolist = []
        self.gui.set_status_bar_msg('')
        self.stm = self.get_side_to_move()
        self.gui.set_side_to_move(self.stm)
        self.gui.unhilite_squares()

        self.tc.reset_clock()
예제 #10
0
    def __init__(self):

        self.verbose = False
        self.verbose_usi = False
        for arg in sys.argv:
            if arg == '-v' or arg == '--verbose':
                self.verbose = True
            if arg == '-vusi':
                self.verbose_usi = True

        self.ask_before_promoting = False
        self.gameover = False
        self.time_limit = '00:10'
        self.stopped = True
        self.quitting = False
        self.src = ''
        self.src_x = ''
        self.src_y = ''
        self.startpos = 'startpos'
        self.start_stm = BLACK

        self.search_depth = 39
        self.thinking = False
        self.cmove = 'none'
        self.movelist = []
        self.redolist = []
        self.player = ["Human", "gshogi"]
        self.pondermove = [None, None]

        # set paths to images. opening book etc
        self.set_data_paths()
        opening_book_path = os.path.join(self.prefix, "data/opening.bbk")
        engine.init(opening_book_path, self.verbose)

        self.glade_dir = os.path.join(self.prefix, 'glade')

        utils.set_game_ref(self)

        # usiw is the instance that plays white (gote)
        # usib is the instance that plays black (sente)
        self.usib = usi.Usi(self.verbose, self.verbose_usi, 'b')
        self.usiw = usi.Usi(self.verbose, self.verbose_usi, 'w')
        utils.set_usi_refs(self.usib, self.usiw)

        # instantiate board, gui, classes
        self.tc = time_control.Time_Control(self.verbose)
        utils.set_tc_ref(self.tc)
        self.engine_manager = engine_manager.Engine_Manager(self.verbose)
        self.board = utils.get_board_ref()
        self.pieces = utils.get_pieces_ref()

        self.gui = utils.get_gui_ref()
        self.board.set_refs(self, self.gui)
        self.gui.set_refs(self, self.board, self.engine_manager, self.tc)
        self.gui.build_gui()
        self.board.build_board()
        self.engine_manager.set_refs(self, self.gui, self.usib, self.usiw)
        self.usib.set_refs(self, self.engine_manager, self.gui, self.tc)
        self.usiw.set_refs(self, self.engine_manager, self.gui, self.tc)
        self.tc.set_refs(self, self.gui)
        self.set_board_colours = set_board_colours.get_ref()
        self.engine_output = engine_output.get_ref()

        # set level
        command = 'level 0 ' + self.time_limit
        engine.command(command)
        # turn off beeps
        if not BEEP:
            engine.command('beep')

        # restore users settings to values from previous game
        self.restore_settings()

        self.usib.set_engine(self.player[BLACK], None)
        self.usiw.set_engine(self.player[WHITE], None)
        self.gui.update_toolbar(self.player)

        self.move_list = move_list.get_ref()

        self.tc.reset_clock()

        self.gui.enable_go_button()
        self.gui.disable_stop_button()

        self.stm = self.get_side_to_move()

        self.timer_active = False
예제 #11
0
파일: gshogi.py 프로젝트: gbtami/gshogi
    def new_game(self, menu_name):

        self.gameover = False
        engine.command("new")

        if menu_name == "NewGame":
            # Normal Game (No handicap)
            self.startpos = "startpos"
            self.start_stm = BLACK
        else:
            # Handicap Game
            self.start_stm = WHITE
            if menu_name == "LanceHandicap":
                sfen = "lnsgkgsn1/1r5b1/ppppppppp/9/9/9/" \
                       "PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
            elif menu_name == "BishopHandicap":
                sfen = "lnsgkgsnl/1r7/ppppppppp/9/9/9/" \
                       "PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
            elif menu_name == "RookHandicap":
                sfen = "lnsgkgsnl/7b1/ppppppppp/9/9/9/" \
                       "PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
            elif menu_name == "RookandLanceHandicap":
                sfen = "lnsgkgsn1/7b1/ppppppppp/9/9/9/" \
                       "PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
            elif menu_name == "TwoPieceHandicap":
                sfen = "lnsgkgsnl/9/ppppppppp/9/9/9/" \
                       "PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
            elif menu_name == "FourPieceHandicap":
                sfen = "1nsgkgsn1/9/ppppppppp/9/9/9/" \
                       "PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
            elif menu_name == "SixPieceHandicap":
                sfen = "2sgkgs2/9/ppppppppp/9/9/9/" \
                       "PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
            elif menu_name == "EightPieceHandicap":
                sfen = "3gkg3/9/ppppppppp/9/9/9/" \
                       "PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
            elif menu_name == "TenPieceHandicap":
                sfen = "4k4/9/ppppppppp/9/9/9/" \
                       "PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
            else:
                gv.gui.info_box("Error. Invalid menu name:" + menu_name)
                return

            engine.setfen(sfen)
            self.startpos = sfen

        gv.board.update()
        # update move list in move list window
        self.move_list.update()
        if not BEEP:
            engine.command("beep")

        gv.usib.set_newgame()
        gv.usiw.set_newgame()
        self.movelist = []
        self.redolist = []
        gv.gui.set_status_bar_msg("")
        self.stm = self.get_side_to_move()
        gv.gui.set_side_to_move(self.stm)
        gv.gui.unhilite_squares()

        gv.tc.reset_clock()
예제 #12
0
파일: gshogi.py 프로젝트: gbtami/gshogi
    def __init__(self):
        gv.gshogi = self
        # set global variables for debug messages
        gv.verbose, gv.verbose_usi = utils.get_verbose()
        # prefix to find package files/folders
        self.prefix = utils.get_prefix()
        # set up .gshogi directory in the home directory
        self.gshogipath = utils.create_settings_dir()
        self.glade_dir = os.path.join(self.prefix, "glade")
        # get settings saved from previous game
        self.settings = utils.get_settings_from_file(self.gshogipath)

        self.ask_before_promoting = False
        self.gameover = False
        self.time_limit = "00:10"
        self.stopped = True
        self.quitting = False
        self.src = ""
        self.src_x = ""
        self.src_y = ""
        self.startpos = "startpos"
        self.start_stm = BLACK

        self.search_depth = 39
        self.thinking = False
        self.cmove = "none"
        self.movelist = []
        self.redolist = []
        self.player = ["Human", "gshogi"]
        self.pondermove = [None, None]

        opening_book_path = os.path.join(self.prefix, "data/opening.bbk")
        engine.init(opening_book_path, gv.verbose)

        gv.gui = gui.Gui()

        gv.pieces = pieces.Pieces()
        # custom pieceset path
        if self.settings != "":
            gv.pieces.set_custom_pieceset_path(
                self.settings.custom_pieceset_path)
        gv.pieces.load_pieces(self.prefix)

        # usiw is the instance that plays white (gote)
        # usib is the instance that plays black (sente)
        gv.usib = usi.Usi("b")
        gv.usiw = usi.Usi("w")

        # instantiate board, gui, classes
        gv.tc = time_control.Time_Control()
        gv.engine_manager = engine_manager.Engine_Manager()
        gv.board = board.Board()

        self.set_board_colours = set_board_colours.get_ref()
        # set colours to previous game (if any)
        if self.settings != "":
            self.set_board_colours.restore_colour_settings(
                self.settings.colour_settings)
        gv.gui.build_gui()
        gv.board.build_board()
        self.engine_output = engine_output.get_ref()

        # set level
        command = "level 0 " + self.time_limit
        engine.command(command)
        # turn off beeps
        if not BEEP:
            engine.command("beep")

        # restore users settings to values from previous game
        self.restore_settings(self.settings)

        gv.usib.set_engine(self.player[BLACK], None)
        gv.usiw.set_engine(self.player[WHITE], None)
        gv.gui.update_toolbar(self.player)

        self.move_list = move_list.get_ref()

        gv.tc.reset_clock()

        gv.gui.enable_go_button()
        gv.gui.disable_stop_button()

        self.stm = self.get_side_to_move()
        self.timer_active = False
        self.set_board_colours.apply_colour_settings()
예제 #13
0
파일: gshogi.py 프로젝트: gbtami/gshogi
    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
예제 #14
0
    def set_gshogi_time_limit(self, stm):

        #
        # set engine depth/nodes/time to maximum
        #
        engine.depth(39)
        engine.nodes(0)
        engine.command("level 0 59:59")

        # byoyomi
        if self.type == 0:
            if stm == WHITE:
                gs_movetime = self.wtime
            else:
                gs_movetime = self.btime
            gs_byoyomi = self.byo_byoyomi * 1000
            if gs_movetime > 0:
                gs_movetime = gs_movetime / 80
            elif gs_byoyomi > 0:
                gs_movetime = gs_byoyomi
            else:
                # no time left - set to 1 second
                gs_movetime = 1000

            # if less than 1 second set to 1 second
            if gs_movetime < 1000:
                gs_movetime = 1000

            gs_secs = gs_movetime / 1000
            gs_mins = gs_secs / 60
            if gs_mins > 59:
                gs_mins = 59
                gs_secs = 59
            elif gs_mins > 0:
                gs_secs = gs_secs % (gs_mins * 60)

            gs_mins = int(gs_mins)
            gs_secs = int(gs_secs)

            gs_mins_str = str(gs_mins)
            gs_secs_str = str(gs_secs)
            if gs_mins < 10:
                gs_mins_str = "0" + gs_mins_str
            if gs_secs < 10:
                gs_secs_str = "0" + gs_secs_str

            gs_level = gs_mins_str + ":" + gs_secs_str

            if gv.verbose:
                print("using byoyomi TC - wtime:", self.wtime, ", btime:",
                      self.btime, ", byoyomi:", self.byo_byoyomi * 1000,
                      ", stm:", stm, ", time for move (ms):", gs_movetime)
            command = "level 0 " + gs_level
            # print "gshogi time limit:",command

            # e.g.
            # time limit of 10 seconds per move
            # level 0 00:10
            #
            # time limit of 10 moves in 2 minutes
            # level 10 2

            engine.command(command)
        # classical
        elif self.type == 1:
            if stm == WHITE:
                gs_movetime = self.wtime
            else:
                gs_movetime = self.btime
            # if less than 1 second set to 1 second
            if gs_movetime < 1000:
                gs_movetime = 1000

            gs_secs = gs_movetime / 1000
            gs_mins = gs_secs / 60
            if gs_mins > 0:
                gs_secs = gs_secs % (gs_mins * 60)

            gs_mins = int(gs_mins)
            gs_secs = int(gs_secs)

            gs_mins_str = str(gs_mins)
            gs_secs_str = str(gs_secs)
            if gs_mins < 10:
                gs_mins_str = "0" + gs_mins_str
            if gs_secs < 10:
                gs_secs_str = "0" + gs_secs_str

            gs_level = gs_mins_str + ":" + gs_secs_str
            if gv.verbose:
                print("using classical TC - wtime:", self.wtime, ", btime:",
                      self.btime, ", movestogo:", str(self.wmoves_to_go),
                      ", stm:", stm, ", time for move (ms):", gs_movetime)
                print("                   - ", str(self.wmoves_to_go),
                      " moves in ", gs_mins_str, "minutes", gs_secs_str,
                      " seconds")

            if self.wmoves_to_go == 1:
                command = "level 0 " + gs_level
            else:
                command = "level " + str(self.wmoves_to_go) + " " + gs_level
            engine.command(command)
        # incremental
        elif self.type == 2:
            if stm == WHITE:
                gs_movetime = self.wtime
            else:
                gs_movetime = self.btime

            if gs_movetime > 0:
                gs_movetime = gs_movetime / 80
            else:
                # no time left - set to 1 second
                gs_movetime = 1000

            gs_secs = gs_movetime / 1000
            gs_mins = gs_secs / 60
            if gs_mins > 0:
                gs_secs = gs_secs % (gs_mins * 60)

            gs_mins = int(gs_mins)
            gs_secs = int(gs_secs)

            if gs_mins <= 0 and gs_secs < 1:
                gs_secs = 1

            gs_mins_str = str(gs_mins)
            gs_secs_str = str(gs_secs)
            if gs_mins < 10:
                gs_mins_str = "0" + gs_mins_str
            if gs_secs < 10:
                gs_secs_str = "0" + gs_secs_str

            gs_level = gs_mins_str + ":" + gs_secs_str
            if gv.verbose:
                print("using incremental TC - wtime:", self.wtime, ", btime:",
                      self.btime, ", winc/binc:", str(self.inc_bonus * 1000),
                      ", stm:", stm, ", time for move (ms):", gs_movetime)

            command = "level 0 " + gs_level
            engine.command(command)
        # fixed time per move
        elif self.type == 3:
            gs_movetime = self.ftpm_seconds * 1000

            gs_secs = gs_movetime / 1000
            gs_mins = gs_secs / 60
            if gs_mins > 0:
                gs_secs = gs_secs % (gs_mins * 60)

            gs_mins = int(gs_mins)
            gs_secs = int(gs_secs)

            if gs_mins <= 0 and gs_secs < 1:
                gs_secs = 1

            gs_mins_str = str(gs_mins)
            gs_secs_str = str(gs_secs)
            if gs_mins < 10:
                gs_mins_str = "0" + gs_mins_str
            if gs_secs < 10:
                gs_secs_str = "0" + gs_secs_str

            gs_level = gs_mins_str + ":" + gs_secs_str
            if gv.verbose:
                print("using fixed time per move TC - wtime:", self.wtime,
                      ", btime:", self.btime, " stm:", stm,
                      ", time for move (ms):", gs_movetime)

            command = "level 0 " + gs_level
            engine.command(command)
        # fixed search depth
        elif self.type == 4:
            idepth = self.dpth_depth
            if idepth > 39:
                if gv.verbose:
                    print("search depth (", idepth,
                          ") exceeds max for gshogi engine, setting to 39")
                idepth = 39
            if gv.verbose:
                print "setting depth for gshogi engine to", idepth
            engine.depth(idepth)
        # fixed nodes search
        elif self.type == 6:
            inodes = self.nodes_nodes

            if gv.verbose:
                print "setting nodes for gshogi engine to", inodes
                print "time=", time.time()
            engine.nodes(inodes)
            command = "level 0 59:59"
            engine.command(command)
예제 #15
0
    def set_gshogi_time_limit(self, stm):

        #
        # set engine depth/nodes/time to maximum
        #
        engine.depth(39)
        engine.nodes(0)
        engine.command("level 0 59:59")

        # byoyomi
        if self.type == 0:
            if stm == WHITE:
                gs_movetime = self.wtime
            else:
                gs_movetime = self.btime
            gs_byoyomi = self.byo_byoyomi * 1000
            if gs_movetime > 0:
                gs_movetime = gs_movetime / 80
            elif gs_byoyomi > 0:
                gs_movetime = gs_byoyomi
            else:
                # no time left - set to 1 second
                gs_movetime = 1000

            # if less than 1 second set to 1 second
            if gs_movetime < 1000:
                gs_movetime = 1000

            gs_secs = gs_movetime / 1000
            gs_mins = gs_secs / 60
            if gs_mins > 59:
                gs_mins = 59
                gs_secs = 59
            elif gs_mins > 0:
                gs_secs = gs_secs % (gs_mins * 60)

            gs_mins = int(gs_mins)
            gs_secs = int(gs_secs)

            gs_mins_str = str(gs_mins)
            gs_secs_str = str(gs_secs)
            if gs_mins < 10:
                gs_mins_str = "0" + gs_mins_str
            if gs_secs < 10:
                gs_secs_str = "0" + gs_secs_str

            gs_level = gs_mins_str + ":" + gs_secs_str

            if gv.verbose:
                print("using byoyomi TC - wtime:", self.wtime, ", btime:",
                      self.btime, ", byoyomi:", self.byo_byoyomi * 1000,
                      ", stm:", stm, ", time for move (ms):", gs_movetime)
            command = "level 0 " + gs_level
            # print "gshogi time limit:",command

            # e.g.
            # time limit of 10 seconds per move
            # level 0 00:10
            #
            # time limit of 10 moves in 2 minutes
            # level 10 2

            engine.command(command)
        # classical
        elif self.type == 1:
            if stm == WHITE:
                gs_movetime = self.wtime
            else:
                gs_movetime = self.btime
            # if less than 1 second set to 1 second
            if gs_movetime < 1000:
                gs_movetime = 1000

            gs_secs = gs_movetime / 1000
            gs_mins = gs_secs / 60
            if gs_mins > 0:
                gs_secs = gs_secs % (gs_mins * 60)

            gs_mins = int(gs_mins)
            gs_secs = int(gs_secs)

            gs_mins_str = str(gs_mins)
            gs_secs_str = str(gs_secs)
            if gs_mins < 10:
                gs_mins_str = "0" + gs_mins_str
            if gs_secs < 10:
                gs_secs_str = "0" + gs_secs_str

            gs_level = gs_mins_str + ":" + gs_secs_str
            if gv.verbose:
                print("using classical TC - wtime:", self.wtime, ", btime:",
                      self.btime, ", movestogo:", str(self.wmoves_to_go),
                      ", stm:", stm, ", time for move (ms):", gs_movetime)
                print("                   - ", str(self.wmoves_to_go),
                      " moves in ", gs_mins_str, "minutes", gs_secs_str,
                      " seconds")

            if self.wmoves_to_go == 1:
                command = "level 0 " + gs_level
            else:
                command = "level " + str(self.wmoves_to_go) + " " + gs_level
            engine.command(command)
        # incremental
        elif self.type == 2:
            if stm == WHITE:
                gs_movetime = self.wtime
            else:
                gs_movetime = self.btime

            if gs_movetime > 0:
                gs_movetime = gs_movetime / 80
            else:
                # no time left - set to 1 second
                gs_movetime = 1000

            gs_secs = gs_movetime / 1000
            gs_mins = gs_secs / 60
            if gs_mins > 0:
                gs_secs = gs_secs % (gs_mins * 60)

            gs_mins = int(gs_mins)
            gs_secs = int(gs_secs)

            if gs_mins <= 0 and gs_secs < 1:
                gs_secs = 1

            gs_mins_str = str(gs_mins)
            gs_secs_str = str(gs_secs)
            if gs_mins < 10:
                gs_mins_str = "0" + gs_mins_str
            if gs_secs < 10:
                gs_secs_str = "0" + gs_secs_str

            gs_level = gs_mins_str + ":" + gs_secs_str
            if gv.verbose:
                print("using incremental TC - wtime:", self.wtime, ", btime:",
                      self.btime, ", winc/binc:", str(self.inc_bonus * 1000),
                      ", stm:", stm, ", time for move (ms):", gs_movetime)

            command = "level 0 " + gs_level
            engine.command(command)
        # fixed time per move
        elif self.type == 3:
            gs_movetime = self.ftpm_seconds * 1000

            gs_secs = gs_movetime / 1000
            gs_mins = gs_secs / 60
            if gs_mins > 0:
                gs_secs = gs_secs % (gs_mins * 60)

            gs_mins = int(gs_mins)
            gs_secs = int(gs_secs)

            if gs_mins <= 0 and gs_secs < 1:
                gs_secs = 1

            gs_mins_str = str(gs_mins)
            gs_secs_str = str(gs_secs)
            if gs_mins < 10:
                gs_mins_str = "0" + gs_mins_str
            if gs_secs < 10:
                gs_secs_str = "0" + gs_secs_str

            gs_level = gs_mins_str + ":" + gs_secs_str
            if gv.verbose:
                print("using fixed time per move TC - wtime:", self.wtime,
                      ", btime:", self.btime, " stm:", stm,
                      ", time for move (ms):", gs_movetime)

            command = "level 0 " + gs_level
            engine.command(command)
        # fixed search depth
        elif self.type == 4:
            idepth = self.dpth_depth
            if idepth > 39:
                if gv.verbose:
                    print("search depth (", idepth,
                          ") exceeds max for gshogi engine, setting to 39")
                idepth = 39
            if gv.verbose:
                print "setting depth for gshogi engine to", idepth
            engine.depth(idepth)
        # fixed nodes search
        elif self.type == 6:
            inodes = self.nodes_nodes

            if gv.verbose:
                print "setting nodes for gshogi engine to", inodes
                print "time=", time.time()
            engine.nodes(inodes)
            command = "level 0 59:59"
            engine.command(command)
예제 #16
0
파일: psn.py 프로젝트: nestukh/gshogi
    def load_game_psn_from_str(self, gamestr):

        cnt = 0
        comment_active = False
        movelist = []
        redolist = []
        startpos = 'startpos'
        engine.command('new')
        stm = BLACK

        movecnt = 0

        self.gamestr = gamestr
        self.game_len = len(self.gamestr)

        self.comments.clear_comments()

        # First header should be near the start of the file
        # Note the first chars in the file may be a BOM entry and
        # we need to skip these

        ptr = 0

        # Find '[' of first header
        while 1:
            if ptr >= self.game_len or ptr > 2000:
                self.gui.info_box("Error (1) loading file. No headers found")
                self.game.new_game('NewGame')
                self.gui.set_status_bar_msg("Error loading game")
                return 1
            if gamestr[ptr] == '[':
                break
            ptr += 1

        # First Header found (ptr pointing at '[')
        hdr, newptr = self.get_header(ptr)
        if hdr is None:
            self.gui.info_box("Error (2) loading file. No headers found")
            self.game.new_game('NewGame')
            self.gui.set_status_bar_msg("Error loading game")
            return 1

        # Read remaining headers
        while hdr is not None:
            ptr = newptr
            try:
                prop, value = hdr.split(None, 1)
            except ValueError:
                # Error - cannot split header into a property/value pair
                self.gui.info_box("Error loading file. Invalid header:" + hdr)
                self.game.new_game('NewGame')
                self.gui.set_status_bar_msg("Error loading game")
                return 1
            if prop == 'SFEN':
                # set board position, side to move
                startpos, stm = self.process_sfen(value)
            elif prop == "Handicap":

                handicap = value.strip('"')
                handicap = handicap.lower()

                sfen = ''
                if handicap == 'lance':
                    sfen = 'lnsgkgsn1/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'
                elif handicap == 'bishop':
                    sfen = 'lnsgkgsnl/1r7/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'
                elif handicap == 'rook':
                    sfen = 'lnsgkgsnl/7b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'
                elif handicap == 'rook+lance':
                    sfen = 'lnsgkgsn1/7b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'
                elif handicap == 'rook+bishop':
                    sfen = 'lnsgkgsnl/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'
                elif handicap == 'four piece':
                    sfen = '1nsgkgsn1/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'
                elif handicap == 'six piece':
                    sfen = '2sgkgs2/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'
                elif handicap == 'eight piece':
                    sfen = '3gkg3/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'
                elif handicap == 'ten piece':
                    sfen = '4k4/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'

                if sfen != '':
                    startpos, stm = self.process_sfen(sfen)

            hdr, newptr = self.get_header(ptr)

        # Moves
        while 1:

            if self.verbose:
                print
                print "Processing move number (ply):" + str(movecnt + 1)

            newptr = self.skip_whitespace(ptr)
            if newptr is None:
                break
            ptr = newptr

            # if we get a header here then it must be a multi-game file
            # We cannot process multi-game so treat it as eof and exit
            if gamestr[ptr] == '[':
                break

            # Check for stuff to ignore
            # ignore it and continue processing
            ignore_string, newptr = self.get_ignore_string(ptr)
            if ignore_string is not None:
                #  Can get '1....' to indicate white to move first
                #if ignore_string == '1....' and movecnt == 0:
                #    # Use sfen for initial position but set stm to white
                #    startpos, stm = self.process_sfen("lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1")
                ptr = newptr
                continue

            # comment
            if self.gamestr[ptr] == '{':
                comment = ''
                ptr += 1
                while 1:
                    if ptr >= self.game_len:
                        self.gui.info_box("Error unterminated comment")
                        self.game.new_game('NewGame')
                        self.gui.set_status_bar_msg("Error loading game")
                        return 1  # end of file before end of comment
                    if self.gamestr[ptr] == '}':
                        ptr += 1
                        break
                    comment += self.gamestr[ptr]
                    ptr += 1

                # add comment
                self.comments.set_comment(movecnt, comment)
                continue

            # move number
            # we do not use it
            moveno, newptr = self.get_moveno(ptr)
            if moveno is not None:
                ptr = newptr
                continue

            # move
            move, newptr = self.get_move(ptr, movelist, stm)
            if move is not None:
                ptr = newptr

                engine.setplayer(stm)
                stm = stm ^ 1
                if self.verbose: print "move=", move
                validmove = engine.hmove(move)
                if (not validmove):
                    # Should never get this message since get_move has already
                    # validated the move aginst the legal move list
                    # Can get it for sennichite (repetition)
                    self.gui.info_box(
                        "Error loading file, illegal move (possible sennichite (repetition)):"
                        + move + " move number:" + str(movecnt + 1))
                    self.game.new_game('NewGame')
                    self.gui.set_status_bar_msg("Error loading game")
                    return 1
                movecnt += 1
                movelist.append(move)
                if self.verbose: engine.command('bd')

                continue

            # Invalid - Display Error
            # If we get here we have an invalid word
            # Errors here will stop the processing and prevent the file loading
            # If the invalid word should be skipped instead then add it to the
            # ignore processing above
            word, newptr = self.get_word(ptr)
            if word is not None:
                self.gui.info_box(
                    "Error loading file, unable to process move number " +
                    str(movecnt + 1) + ". (illegal move or invalid format):" +
                    word)
                # Load failed part way through so reset the game
                self.game.new_game('NewGame')
                self.gui.set_status_bar_msg("Error loading game")
                return 1

            ptr += 1

        self.usib.set_newgame()
        self.usiw.set_newgame()
        self.gui.set_status_bar_msg("game loaded")
        self.gameover = False

        self.game.set_movelist(movelist)
        self.game.set_redolist(redolist)
        self.game.set_startpos(startpos)

        self.board.update()

        # update move list in move list window
        self.move_list.update()
        stm = self.game.get_side_to_move()
        self.game.set_side_to_move(stm)
        self.gui.set_side_to_move(stm)
        self.gui.unhilite_squares()

        self.tc.reset_clock()

        return 0
예제 #17
0
파일: gshogi.py 프로젝트: nestukh/gshogi
    def __init__(self):

        self.verbose = False
        self.verbose_usi = False
        for arg in sys.argv:
            if arg == '-v' or arg == '--verbose':
                self.verbose = True
            if arg == '-vusi':
                self.verbose_usi = True

        self.ask_before_promoting = False
        self.gameover = False       
        self.time_limit = '00:10'
        self.stopped = True
        self.quitting = False        
        self.src = ''
        self.src_x = ''
        self.src_y = ''
        self.startpos = 'startpos'
        self.start_stm = BLACK        

        self.search_depth = 39
        self.thinking = False
        self.cmove = 'none'
        self.movelist = []
        self.redolist = []        
        self.player = ["Human", "gshogi"]
        self.pondermove = [None, None]

       # set paths to images. opening book etc        
        self.set_data_paths() 
        opening_book_path = os.path.join(self.prefix, "data/opening.bbk")              
        engine.init(opening_book_path, self.verbose)

        self.glade_dir = os.path.join(self.prefix, 'glade')             
        
        utils.set_game_ref(self)            

        # usiw is the instance that plays white (gote)
        # usib is the instance that plays black (sente)
        self.usib = usi.Usi(self.verbose, self.verbose_usi, 'b')
        self.usiw = usi.Usi(self.verbose, self.verbose_usi, 'w')
        utils.set_usi_refs(self.usib, self.usiw)  

        # instantiate board, gui, classes 
        self.tc = time_control.Time_Control(self.verbose) 
        utils.set_tc_ref(self.tc)
        self.engine_manager = engine_manager.Engine_Manager(self.verbose)        
        self.board = utils.get_board_ref()
        self.pieces = utils.get_pieces_ref()
        
        self.gui = utils.get_gui_ref() 
        self.board.set_refs(self, self.gui)
        self.gui.set_refs(self, self.board, self.engine_manager, self.tc)
        self.gui.build_gui()
        self.board.build_board()
        self.engine_manager.set_refs(self, self.gui, self.usib, self.usiw)
        self.usib.set_refs(self, self.engine_manager, self.gui, self.tc)
        self.usiw.set_refs(self, self.engine_manager, self.gui, self.tc)
        self.tc.set_refs(self, self.gui)               
        self.set_board_colours = set_board_colours.get_ref()
        self.engine_output = engine_output.get_ref()                     

        # set level        
        command = 'level 0 ' + self.time_limit                  
        engine.command(command)   
        # turn off beeps
        if not BEEP:
            engine.command('beep')

        # restore users settings to values from previous game
        self.restore_settings()

        self.usib.set_engine(self.player[BLACK], None)
        self.usiw.set_engine(self.player[WHITE], None)        
        self.gui.update_toolbar(self.player)
               
        self.move_list = move_list.get_ref()

        self.tc.reset_clock()
        
        self.gui.enable_go_button()            
        self.gui.disable_stop_button()

        self.stm = self.get_side_to_move()
        
        self.timer_active = False       
예제 #18
0
파일: psn.py 프로젝트: nestukh/gshogi
    def load_game_psn_from_str(self, gamestr):

        cnt = 0
        comment_active = False
        movelist = []
        redolist = []        
        startpos = 'startpos'
        engine.command('new')
        stm = BLACK        
        
        movecnt = 0

        self.gamestr = gamestr
        self.game_len = len(self.gamestr)

        self.comments.clear_comments()

        # First header should be near the start of the file
        # Note the first chars in the file may be a BOM entry and
        # we need to skip these

        ptr = 0                 
     
        # Find '[' of first header
        while 1:
            if ptr >= self.game_len or ptr > 2000:                
                self.gui.info_box("Error (1) loading file. No headers found")                
                self.game.new_game('NewGame')
                self.gui.set_status_bar_msg("Error loading game")
                return 1
            if gamestr[ptr] == '[':
                break
            ptr += 1

        # First Header found (ptr pointing at '[')
        hdr, newptr = self.get_header(ptr)
        if hdr is None:
            self.gui.info_box("Error (2) loading file. No headers found")
            self.game.new_game('NewGame')                    
            self.gui.set_status_bar_msg("Error loading game")
            return 1        

        # Read remaining headers
        while hdr is not None:
            ptr = newptr                    
            try:
                prop, value = hdr.split(None, 1)
            except ValueError:
                # Error - cannot split header into a property/value pair
                self.gui.info_box("Error loading file. Invalid header:" + hdr)
                self.game.new_game('NewGame')
                self.gui.set_status_bar_msg("Error loading game")
                return 1             
            if prop == 'SFEN':                      
                # set board position, side to move
                startpos, stm = self.process_sfen(value)
            elif prop == "Handicap":

                handicap = value.strip('"')
                handicap = handicap.lower()
 
                sfen = ''
                if handicap == 'lance':           
                    sfen = 'lnsgkgsn1/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'                  
                elif handicap == 'bishop':            
                    sfen = 'lnsgkgsnl/1r7/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'                  
                elif handicap == 'rook':           
                    sfen = 'lnsgkgsnl/7b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'                   
                elif handicap == 'rook+lance':            
                    sfen = 'lnsgkgsn1/7b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'                  
                elif handicap == 'rook+bishop':            
                    sfen = 'lnsgkgsnl/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'                                   
                elif handicap == 'four piece':            
                    sfen = '1nsgkgsn1/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'                  
                elif handicap == 'six piece':            
                    sfen = '2sgkgs2/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'                 
                elif handicap == 'eight piece':            
                    sfen = '3gkg3/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'                 
                elif handicap == 'ten piece':           
                    sfen = '4k4/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1'               

                if sfen != '':
                    startpos, stm = self.process_sfen(sfen)             


            hdr, newptr = self.get_header(ptr) 
               
        # Moves       
        while 1:

            if self.verbose:
                print
                print "Processing move number (ply):" + str(movecnt + 1)

            newptr = self.skip_whitespace(ptr)
            if newptr is None:
                break
            ptr = newptr

            # if we get a header here then it must be a multi-game file
            # We cannot process multi-game so treat it as eof and exit          
            if gamestr[ptr] == '[':
                break

            # Check for stuff to ignore
            # ignore it and continue processing
            ignore_string, newptr = self.get_ignore_string(ptr)
            if ignore_string is not None:
                #  Can get '1....' to indicate white to move first 
                #if ignore_string == '1....' and movecnt == 0:                    
                #    # Use sfen for initial position but set stm to white
                #    startpos, stm = self.process_sfen("lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1")                  
                ptr = newptr
                continue

            # comment
            if self.gamestr[ptr] == '{':
                comment = ''
                ptr += 1
                while 1:
                    if ptr >= self.game_len:
                        self.gui.info_box("Error unterminated comment")
                        self.game.new_game('NewGame')
                        self.gui.set_status_bar_msg("Error loading game")                
                        return 1  # end of file before end of comment
                    if self.gamestr[ptr] == '}':
                        ptr += 1
                        break
                    comment += self.gamestr[ptr]
                    ptr += 1

                # add comment
                self.comments.set_comment(movecnt, comment)                         
                continue

            # move number
            # we do not use it
            moveno, newptr = self.get_moveno(ptr)
            if moveno is not None:                
                ptr = newptr
                continue

            # move
            move, newptr = self.get_move(ptr, movelist, stm)
            if move is not None:                
                ptr = newptr

                engine.setplayer(stm)
                stm = stm ^ 1
                if self.verbose: print "move=",move
                validmove = engine.hmove(move)                             
                if (not validmove):
                    # Should never get this message since get_move has already
                    # validated the move aginst the legal move list
                    # Can get it for sennichite (repetition)                    
                    self.gui.info_box("Error loading file, illegal move (possible sennichite (repetition)):" + move + " move number:" + str(movecnt + 1))
                    self.game.new_game('NewGame')
                    self.gui.set_status_bar_msg("Error loading game")
                    return 1                                  
                movecnt += 1                   
                movelist.append(move)
                if self.verbose: engine.command('bd')
 
                continue

            # Invalid - Display Error
            # If we get here we have an invalid word
            # Errors here will stop the processing and prevent the file loading
            # If the invalid word should be skipped instead then add it to the 
            # ignore processing above
            word, newptr = self.get_word(ptr)
            if word is not None:
                self.gui.info_box("Error loading file, unable to process move number " + str(movecnt + 1) + ". (illegal move or invalid format):" + word)
                # Load failed part way through so reset the game
                self.game.new_game('NewGame') 
                self.gui.set_status_bar_msg("Error loading game")                               
                return 1

            ptr +=1
        
        self.usib.set_newgame()
        self.usiw.set_newgame()
        self.gui.set_status_bar_msg("game loaded")
        self.gameover = False            

        self.game.set_movelist(movelist)
        self.game.set_redolist(redolist)
        self.game.set_startpos(startpos)

        self.board.update()
        
        # update move list in move list window
        self.move_list.update()
        stm = self.game.get_side_to_move()
        self.game.set_side_to_move(stm)
        self.gui.set_side_to_move(stm)
        self.gui.unhilite_squares()               
            
        self.tc.reset_clock()

        return 0
예제 #19
0
파일: gshogi.py 프로젝트: gbtami/gshogi
    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
예제 #20
0
파일: gshogi.py 프로젝트: gbtami/gshogi
    def new_game(self, menu_name):

        self.gameover = False
        engine.command("new")

        if menu_name == "NewGame":
            # Normal Game (No handicap)
            self.startpos = "startpos"
            self.start_stm = BLACK
        else:
            # Handicap Game
            self.start_stm = WHITE
            if menu_name == "LanceHandicap":
                sfen = "lnsgkgsn1/1r5b1/ppppppppp/9/9/9/" \
                       "PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
            elif menu_name == "BishopHandicap":
                sfen = "lnsgkgsnl/1r7/ppppppppp/9/9/9/" \
                       "PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
            elif menu_name == "RookHandicap":
                sfen = "lnsgkgsnl/7b1/ppppppppp/9/9/9/" \
                       "PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
            elif menu_name == "RookandLanceHandicap":
                sfen = "lnsgkgsn1/7b1/ppppppppp/9/9/9/" \
                       "PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
            elif menu_name == "TwoPieceHandicap":
                sfen = "lnsgkgsnl/9/ppppppppp/9/9/9/" \
                       "PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
            elif menu_name == "FourPieceHandicap":
                sfen = "1nsgkgsn1/9/ppppppppp/9/9/9/" \
                       "PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
            elif menu_name == "SixPieceHandicap":
                sfen = "2sgkgs2/9/ppppppppp/9/9/9/" \
                       "PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
            elif menu_name == "EightPieceHandicap":
                sfen = "3gkg3/9/ppppppppp/9/9/9/" \
                       "PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
            elif menu_name == "TenPieceHandicap":
                sfen = "4k4/9/ppppppppp/9/9/9/" \
                       "PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
            else:
                gv.gui.info_box("Error. Invalid menu name:" + menu_name)
                return

            engine.setfen(sfen)
            self.startpos = sfen

        gv.board.update()
        # update move list in move list window
        self.move_list.update()
        if not BEEP:
            engine.command("beep")

        gv.usib.set_newgame()
        gv.usiw.set_newgame()
        self.movelist = []
        self.redolist = []
        gv.gui.set_status_bar_msg("")
        self.stm = self.get_side_to_move()
        gv.gui.set_side_to_move(self.stm)
        gv.gui.unhilite_squares()

        gv.tc.reset_clock()
예제 #21
0
파일: gshogi.py 프로젝트: gbtami/gshogi
    def __init__(self):
        gv.gshogi = self
        # set global variables for debug messages
        gv.verbose, gv.verbose_usi = utils.get_verbose()
        # prefix to find package files/folders
        self.prefix = utils.get_prefix()
        # set up .gshogi directory in the home directory
        self.gshogipath = utils.create_settings_dir()
        self.glade_dir = os.path.join(self.prefix, "glade")
        # get settings saved from previous game
        self.settings = utils.get_settings_from_file(self.gshogipath)

        self.ask_before_promoting = False
        self.gameover = False
        self.time_limit = "00:10"
        self.stopped = True
        self.quitting = False
        self.src = ""
        self.src_x = ""
        self.src_y = ""
        self.startpos = "startpos"
        self.start_stm = BLACK

        self.search_depth = 39
        self.thinking = False
        self.cmove = "none"
        self.movelist = []
        self.redolist = []
        self.player = ["Human", "gshogi"]
        self.pondermove = [None, None]

        opening_book_path = os.path.join(self.prefix, "data/opening.bbk")
        engine.init(opening_book_path, gv.verbose)

        gv.gui = gui.Gui()

        gv.pieces = pieces.Pieces()
        # custom pieceset path
        if self.settings != "":
            gv.pieces.set_custom_pieceset_path(
                self.settings.custom_pieceset_path)
        gv.pieces.load_pieces(self.prefix)

        # usiw is the instance that plays white (gote)
        # usib is the instance that plays black (sente)
        gv.usib = usi.Usi("b")
        gv.usiw = usi.Usi("w")

        # instantiate board, gui, classes
        gv.tc = time_control.Time_Control()
        gv.engine_manager = engine_manager.Engine_Manager()
        gv.board = board.Board()

        self.set_board_colours = set_board_colours.get_ref()
        # set colours to previous game (if any)
        if self.settings != "":
            self.set_board_colours.restore_colour_settings(
                self.settings.colour_settings)
        gv.gui.build_gui()
        gv.board.build_board()
        self.engine_output = engine_output.get_ref()

        # set level
        command = "level 0 " + self.time_limit
        engine.command(command)
        # turn off beeps
        if not BEEP:
            engine.command("beep")

        # restore users settings to values from previous game
        self.restore_settings(self.settings)

        gv.usib.set_engine(self.player[BLACK], None)
        gv.usiw.set_engine(self.player[WHITE], None)
        gv.gui.update_toolbar(self.player)

        self.move_list = move_list.get_ref()

        gv.tc.reset_clock()

        gv.gui.enable_go_button()
        gv.gui.disable_stop_button()

        self.stm = self.get_side_to_move()
        self.timer_active = False
        self.set_board_colours.apply_colour_settings()