示例#1
0
def select_move_prioritizing_capture(player_stone_color, boards, depth):
    '''
    moves =
    [
        [point1, point2, ...] (from oldest to newest)
        ...
    ]
    [[(1,1)], []]
    '''
    most_recent_board = boards[0]
    opponent_stone_color = utils.get_opponent_stone_color(player_stone_color)
    moves = []
    if depth == 1:
        chains = most_recent_board.get_all_chains()
        opponent_chains = [
            chain for chain in chains
            if chain.stone_color == opponent_stone_color
        ]
        for chain in opponent_chains:
            if len(chain.liberties) == 1:
                move = (chain.liberties[0], boards)
                if rc.is_move_legal(player_stone_color, move):
                    moves.append(move)
    if depth > 1:
        # Idea: define new_move as [point()]
        # all_moves = generate_all_moves_that_d
        # return select_simple_move(boards)
        raise NotImplementedError()
    if moves:
        return sorted(moves, key=lambda move: move[0])[0]
    else:
        return select_simple_move(player_stone_color, boards)
示例#2
0
文件: referee.py 项目: wungjaelee/Go
    def play_point(self, point_or_pass):
        # both players need to be registered before playing point
        if not (self.is_black_registered and self.is_white_registered):
            return
        if point_or_pass == PASS:
            if self.prev_move == PASS:
                # proper end to the game
                self.is_game_ended = True
                return self.determine_winner(PROPER_END)
            else:
                self.update_board_history(self.board_history[0])
                self.update_turn()
                self.update_prev_move(point_or_pass)
                #return self.board_history[1]
                return self.board_history

        point = point_or_pass
        if rc.is_move_legal(self.current_turn, (point, self.board_history)):
            curr_board = self.board_history[0]
            new_board = rc._get_board_if_valid_play(curr_board,
                                                    self.current_turn, point)
            self.update_board_history(new_board)
            self.update_turn()
            self.update_prev_move(point_or_pass)
            #return curr_board
            return self.board_history
        else:
            #ILLEGAL_END
            self.is_game_ended = True
            return self.determine_winner(ILLEGAL_END)
示例#3
0
 def act(self, container):
     try:
         response = container.white_player.make_a_move(container.boards)
         if response == PASS and rc.is_move_legal(WHITE, PASS):
             if container.previous_move_was_pass:
                 container.next_action = LegalEnd()
             else:
                 container.previous_move_was_pass = True
                 container.boards = [
                     container.boards[0], *container.boards[0:2]
                 ]
                 container.next_action = MakeAMoveBlack()
         elif isinstance(response, Point) and rc.is_move_legal(
                 WHITE, (response, container.boards)):
             container.previous_move_was_pass = False
             new_board = rc.get_board_if_valid_play(container.boards[0],
                                                    WHITE, response)
             container.boards = [new_board, *container.boards[0:2]]
             container.next_action = MakeAMoveBlack()
         else:
             container.next_action = WhiteIllegalMove()
     except RuntimeError:
         container.next_action = WhiteIllegalMove()
示例#4
0
def main():
    # Parse the text input into a list of JSON elements.
    txt = sys.stdin.read().rstrip()
    json_elements = txt2json(txt)

    # Handle each input and collect the results.
    results = []
    for json_element in json_elements:
        if len(json_element) == constants.BOARD_ROW_LENGTH:
            b = Board(json_element)
            results.append(rc.get_scores(b))
        else:
            stone, move = json_element
            results.append(rc.is_move_legal(stone, move))
    json.dump(results, sys.stdout)
示例#5
0
def select_simple_move(player_stone_color, boards):
    '''
    input: boards
    output:
        if there is a move that self can make,
            returns a point with the lowest column index,
            and then with the lowest row index
        if there isn't,
            returns a string "pass"
    '''
    for point, maybe_stone in iter(boards[0]):
        if maybe_stone == EMPTY:
            move = (point, boards)
            if rc.is_move_legal(player_stone_color, move):
                return move
    return PASS