예제 #1
0
    def negamax(self, pnode, color, curtime, delta):
        if int(time.time() - curtime) > delta:
            #If the timelimit is passed, just return the heuristic value of the move
            return pnode.state.score(self.go_engine.komi)

        #Generate all of the children of the current node
        children = GoBoardUtil.generate_legal_moves(
            pnode.state, GoBoardUtil.color_to_int(color))
        #Which value is the best? I dunno?
        best = float("-inf")
        #Children list comes out as a string for some reason.
        children = children.split(" ")
        children.append('')
        #Go through the children
        movew = ''
        for child in children:
            #If the time is expired, return the score and the child

            nodecopy = node(pnode.state.copy())
            if child == '':
                child = None
                val = nodecopy.state.move(child,
                                          GoBoardUtil.color_to_int(color))

            else:
                coord = GoBoardUtil.move_to_coord(child, self.board.size)
                point = self.board._coord_to_point(coord[0], coord[1])
                val = nodecopy.state.move(point,
                                          GoBoardUtil.color_to_int(color))

            if nodecopy.state.end_of_game():
                return pnode.state.score(self.go_engine.komi)

            if color == "b":
                moved, score = self.negamax(nodecopy, "w", curtime, delta)

            else:
                moved, score = self.negamax(nodecopy, "b", curtime, delta)

            #best = max(best, score)
            if (best < -score):
                best = -score
                movew = moved
            if int(time.time() - curtime) > delta:
                # return movew, best
                return pnode.state.score(self.go_engine.komi)

        #print("returning")
        print("movew", movew)
        return best, movew
예제 #2
0
    def genmove_cmd(self, args):

        try:
            color = GoBoardUtil.color_to_int(args[0].lower())
            move = self.policy_moves_return_cmd(color)
            if move is None:
                self.respond("pass")
                return
            print(move)
            one_move = move[0]
            print(one_move)
            if not self.board.check_legal(one_move, color):
                one_move = self.board._point_to_coord(one_move)
                board_move = GoBoardUtil.format_point(one_move)
                self.respond("Illegal move: {}".format(board_move))
                raise RuntimeError("Illegal move given by engine")

            # move is legal; play it
            self.board.move(one_move, color)

            self.debug_msg("Move: {}\nBoard: \n{}\n".format(
                one_move, str(self.board.get_twoD_board())))
            one_move = self.board._point_to_coord(one_move)
            board_move = GoBoardUtil.format_point(one_move)
            self.respond(board_move)
        except Exception as e:
            print(e)
            self.respond('Error: {}'.format(str(e)))
예제 #3
0
    def play_cmd(self, args):
        """
        play a move as the given color

        Arguments
        ---------
        args[0] : {'b','w'}
            the color to play the move as
            it gets converted to  Black --> 1 White --> 2
            color : {0,1}
            board_color : {'b','w'}
        args[1] : str
            the move to play (e.g. A5)
        """
        try:
            board_color = args[0].lower()
            board_move = args[1]
            color = GoBoardUtil.color_to_int(board_color)
            move = GoBoardUtil.move_to_coord(args[1], self.board.size)
            if move:
                move = self.board._coord_to_point(move[0], move[1])
            else:
                return
            if not self.board.move(move, color):
                return
            self.respond()
        except Exception as e:
            self.respond("illegal move: {} {} {}".format(
                board_color, board_move, str(e)))
