/
doduohor
/
SavelevSD_maze
Обзор
Документация
Войти
/
doduohor
/
SavelevSD_maze
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main.py
133 строки
4 KB
Doduohor
first commit
16 апр 2026, 21:44
16 апр 2026, 21:44
cc5ba95
Код
Авторство
О чём код?
import random import os import sys import tty import termios def get_dimension(prompt): while True: value = input(prompt) if value.isdigit(): number = int(value) if number >= 7 and number % 2 != 0: return number print("Ошибка. Нужно нечетное число (минимум 7).") def generate_maze(width, height): maze =[] for y in range(height): row =[] for x in range(width): row.append(1) maze.append(row) stack_x = [1] stack_y = [1] maze[1][1] = 0 while len(stack_x) > 0: cx = stack_x[-1] cy = stack_y[-1] unvisited = [] if cy - 2 > 0 and maze[cy - 2][cx] == 1: unvisited.append([cx, cy - 2]) if cy + 2 < height - 1 and maze[cy + 2][cx] == 1: unvisited.append([cx, cy + 2]) if cx - 2 > 0 and maze[cy][cx - 2] == 1: unvisited.append([cx - 2, cy]) if cx + 2 < width - 1 and maze[cy][cx + 2] == 1: unvisited.append([cx + 2, cy]) if len(unvisited) > 0: choice = random.choice(unvisited) nx = choice[0] ny = choice[1] maze[ny][nx] = 0 maze[(cy + ny) // 2][(cx + nx) // 2] = 0 stack_x.append(nx) stack_y.append(ny) else: stack_x.pop() stack_y.pop() maze[0][1] = 0 maze[height - 1][width - 2] = 0 return maze def draw_maze(maze, player_x, player_y, exit_x, exit_y): os.system('clear') for y in range(len(maze)): row_string = "" for x in range(len(maze[y])): if x == player_x and y == player_y: row_string += "SS" elif x == exit_x and y == exit_y: row_string += "EX" elif maze[y][x] == 1: row_string += "██" else: row_string += " " print(row_string) def get_keyboard_move(): move = "none" fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(fd) ch = sys.stdin.read(1) if ch == '\x1b': ch += sys.stdin.read(2) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) if ch == '\x03': sys.exit(0) if ch == '\x1b[A': move = "up" elif ch == '\x1b[B': move = "down" elif ch == '\x1b[C': move = "right" elif ch == '\x1b[D': move = "left" return move def play_game(maze, width, height): player_x = 1 player_y = 0 exit_x = width - 2 exit_y = height - 1 while True: draw_maze(maze, player_x, player_y, exit_x, exit_y) if player_x == exit_x and player_y == exit_y: print("Поздравляю. Вы нашли выход из лабиринта") break move = "none" while move == "none": move = get_keyboard_move() new_x = player_x new_y = player_y if move == "up" and player_y > 0: new_y -= 1 if move == "down" and player_y < height - 1: new_y += 1 if move == "left" and player_x > 0: new_x -= 1 if move == "right" and player_x < width - 1: new_x += 1 if maze[new_y][new_x] == 0: player_x = new_x player_y = new_y def main(): print("Добро пожаловать в игру 'Лабиринт'") width = get_dimension("Введите ширину лабиринта (например, 21): ") height = get_dimension("Введите высоту лабиринта (например, 11): ") maze = generate_maze(width, height) play_game(maze, width, height) if __name__ == "__main__": main()