예제 #1
0
    def beta_play(self, board_history):
        # work in progress
        current_board = board_history[0]
        opponent_point = MaybeStone.flip_stone(self.stone)
        all_opponent_connections = []
        for point in Board.get_points(current_board, opponent_point):
            all_opponent_connections.append(get_blob(current_board, point))

        all_opponent_liberties = []
        for connection in all_opponent_connections:
            all_opponent_liberties.append(
                get_blob_liberties(current_board, connection))

        connections_with_one_liberty = set()
        for liberties_set in all_opponent_liberties:
            if len(liberties_set) == 1:
                connections_with_one_liberty.union(liberties_set)

        for row, maybe_stones in enumerate(current_board):
            for column, maybe_stone in enumerate(current_board):
                if (column, row) in connections_with_one_liberty:
                    try:
                        RuleChecker.check_move(self.stone, (column, row),
                                               board_history)
                        return Point.point_to_string((column, row))
                    except IllegalMoveError:
                        pass
                    except ImproperHistoryError as e:
                        # raise PlayerCommandError('Invalid history') from e
                        return 'This history makes no sense!'

        return 'pass'
예제 #2
0
파일: board.py 프로젝트: zbash75/Go
 def _blob_walk(board, point, blob_stone, walked_points):
     walked_points.add(point)
     for neighbor_point in Point.neighbor_points(point):
         if neighbor_point not in walked_points and Board.occupies(
                 board, blob_stone, neighbor_point):
             RealBoard._blob_walk(board, neighbor_point, blob_stone,
                                  walked_points)
예제 #3
0
def main():
    # setup gui
    image = Image.open(IMAGE_FILE)
    image_width, image_height = image.size
    window_size = RectSize(width=image_width, height=image_height)
    window = initialize_window(window_size)
    canvas = create_canvas_on(window, window_size, canvas_place=Point(0, 0))
    image_tk = ImageTk.PhotoImage(image)
    canvas.create_image(0, 0, image=image_tk, anchor="nw")

    def draw_rect(rectangle):
        canvas.delete("rect")
        draw_rect_on(
            canvas,
            rectangle,
            fill_color="orange",
            stipple="gray12",
            outline_color="orange",
            width=1.0,
            tag="rect",
        )

    # state
    mouse_left_click = createMouseEventStream(window, "<Button-1>")
    mouse_left_drag = createMouseEventStream(window, "<B1-Motion>")
    rect_begin = mouse_left_click
    rect_end = rx.merge(mouse_left_drag, mouse_left_click)
    rect = rx.combine_latest(rect_begin, rect_end).pipe(
        ops.map(lambda tpl: rect_from_2points(*tpl)))

    rect.subscribe(draw_rect)

    window.mainloop()
예제 #4
0
파일: referee.py 프로젝트: zbash75/Go
    def execute_turn(self, action):
        # Check for double pass
        if action == "pass":
            if not self.last_turn_was_pass:
                self.last_turn_was_pass = True
                self._update_history(self.board_hist[0])
                return deepcopy(self.board_hist)

            elif self.last_turn_was_pass:
                return self.end_game()

        elif action == "GO has gone crazy!" or action == "This history makes no sense!":
            if self.current_turn == "B":
                return self.end_game(self.black_player)
            else:
                return self.end_game(self.white_player)

        else:
            self.last_turn_was_pass = False
            action = Point.string_to_point(action)
            try:
                new_board = RuleChecker.check_move(self.current_turn, action,
                                                   self.board_hist)

                self._update_history(new_board)

                return deepcopy(self.board_hist)

            except RuleCheckerError as e:
                if self.current_turn == "B":
                    return self.end_game(self.black_player)
                else:
                    return self.end_game(self.white_player)
예제 #5
0
파일: board.py 프로젝트: zbash75/Go
 def get_blob_liberties(board, blob):
     blob_liberties = set()
     for point in blob:
         neighbor_points = Point.neighbor_points(point)
         for neighbor_point in neighbor_points:
             if not Board.occupied(board, neighbor_point):
                 blob_liberties.add(neighbor_point)
     return blob_liberties
예제 #6
0
def main():
    # setup gui
    image = Image.open(IMAGE_FILE)
    image_width, image_height = image.size
    window_size = RectSize(width=image_width, height=image_height)
    window = initialize_window(window_size)
    canvas = create_canvas_on(window, window_size, canvas_place=Point(0, 0))
    image_tk = ImageTk.PhotoImage(image)
    canvas.create_image(0, 0, image=image_tk, anchor="nw")

    def draw_rect(rectangle):
        canvas.delete("rect")
        draw_rect_on(
            canvas,
            rectangle,
            fill_color="orange",
            stipple="gray12",
            outline_color="orange",
            width=1.0,
            tag="rect",
        )

    # state
    rect_begin = Point(0, 0)
    rect_end = Point(0, 0)

    def on_mouse_left_down(x, y):
        nonlocal rect_begin, rect_end
        rect_begin = Point(x, y)
        rect_end = Point(x, y)
        draw_rect(rect_from_2points(rect_begin, rect_end))

    def on_mouse_move(x, y):
        nonlocal rect_begin, rect_end
        rect_end = Point(x, y)
        draw_rect(rect_from_2points(rect_begin, rect_end))

    window.bind("<Button-1>", lambda ev: on_mouse_left_down(ev.x, ev.y))
    window.bind("<B1-Motion>", lambda ev: on_mouse_move(ev.x, ev.y))

    window.mainloop()