예제 #4
0
    def genmove_cmd(self, args):
        """
        generate a move for the specified color

        Arguments
        ---------
        args[0] : {'b','w'}
            the color to generate a move for
            it gets converted to  Black --> 1 White --> 2
            color : {0,1}
            board_color : {'b','w'}
        """
        try:
            board_color = args[0].lower()
            color = GoBoardUtil.color_to_int(board_color)
            move = self.go_engine.get_move(self.board, color)
            if move is None:
                self.respond("pass")
                return

            if not self.board.check_legal(move, color):
                move = self.board._point_to_coord(move)
                board_move = GoBoardUtil.format_point(move)
                self.respond("Illegal move: {}".format(board_move))
                raise RuntimeError("Illegal move given by engine")

            # move is legal; play it
            self.board.move(move, color)
            self.debug_msg("Move: {}\nBoard: \n{}\n".format(move, str(self.board.get_twoD_board())))
            move = self.board._point_to_coord(move)
            board_move = GoBoardUtil.format_point(move)
            self.respond(board_move)
        except Exception as e:
            self.respond('Error: {}'.format(str(e)))
    def genmove_cmd(self, args):
        """
        generate a move for the specified color

        Arguments
        ---------
        args[0] : {'b','w'}
            the color to generate a move for
            it gets converted to  Black --> 1 White --> 2
            color : {0,1}
            board_color : {'b','w'}
        """
        try:
            board_color = args[0].lower()
            color = GoBoardUtil.color_to_int(board_color)
            self.debug_msg("Board:\n{}\nko: {}\n".format(
                str(self.board.get_twoD_board()), self.board.ko_constraint))
            self.current_player = color
            move = self.go_engine.get_move(self.board, color)

            # policy_moves, type_of_move = GoBoardUtil.generate_all_policy_moves(self.board,
            #self.go_engine.pattern,
            #self.go_engine.selfatari)
            # result = []
            # for i in range(len(policy_moves)):
            #     result1 = []
            #     result1.append(policy_moves[i])
            #     result.append(GoBoardUtil.sorted_point_string(result1, self.board.NS))
            # print(len(result))
            # if len(result) != 0:
            #     row, col = GoBoardUtil.move_to_coord(random.choice(result), self.board.size)
            #     move = self.board._coord_to_point(row, col)

            # print(policy_moves[0])
            # row, col = GoBoardUtil.move_to_coord(policy_moves[0], self.board.size)
            # move = policy_moves[0]

            if move is None:
                self.respond("pass")
                return

            if not self.board.check_legal(move, color):
                move = self.board._point_to_coord(move)
                board_move = GoBoardUtil.format_point(move)
                self.respond("Illegal move: {}".format(board_move))
                raise RuntimeError("Illegal move given by engine")

            # move is legal; play it
            self.board.move(move, color)
            self.debug_msg("Move: {}\nBoard: \n{}\n".format(
                move, str(self.board.get_twoD_board())))
            move = self.board._point_to_coord(move)
            board_move = GoBoardUtil.format_point(move)
            self.respond(board_move)
        except Exception as e:
            self.respond('Error: {}'.format(str(e)))
    def play_cmd(self, args):
        """
        play a move as the given color

        Arguments
        ---------
        args[0] : {'b','w'}
            the color to play the move as
            it gets converted to  Black --> 1 White --> 2
            color : {0,1}
            board_color : {'b','w'}
        args[1] : str
            the move to play (e.g. A5)
        """
        try:
            board_color = args[0].lower()
            board_move = args[1]
            color = GoBoardUtil.color_to_int(board_color)
            if args[1].lower() == 'pass':
                self.debug_msg("Player {} is passing\n".format(args[0]))
                self.respond("illegal move: {} wrong coordinate".format(
                    ' '.join(args)))
                return
            move = GoBoardUtil.move_to_coord(args[1], self.board.size)
            if move:
                move = self.board._coord_to_point(move[0], move[1])
            # move == None on pass
            else:
                self.error(
                    "Error in executing the move %s, check given move: %s" %
                    (move, args[1]))
                return

            #TODO
            check, msg = self.board.move(move, color)
            if not check:
                if msg[0] == "R":
                    self.respond("illegal move: {} occupied".format(
                        ' '.join(args)))
                    return
                if msg[0] == "C":
                    self.respond("illegal move: {} capture".format(
                        ' '.join(args)))
                    return
                if msg[0] == "S":
                    self.respond("illegal move: {} suicide".format(
                        ' '.join(args)))
                    return
            else:
                self.debug_msg("Move: {}\nBoard:\n{}\n".format(
                    board_move, str(self.board.get_twoD_board())))
            self.respond()
        except Exception as e:
            self.respond("illegal move: {} wrong coordinate".format(
                ' '.join(args)))
