My Python Sokoban Level Generator hangs indefinitely without display the level itself

I have implemented a Sokoban level generator in Python using numpy, but when I run the code, it doesn’t print anything or generate the expected Sokoban level. The generator is supposed to go through several steps:

  1. Placing Borders: Surrounding the level with walls (#)
  2. Placing Walls: Adding random walls within the level for complexity.
  3. Placing Player: Positioning the player (@) at a random spot.
  4. Placing Boxes and Goals: Adding boxes ($) and their respective goals (.) randomly.
  5. Checking Solvability: Ensuring the level is solvable by confirming all boxes can reach their respective goals.
  6. Generating and Returning the Level: Constructing and returning a 2D list representing the Sokoban level.

Code Implementation:

import numpy as np

class LevelGenerator:
    def __init__(self, size, num_walls):
        self.size = size
        self.num_walls = num_walls
        self.level = np.full((size, size), ' ')
        self.generate_level()

    def place_borders(self):
        self.level[0, :] = '#'
        self.level[:, 0] = '#'
        self.level[-1, :] = '#'
        self.level[:, -1] = '#'

    def place_walls(self):
        for _ in range(self.num_walls):
            while True:
                x, y = np.random.randint(1, self.size-1, size=2)
                if self.level[x, y] == ' ':
                    self.level[x, y] = '#'
                    break

    def place_player(self):
        while True:
            x, y = np.random.randint(1, self.size-1, size=2)
            if self.level[x, y] == ' ':
                self.level[x, y] = '@'
                self.player_pos = (x, y)
                break

    def place_boxes_and_goals(self):
        num_boxes = (self.size - 2) // 2
        self.boxes = []
        self.goals = []
        for _ in range(num_boxes):
            while True:
                x, y = np.random.randint(1, self.size-1, size=2)
                if self.level[x, y] == ' ':
                    self.level[x, y] = '$'
                    self.boxes.append((x, y))
                    break
        for _ in range(num_boxes):
            while True:
                x, y = np.random.randint(1, self.size-1, size=2)
                if self.level[x, y] == ' ':
                    self.level[x, y] = '.'
                    self.goals.append((x, y))
                    break

    def is_solvable(self):
        return all(self.is_reachable(box) for box in self.boxes) and all(self.is_reachable(goal) for goal in self.goals)

    def is_reachable(self, target):
        x, y = target
        if self.level[x, y] in '#':
            return False

        visited = set()
        stack = [self.player_pos]
        
        while stack:
            cx, cy = stack.pop()
            if (cx, cy) == target:
                return True
            if (cx, cy) in visited:
                continue
            visited.add((cx, cy))

            for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                nx, ny = cx + dx, cy + dy
                if 0 <= nx < self.size and 0 <= ny < self.size and self.level[nx, ny] not in '#$' and (nx, ny) not in visited:
                    stack.append((nx, ny))

        return False

    def generate_level(self):
        while True:
            self.level.fill(' ')
            self.place_borders()
            self.place_walls()
            self.place_player()
            self.place_boxes_and_goals()
            if self.is_solvable():
                break

    def get_level(self):
        return self.level
# Example usage
print("Generating Sokoban level...")
size = 10
num_walls = 20
generator = LevelGenerator(size, num_walls)
level = generator.get_level()
print("n".join("".join(row) for row in level))

Issue Explanation:

Upon running the code, instead of getting a printed Sokoban level, the program seems to hang indefinitely without displaying anything. I expected the program to generate and print a Sokoban level based on the specified size (size = 10) and number of walls (num_walls = 20).

Request:

Could someone please assist in identifying what might be causing the code to not display or print the generated Sokoban level? Any insights or suggestions on how to fix this issue would be greatly appreciated.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật