예제 #1
0
 def _pick_random_move(self, board: Board) -> Tuple[int, int]:
     while True:
         row = random.randint(0, BOARD_NUM_ROWS)
         col = random.randint(0, BOARD_NUM_COLS)
         is_valid, _ = board.is_valid_move(row, col)
         if is_valid:
             break
     return (row, col)
예제 #2
0
    def pick_move(self, board: Board) -> Tuple[int, int]:
        """CPU logic to pick a move"""
        # if we are flying blind, just go for anything
        if len(self.last_hits) == 0:
            return self._pick_random_move(board)

        # find all positions surrounding any of our previous hits
        surrounding_positions = board.surrounding_positions(self.last_hits)
        for row, col in surrounding_positions:
            # pick first surrounding position we find that is valid
            # TODO: we can optimize to have it
            # keep searching along a direction
            is_valid, _ = board.is_valid_move(row, col)
            if is_valid:
                return (row, col)

        # no valid surrounding moves, so reset the last hits
        self.last_hits = []
        return self.pick_move(board)
예제 #3
0
    def validate_move(self, board: Board, row: int, col: int):
        """Validtes the human move on a board
        Paramters
        ---------
        board : `battleship.board.Board`
          The board to make a move on

        Raises
        ------
        `battleship.errors.InvalidBoardError`
            If the board is the player's board.
            This must be run on a board that is not yours.
        `battleship.errors.InvalidMoveError`
            If the move is invalid
            Moves can be invalid because they are out of bounds or
            because they've already been attempted
        """
        if board == self.board:
            raise InvalidBoardError("Cannot make a move on your own board")

        is_valid, err = board.is_valid_move(row, col)
        if not is_valid:
            raise InvalidMoveError(err)