예제 #7
0
    def genmove_cmd(self, args):
        """
        generate a move for the specified color

        Arguments
        ---------
        args[0] : {'b','w'}
            the color to generate a move for
            it gets converted to  Black --> 1 White --> 2
            color : {0,1}
            board_color : {'b','w'}
        """
        try:
            board_color = args[0].lower()
            color = GoBoardUtil.color_to_int(board_color)
            self.debug_msg("Board:\n{}\nko: {}\n".format(str(self.board.get_twoD_board()),
                                                          self.board.ko_constraint))

            # Assignment4 - policy probabilistic player
            # =========================================
            if color != self.board.current_player:
                self.respond("Opponent's turn")
                return

            move_prob_tuples = self.get_move_prob()
            if move_prob_tuples[0][0] == 'pass':
                self.respond("pass")
                self.go_engine.update('pass')
                self.board.move(None, color)
                return

            # based on
            # https://docs.python.org/3.5/library/random.html
            population = [val for val, cnt in move_prob_tuples for _ in range(int(cnt*1e5))]
            move = random.choice(population)
            move = GoBoardUtil.move_to_coord(move, self.board.size)
            move = self.board._coord_to_point(move[0], move[1])
            # =========================================

            if not self.board.check_legal(move, color):
                move = self.board._point_to_coord(move)
                board_move = GoBoardUtil.format_point(move)
                self.respond("Illegal move: {}".format(board_move))
                traceback.print_exc(file=sys.stdout)
                self.respond('Error: {}'.format(str(e)))
                raise RuntimeError("Illegal move given by engine")

            # move is legal; play it
            self.board.move(move, color)
            self.debug_msg("Move: {}\nBoard: \n{}\n".format(move, str(self.board.get_twoD_board())))
            move = self.board._point_to_coord(move)
            board_move = GoBoardUtil.format_point(move)
            self.respond(board_move)
        except Exception as e:
            self.respond('Error: {}'.format(str(e)))
예제 #8
0
 def safety_cmd(self, args):
     try:
         color = GoBoardUtil.color_to_int(args[0].lower())
         safety_list = self.board.find_safety(color)
         safety_points = []
         for point in safety_list:
             x, y = self.board._point_to_coord(point)
             safety_points.append(GoBoardUtil.format_point((x, y)))
         self.respond(safety_points)
     except Exception as e:
         self.respond('Error: {}'.format(str(e)))
예제 #9
0
def negamax(node, color, time, delta):
    if int(time.time() - time) > delta:
        return node.score
    children = GoBoardUtil.generate_legal_moves(
        node.state, GoBoardUtil.color_to_int(color))
    for child in children:
        nodecopy = copy.deepcopy(node)
        nodecopy.move(color + child)
        if color == "b":
            v = negamax(nodecopy, "w", time, delta)
        else:
            v = negamax(nodecopy, "b", time, delta)
예제 #10
0
    def genmove_cmd(self, args):
        """
        generate a move for the specified color

        Arguments
        ---------
        args[0] : {'b','w'}
            the color to generate a move for
            it gets converted to  Black --> 1 White --> 2
            color : {0,1}
            board_color : {'b','w'}
        """
        try:
            board_color = args[0].lower()
            color = GoBoardUtil.color_to_int(board_color)
            self.debug_msg("Board:\n{}\nko: {}\n".format(
                str(self.board.get_twoD_board()), self.board.ko_constraint))
            #move = self.go_engine.get_move(self.board, color)
            policy_moves, type_of_move = GoBoardUtil.generate_all_policy_moves(
                self.board, self.go_engine.pattern, self.go_engine.selfatari,
                color)
            if len(policy_moves) == 0:
                self.respond("Pass")
            else:
                #print(policy_moves)
                move = policy_moves[randint(0, len(policy_moves) - 1)]
                #print(move)
                #move = GoBoardUtil.point_to_coord(move)
                #move = GoBoardUtil.sorted_point_string(policy_moves[randint(0,len(policy_moves)-1)], self.board.NS)
                #print(move)
                #print(move)
                #response = type_of_move + " " + GoBoardUtil.sorted_point_string(policy_moves, self.board.NS)
                #self.respond(response)

            if move is None:
                self.respond("pass")
                return

            if not self.board.check_legal(move, color):
                move = self.board._point_to_coord(move)
                board_move = GoBoardUtil.format_point(move)
                self.respond("Illegal move: {}".format(board_move))
                raise RuntimeError("Illegal move given by engine")

            # move is legal; play it
            self.board.move(move, color)
            self.debug_msg("Move: {}\nBoard: \n{}\n".format(
                move, str(self.board.get_twoD_board())))
            move = self.board._point_to_coord(move)
            board_move = GoBoardUtil.format_point(move)
            self.respond(board_move)
        except Exception as e:
            self.respond('Error: {}'.format(str(e)))
