コード例 #1
0
    def test_suicide_rule1(self):
        BoardState.BOARD_SIZE = 9

        board = [
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', 'W', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', 'W', 'W', ' ', ' ', ' ', ' ', ' '],
            [' ', 'W', 'B', ' ', 'W', ' ', ' ', ' ', ' '],
            [' ', ' ', 'W', 'W', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
        ]

        state = BoardState(board)
        threw_correct = False

        try:
            state.move('B', Position(3, 3))
        except Exception as e:
            if isinstance(e, SuicideException):
                threw_correct = True

        assert threw_correct
コード例 #2
0
    def test_capture2(self):
        BoardState.BOARD_SIZE = 9

        board = [
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', 'W', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', 'W', 'B', ' ', ' ', ' ', ' ', ' '],
            [' ', 'W', 'B', 'B', 'W', ' ', ' ', ' ', ' '],
            [' ', ' ', 'W', 'W', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
        ]
        tar_board = [
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', 'W', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', 'W', ' ', 'W', ' ', ' ', ' ', ' '],
            [' ', 'W', ' ', ' ', 'W', ' ', ' ', ' ', ' '],
            [' ', ' ', 'W', 'W', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
        ]
        state = BoardState(board)
        state.move('W', Position(2, 4))
        assert BoardState.same_boards(state.board, tar_board)
コード例 #3
0
ファイル: gui.py プロジェクト: itamar8910/Go
 def mouse_click(self, event):
     click_row = round((event.y - self.PADDING) / self.SQUARE_SIZE)
     click_col = round((event.x - self.PADDING) / self.SQUARE_SIZE)
     try:
         self.state.move(self.turn, Position(click_row, click_col))
         self.turn = BoardState.other_player(self.turn)
         self.draw_state()
     except InvalidMoveException as e:
         print(e)
コード例 #4
0
    def test_liberties1(self):
        BoardState.BOARD_SIZE = 9
        board = [
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', 'B', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
        ]

        state = BoardState(board)
        group, captured = state.get_group_and_is_captured(Position(4, 4))

        assert len(group) == 1 and list(group)[0] == Position(
            4, 4) and not captured
コード例 #5
0
 def node_properties_to_move(props):
     assert len(props.keys()) == 1
     player = list(props.keys())[0]
     assert player == 'B' or player == 'W'
     move_letters = props[player][0]
     if len(move_letters) == 0:
         row, col = -1, -1
     else:
         col = ord(move_letters[0]) - ord('a')
         row = ord(move_letters[1]) - ord('a')
     return Move(player, Position(row, col))
コード例 #6
0
    def test_ko_rule1(self):
        BoardState.BOARD_SIZE = 9

        board = [
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', 'B', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            ['B', 'W', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            ['W', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
        ]
        ko_board = [
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            ['B', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            ['W', 'B', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', 'W', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            ['W', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
        ]

        state = BoardState(board)
        state.num_turns = 10
        state.move('B', Position(3, 0))
        state.move('W', Position(4, 0))
        assert BoardState.same_boards(state.board, ko_board)
        threw_correct = False

        try:
            state.move('B', Position(5, 0))
        except Exception as e:
            if isinstance(e, KoException):
                threw_correct = True

        assert threw_correct
コード例 #7
0
    def test_liberties3(self):
        BoardState.BOARD_SIZE = 9

        board = [
            ['B', 'W', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            ['W', 'W', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
        ]

        state = BoardState(board)
        group, captured = state.get_group_and_is_captured(Position(0, 0))

        assert len(group) == 1 and captured
コード例 #8
0
 def get_surrounding_valid_positios(pos: Position) -> List[Position]:
     possible_positions = [
         Position(pos.row + delta_r, pos.col + delta_c)
         for delta_r, delta_c in [(1, 0), (-1, 0), (0, 1), (0, -1)]
     ]
     return [pos for pos in possible_positions if BoardState.valid_pos(pos)]