예제 #7
0
    def play(self, strategy, board_history):
        if self.strategy == "random":
            rand_1 = randint(0, MAX_ROW)
            rand_2 = randint(0, MAX_COLUMN)
            return Point.point_to_string((rand_1, rand_2))

        elif self.strategy == "n1":
            return self.n1play(board_history)

        elif self.strategy == "dumb":
            return self.dumb_play(board_history)

        else:
            raise PlayerCommandError("Undefined Strategy")
예제 #8
0
    def dumb_play(self, board_history):
        if self.stone == None:
            raise PlayerCommandError(
                'You need to set the stone before you can play.')

        current_board = board_history[0]
        for column, maybe_stones in enumerate(current_board):
            for row, maybe_stone in enumerate(current_board):
                try:
                    RuleChecker.check_move(self.stone, (column, row),
                                           board_history)
                    return Point.point_to_string((column, row))
                except IllegalMoveError:
                    pass
                except ImproperHistoryError as e:
                    # raise PlayerCommandError('Invalid history') from e
                    return 'This history makes no sense!'

        return 'pass'
예제 #9
0
    def run(self):
        game_over = False
        # while not game_over:

        outcome = 1

        # Make referee, set stones of player
        while not game_over:
            for curr_player in self.player_holder:
                try:
                    move = curr_player.n1play(self.ref.rr.board_hist)
                    # Random move -- comment this out to use real ai
                    move = Point.point_to_string((randrange(0, MAX_ROW), randrange(0, MAX_COLUMN)))
                    # print(move)
                # If player disconnects, force ref to end game
                except (ConnectionError, player.PlayerCommandError) as e:
                    move = "GO has gone crazy!"

                outcome = self.ref.execute_turn(move)
                # print(outcome)
                if isinstance(outcome, list) and isinstance(outcome[0], str):
                    game_over = True
                    break

        try:
            ok1 = self.player_holder[0].end_game()
            # print(ok1)
            # print("Gets past first")
            ok2 = self.player_holder[1].end_game()
            # print(ok2)
        except (ConnectionError, ConnectionRefusedError, ConnectionAbortedError, ConnectionResetError):
            pass
        # try:
        #
        # except ConnectionError:
        #     pass

        return outcome
예제 #10
0
    def n1play(self, board_history):
        if self.stone == None:
            raise PlayerCommandError(
                'You need to set the stone before you can play.')

        current_board = copy.deepcopy(board_history[0])
        opponent_point = MaybeStone.flip_stone(self.stone)

        for column_index in range(MAX_COLUMN):
            for row_index in range(MAX_ROW):
                if self.check_for_adjacent_stones(opponent_point,
                                                  (column_index, row_index),
                                                  current_board):
                    try:
                        # Try putting a stone in the current position
                        new_board = RuleChecker.check_move(
                            self.stone, (column_index, row_index),
                            board_history)
                        try:
                            Board.place(current_board, self.stone,
                                        (column_index, row_index))
                            # See if capture occurred by comparing with just placing
                            if current_board != new_board:
                                return Point.point_to_string(
                                    (column_index, row_index))
                            else:
                                Board.remove(current_board, self.stone,
                                             (column_index, row_index))
                        except BoardStateError as e:
                            pass
                    except IllegalMoveError as e:
                        pass
                    except ImproperHistoryError as e:
                        # raise PlayerCommandError('Invalid history') from e
                        return 'This history makes no sense!'

        return self.dumb_play(board_history)
예제 #11
0
 def on_mouse_move(x, y):
     nonlocal rect_begin, rect_end
     rect_end = Point(x, y)
     draw_rect(rect_from_2points(rect_begin, rect_end))
예제 #12
0
 def on_mouse_left_down(x, y):
     nonlocal rect_begin, rect_end
     rect_begin = Point(x, y)
     rect_end = Point(x, y)
     draw_rect(rect_from_2points(rect_begin, rect_end))
예제 #13
0
def createMouseEventStream(window, event_name: str):
    stream = rx.Subject()
    window.bind(event_name, lambda ev: stream.on_next(Point(ev.x, ev.y)))
    return stream