예제 #11
0
    def genmove_cmd(self, args):
        """
        generate a move for the specified color

        Arguments
        ---------
        args[0] : {'b','w'}
            the color to generate a move for
            it gets converted to  Black --> 1 White --> 2
            color : {0,1}
            board_color : {'b','w'}
        """
        try:
            board_color = args[0].lower()
            color = GoBoardUtil.color_to_int(board_color)

            # First try to solve the state and generate a best move
            to_play_bkp = self.board.to_play
            self.board.to_play = color
            score = self.board.call_search()
            if score == 1:
                # The player can win, make a move
                _, move = self.board.tree_cache[self.board]
                coord = GoBoardUtil.move_to_coord(move, self.board.size)
                point = self.board._coord_to_point(coord[0], coord[1])
                self.board.move(point, color)
                self.respond(GoBoardUtil.format_point(coord))
                return
            # Opponent can win or timeout, use the existing code to generate
            # a random move
            self.board.to_play = to_play_bkp

            move = self.go_engine.get_move(self.board, color)
            if move is None:
                # self.respond("pass")
                self.respond("resign")
                return

            if not self.board.check_legal(move, color):
                move = self.board._point_to_coord(move)
                board_move = GoBoardUtil.format_point(move)
                self.respond("Illegal move: {}".format(board_move))
                raise RuntimeError("Illegal move given by engine")

            # move is legal; play it
            self.board.move(move, color)
            self.debug_msg("Move: {}\nBoard: \n{}\n".format(
                move, str(self.board.get_twoD_board())))
            move = self.board._point_to_coord(move)
            board_move = GoBoardUtil.format_point(move)
            self.respond(board_move)
        except Exception as e:
            self.respond('Error: {}'.format(str(e)))
예제 #12
0
    def genmove_cmd(self, args):
        """
        generate a move for the specified color

        Arguments
        ---------
        args[0] : {'b','w'}
            the color to generate a move for
            it gets converted to  Black --> 1 White --> 2
            color : {0,1}
            board_color : {'b','w'}
        """
        try:
            board_color = args[0].lower()
            color = GoBoardUtil.color_to_int(board_color)
            self.debug_msg("Board:\n{}\nko: {}\n".format(
                str(self.board.get_twoD_board()), self.board.ko_constraint))

            # Assignment4 - policy max player
            # =========================================
            if color != self.board.current_player:
                self.respond("Opponent's turn")
                return

            move = self.get_move_prob()[0][0]
            if move == 'pass':
                self.respond("pass")
                self.go_engine.update('pass')
                self.board.move(None, color)
                return
            move = GoBoardUtil.move_to_coord(move, self.board.size)
            move = self.board._coord_to_point(move[0], move[1])
            # =========================================

            if not self.board.check_legal(move, color):
                move = self.board._point_to_coord(move)
                board_move = GoBoardUtil.format_point(move)
                self.respond("Illegal move: {}".format(board_move))
                traceback.print_exc(file=sys.stdout)
                self.respond('Error: {}'.format(str(e)))
                raise RuntimeError("Illegal move given by engine")

            # move is legal; play it
            self.board.move(move, color)
            self.debug_msg("Move: {}\nBoard: \n{}\n".format(
                move, str(self.board.get_twoD_board())))
            move = self.board._point_to_coord(move)
            board_move = GoBoardUtil.format_point(move)
            self.respond(board_move)
        except Exception as e:
            self.respond('Error: {}'.format(str(e)))
예제 #13
0
    def play_cmd(self, args):
        """
        play a move as the given color

        Arguments
        ---------
        args[0] : {'b','w'}
            the color to play the move as
            it gets converted to  Black --> 1 White --> 2
            color : {0,1}
            board_color : {'b','w'}
        args[1] : str
            the move to play (e.g. A5)
        """
        try:
            board_color = args[0].lower()
            board_move = args[1]
            color = GoBoardUtil.color_to_int(board_color)
            if args[1].lower() == 'pass':
                #GIVES RESPONSE, UNSURE IF ENFORCED AS IN TURN CHANGE=??
                self.debug_msg("Player {} is passing\n".format(args[0]))
                self.respond("Illegal Move: {} is passing".format(args[0]))
                return
            move = GoBoardUtil.move_to_coord(args[1], self.board.size)
            if move:
                move = self.board._coord_to_point(move[0], move[1])
            # move == None on pass
            else:
                self.error(
                    "Error in executing the move %s, check given move: %s" %
                    (move, args[1]))
                return
            if not self.board.move(move, color):
                self.respond("Illegal Move: {}".format(board_move))
                return
            else:
                self.debug_msg("Move: {}\nBoard:\n{}\n".format(
                    board_move, str(self.board.get_twoD_board())))
            self.respond()
            #CHECKS VALID MOVES OF OPPONENT, IF EMPTY WINNING
            if GoBoardUtil.generate_legal_moves(self.board, color) == '':
                if args[0].lower() == "b":
                    self.respond("Black Wins!!")
                if args[0].lower() == "w":
                    self.respond("White Wins!!")
                self.respond("Board has been cleared.")
                self.reset(self.board.size)
        except Exception as e:
            self.respond('Error: {}'.format(str(e)))
예제 #14
0
    def play_cmd(self, args):
        """
        play a move as the given color

        Arguments
        ---------
        args[0] : {'b','w'}
            the color to play the move as
            it gets converted to  Black --> 1 White --> 2
            color : {0,1}
            board_color : {'b','w'}
        args[1] : str
            the move to play (e.g. A5)
        """
        #  print("play_cmd")
        try:
            #  print("play_cmd try") # remove
            board_color = args[0].lower()
            board_move = args[1]
            color = GoBoardUtil.color_to_int(board_color)
            if args[1].lower() == 'pass':
                #print("play_cmd if #1")

                self.debug_msg("Player {} is passing\n".format(args[0]))
                self.respond()
                return
            move = GoBoardUtil.move_to_coord(args[1], self.board.size)
            if move:  # if move is a1, move is (1, 1). the numeric version of args[1]
                #   print("play_cmd if #2") # remove
                #    print("move: ", move) # remove
                #          print("args[1]: ", args[1]) # remove
                move = self.board._coord_to_point(move[0], move[1])
            # move == None on pass
            else:
                self.error(
                    "Error in executing the move %s, check given move: %s" %
                    (move, args[1]))
                return
            if not self.board.move(move, color):
                return
            else:
                self.debug_msg("Move: {}\nBoard:\n{}\n".format(
                    board_move, str(self.board.get_twoD_board())))
            self.respond()
        except Exception as e:  # illegal chars handled here
            self.respond('Error: {}'.format(str(e)))
예제 #15
0
    def genmove(self, args):
        #original code from util
        """
        generate a move for the specified color

        Arguments
        ---------
        args[0] : {'b','w'}
            the color to generate a move for
            it gets converted to  Black --> 1 White --> 2
            color : {0,1}
            board_color : {'b','w'}
        """
        try:
            board_color = args[0].lower()
            color = GoBoardUtil.color_to_int(board_color)
            self.debug_msg("Board:\n{}\nko: {}\n".format(
                str(self.board.get_twoD_board()), self.board.ko_constraint))

            solver_color, solver_move = self.solve_genmove("1")
            move = self.go_engine.get_move(self.board, color)
            if move is None:
                self.respond("pass")
                return
            if (solver_color == self.color_convert(color)):
                moves = list(
                    GoBoardUtil.move_to_coord(solver_move, self.board.size))
                solver_move = self.board._coord_to_point(moves[0], moves[1])
                move = solver_move

            elif not self.board.check_legal(move, color):
                move = self.board._point_to_coord(move)
                board_move = GoBoardUtil.format_point(move)
                self.respond("Illegal move: {}".format(board_move))
                raise RuntimeError("Illegal move given by engine")

            # move is legal; play it
            self.board.move(move, color)

            self.debug_msg("Move: {}\nBoard: \n{}\n".format(
                move, str(self.board.get_twoD_board())))
            move = self.board._point_to_coord(move)
            board_move = GoBoardUtil.format_point(move)
            self.respond(board_move)
        except Exception as e:
            self.respond('Error: {}'.format(str(e)))
 def genmove_cmd(self, args):
     board_color = args[0].lower()
     color = GoBoardUtil.color_to_int(board_color)
     # Try to use the solver to solve the game perfectly
     winner, point = self.go_engine.solve(self.board, color)
     if winner == 'unknown' or winner != board_color:
         # If the solver cannot solve the game in 'timelimit' seconds, or toPlay is losing
         # then play the same move as the original Go2
         super().genmove_cmd(args)
     else:
         if point == None or point == 'pass':
             self.respond("pass")
             return
         self.board.move(point, color)
         self.debug_msg("Move: {}\nBoard: \n{}\n".format(point, str(self.board.get_twoD_board())))
         board_move = self._point_to_move(point)
         self.respond(board_move)
예제 #17
0
    def play(self, args):
        """
        play a move as the given color

        Arguments
        ---------
        args[0] : {'b','w'}
            the color to play the move as
            it gets converted to  Black --> 1 White --> 2
            color : {0,1}
            board_color : {'b','w'}
        args[1] : str
            the move to play (e.g. A5)
        """
        # self.respond(args)
        try:
            board_color = args[0].lower()
            board_move = args[1]
            color = GoBoardUtil.color_to_int(board_color)
            if args[1].lower() == 'pass':
                self.debug_msg("Player {} is passing\n".format(args[0]))
                self.board.move(None, color)
                self.board.current_player = GoBoardUtil.opponent(color)

                return
            move = GoBoardUtil.move_to_coord(args[1], self.board.size)
            if move:
                move = self.board._coord_to_point(move[0], move[1])
            # move == None on pass
            else:
                self.error(
                    "Error in executing the move %s, check given move: %s" %
                    (move, args[1]))
                return
            if not self.board.move(move, color):
                pass
                # self.respond("Illegal Move: {}".format(board_move))
                return
            else:
                self.debug_msg("Move: {}\nBoard:\n{}\n".format(
                    board_move, str(self.board.get_twoD_board())))

        except Exception as e:
            # self.respond('Error: {}'.format(str(e)))
            pass
예제 #18
0
 def legal_moves_cmd(self, args):
     """
     list legal moves for the given color
     Arguments
     ---------
     args[0] : {'b','w'}
         the color to play the move as
         it gets converted to  Black --> 1 White --> 2
         color : {0,1}
         board_color : {'b','w'}
     """
     try:
         board_color = args[0].lower()
         color = GoBoardUtil.color_to_int(board_color)
         moves = GoBoardUtil.generate_legal_moves(self.board, color)
         self.respond(moves)
     except Exception as e:
         self.respond('Error: {}'.format(str(e)))
예제 #19
0
    def play_cmd(self, args):
        """
        play a move as the given color

        Arguments
        ---------
        args[0] : {'b','w'}
            the color to play the move as
            it gets converted to  Black --> 1 White --> 2
            color : {0,1}
            board_color : {'b','w'}
        args[1] : str
            the move to play (e.g. A5)
        """
        try:
            board_color = args[0].lower()
            board_move = args[1]
            color = GoBoardUtil.color_to_int(board_color)
            if args[1].lower() == 'pass':
                ##                self.debug_msg("Player {} is passing\n".format(args[0]))		 +                self.debug_msg("Passing not allowed\n".format(args[0]))
                ##                self.respond()
                ##                return
                raise ValueError("wrong coordinate")
            move = GoBoardUtil.move_to_coord(args[1], self.board.size)
            if move:
                move = self.board._coord_to_point(move[0], move[1])
            # move == None on pass
            else:
                self.error(
                    "Error in executing the move %s, check given move: %s" %
                    (move, args[1]))
                return
            if not self.board.move(move, color):
                msg = self.board.getMsg(move, color)
                self.respond("illegal move: {} {} {}".format(
                    board_color, board_move, msg))
                return
            else:
                self.debug_msg("Move: {}\nBoard:\n{}\n".format(
                    board_move, str(self.board.get_twoD_board())))
            self.respond()
        except Exception as e:
            self.respond('illegal move: {} {} {}'.format(
                board_color, board_move, str(e)))
예제 #20
0
    def policy_moves_cmd(self, args):
        """
        Return list of policy moves for the current_player of the board
        """
        #ATARI CAPTURE
        if self.board.last_move != None:
            moves = self.last_moves_empty()
            diagonal = self.board._diag_neighbors(self.board.last_move)
            capture_moves = list(set(moves) - set(diagonal))
            capture_moves = GoBoardUtil.filter_moves(
                self.board, capture_moves, self.go_engine.check_selfatari)

            if (len(capture_moves) == 1):
                if (self.board._liberty(
                        capture_moves[0],
                        self.board._points_color(capture_moves[0])) == 1):
                    policy_moves, type_of_move = capture_moves, 'AtariCapture'
                    response = type_of_move + " " + GoBoardUtil.sorted_point_string(
                        policy_moves, self.board.NS)
                    self.respond(response)
                    # self.respond("akjhfjkasdhfkjahsd")
                    return

            # # ATARI DEFENCE
            # else:
            #     defence_moves=[]
            #     moves = self.board._neighbors(self.board.last_move)
            #     # moves.extend(board._diag_neighbors2(board.last_move))

            #     for move in moves:
            #         if(self.board._single_liberty(move,self.board.current_player)!=None):
            #             # print(self.board._single_liberty(move,self.board.current_player))
            #             # print(self.board._point_to_coord(move))
            #             defence_moves.append(self.board._single_liberty(move,self.board.current_player))

            #     if(defence_moves != []):
            #         defence_moves  = GoBoardUtil.filter_moves(self.board, defence_moves, self.go_engine.check_selfatari)
            #         policy_moves, type_of_move =  defence_moves, 'AtariDefense'
            #         response = type_of_move + " " + GoBoardUtil.sorted_point_string(policy_moves, self.board.NS)
            #         self.respond(response)
            #         return

        defence_moves = []
        lm = self.board.last_move
        if lm != None:
            current_play = self.board.current_player
            opponent = GoBoardUtil.opponent(self.board.current_player)
            for elem in self.board._neighbors(lm):
                val = GoBoardUtil.color_to_int(self.board._points_color(elem))
                if current_play == val:
                    # print(self.board._neighbors(elem))
                    # if self.board._neighbors(val) != None:
                    if (self.board._single_liberty(
                            elem, GoBoardUtil.int_to_color(val)) != None):
                        # val3 = GoBoardUtil.color_to_int(self.board._points_color(elem))
                        if self.board._liberty(
                                self.board._single_liberty(
                                    elem, GoBoardUtil.int_to_color(val)),
                                current_play) > 1:
                            defence_moves.append(
                                self.board._single_liberty(
                                    elem, GoBoardUtil.int_to_color(val)))

            ng = self.board._neighbors(lm)
            dg = self.board._diag_neighbors(self.board.last_move)
            all_ng = ng + dg
            # print(all_ng)
            count = 0
            for elem in all_ng:
                val = GoBoardUtil.color_to_int(self.board._points_color(elem))
                if opponent == val:
                    # print(self.board._single_liberty(elem, GoBoardUtil.int_to_color(val)))
                    if (self.board._single_liberty(
                            elem, GoBoardUtil.int_to_color(val)) != None):
                        # print(elem)
                        for i in self.board._neighbors(elem):
                            # print(i)
                            val2 = GoBoardUtil.color_to_int(
                                self.board._points_color(i))
                            if (val2 == opponent):
                                # print(self.board._liberty(i, GoBoardUtil.int_to_color(val2)))
                                if (self.board._liberty(
                                        i, GoBoardUtil.int_to_color(val2)) !=
                                        None):
                                    count += 1
                        # print(count)
                        if (count == 0):
                            defence_moves.append(
                                self.board._single_liberty(
                                    elem, GoBoardUtil.int_to_color(val)))
                        count = 0

        # print(self.board.co defence_moves)
        # for v in defence_moves:
        #     print(v)
        defence_moves = GoBoardUtil.filter_moves(
            self.board, defence_moves, self.go_engine.check_selfatari)
        defence_moves = GoBoardUtil.sorted_point_string(
            defence_moves, self.board.NS)
        if len(defence_moves) > 0:
            # defence_moves = GoBoardUtil.filter_moves(self.board,defence_moves, self.go_engine.check_selfatari)
            self.respond("AtariDefense " + defence_moves)
            return

        policy_moves, type_of_move = GoBoardUtil.generate_all_policy_moves(
            self.board, self.go_engine.use_pattern,
            self.go_engine.check_selfatari)
        if len(policy_moves) == 0:
            self.respond("Pass")
        else:
            response = type_of_move + " " + GoBoardUtil.sorted_point_string(
                policy_moves, self.board.NS)
            self.respond(response)
            return
예제 #21
0
    def genmove_cmd(self, args):
        """
        generate a move for the specified color

        Arguments
        ---------
        args[0] : {'b','w'}
            the color to generate a move for
            it gets converted to  Black --> 1 White --> 2
            color : {0,1}
            board_color : {'b','w'}
        """
        #try:
        board_color = args[0].lower()
        color = GoBoardUtil.color_to_int(board_color)
        self.debug_msg("Board:\n{}\nko: {}\n".format(str(self.board.get_twoD_board()),
                                                      self.board.ko_constraint))
        move = self.go_engine.get_move(self.board, color)
        #move = GoBoardUtil.generate_move_with_filter(self.board, True, True)
        if move is None:
            self.respond("pass")
            return

        if not self.board.check_legal(move, color):
            move = self.board._point_to_coord(move)
            board_move = GoBoardUtil.format_point(move)
            self.respond("Illegal move: {}".format(board_move))
            raise RuntimeError("Illegal move given by engine")

        # move is legal; play it
예제 #22
0
    def genmove_cmd(self, args):
        """
        generate a move for the specified color

        Arguments
        ---------
        args[0] : {'b','w'}
            the color to generate a move for
            it gets converted to  Black --> 1 White --> 2
            color : {0,1}
            board_color : {'b','w'}
        """
        try:
            self.notdone = False
            board_color = args[0].lower()
            color = GoBoardUtil.color_to_int(board_color)
            #TODO
            if self.board.get_winner() != None:
                self.respond("resign")
                return
            self.winMoves = []
            self.genCall = True
            if args[0] != None:
                if args[0] == 'b':
                    self.board.to_play = BLACK
                else:
                    self.board.to_play = WHITE
            self.solve_cmd(args)

            self.genCall = False

            if len(self.winMoves) != 0:
                row, col = GoBoardUtil.move_to_coord(self.winMoves[0],
                                                     self.board.size)
                move = self.board._coord_to_point(row, col)
                #self.respond(self.winMoves[0])

            else:
                move = GoBoardUtil.generate_random_move(self.board, color)
                move = self.board._point_to_coord(move)
                move = GoBoardUtil.format_point(move)
                row, col = GoBoardUtil.move_to_coord(move, self.board.size)
                move = self.board._coord_to_point(row, col)
                #self.respond(move)

            if not self.board.check_legal(move, color):
                move = self.board._point_to_coord(move)
                board_move = GoBoardUtil.format_point(move)
                self.respond("Illegal move: {}".format(board_move))
                raise RuntimeError("Illegal move given by engine")

            # move is legal; play it

            self.board.move(move, color)
            self.debug_msg("Move: {}\nBoard: \n{}\n".format(
                move, str(self.board.get_twoD_board())))
            move = self.board._point_to_coord(move)
            board_move = GoBoardUtil.format_point(move)
            self.respond(board_move)
        except Exception as e:
            self.respond('Error: {}'.format(str(e)))