Skip to content

Commit

Permalink
реализован лабиринт
Browse files Browse the repository at this point in the history
  • Loading branch information
henak1 committed Jan 17, 2025
1 parent 05b41d6 commit 1aa31d4
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 19 deletions.
3 changes: 3 additions & 0 deletions homework00/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions homework00/.idea/homework00.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions homework00/.idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions homework00/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions homework00/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions homework00/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions homework01/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions homework01/.idea/homework01.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions homework01/.idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions homework01/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions homework01/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions homework01/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions homework02/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 30 additions & 14 deletions homework03/maze_gui.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,48 @@
"""Creating a GUI"""

import tkinter as tk
from copy import deepcopy
from tkinter import messagebox, ttk
from typing import List
from tkinter import ttk, messagebox
from maze import bin_tree_maze, solve_maze, add_path_to_grid

from maze import add_path_to_grid, bin_tree_maze, solve_maze


def draw_cell(x, y, color, size: int = 10):
"""Drawing cells"""
x *= size
y *= size
x1 = x + size
y1 = y + size
canvas.create_rectangle(x, y, x1, y1, fill=color)


def draw_maze(grid: List[List[str]], size: int = 10):
def draw_maze(grid: List[List[str | int]], size: int = 10):
"""Drawing the maze"""
for x, row in enumerate(grid):
for y, cell in enumerate(row):
if cell == " ":
color = 'White'
color = "White"
elif cell == "■":
color = 'black'
color = "black"
elif cell == "X":
color = "red"
color = "blue"
draw_cell(y, x, color, size)


def show_solution():
maze, path = solve_maze(GRID)
maze = add_path_to_grid(GRID, path)
if path:
draw_maze(maze, CELL_SIZE)
else:
tk.messagebox.showinfo("Message", "No solutions")
"""What the solution?"""
new_grid = deepcopy(GRID)
new_grid, path = solve_maze(new_grid)
maze = add_path_to_grid(new_grid, path)
draw_maze(maze, CELL_SIZE)


def solvable_maze(grid: List[List[str | int]]) -> bool:
"""Checking if the maze is solvable"""
new_grid = deepcopy(grid)
_, path = solve_maze(new_grid)
return bool(path)


if __name__ == "__main__":
Expand All @@ -39,9 +51,14 @@ def show_solution():

CELL_SIZE = 10
GRID = bin_tree_maze(N, M)
if not solvable_maze(GRID):
print("Maze is not solvable. Trying to regenerate it")
GRID = bin_tree_maze(N, M)

print("Maze is solvable")

window = tk.Tk()
window.title('Maze')
window.title("Maze")
window.geometry("%dx%d" % (M * CELL_SIZE + 100, N * CELL_SIZE + 100))

canvas = tk.Canvas(window, width=M * CELL_SIZE, height=N * CELL_SIZE)
Expand All @@ -51,4 +68,3 @@ def show_solution():
ttk.Button(window, text="Solve", command=show_solution).pack(pady=20)

window.mainloop()

7 changes: 2 additions & 5 deletions homework03/test_maze.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from random import seed

import maze


Expand Down Expand Up @@ -272,9 +273,7 @@ def test_shortest_path(self):
["■", "■", "■", 5, "■"],
]
second_exit_1 = (4, 3)
self.assertEqual(
[(4, 3), (3, 3), (3, 2), (3, 1), (3, 0)], maze.shortest_path(grid_1, second_exit_1)
)
self.assertEqual([(4, 3), (3, 3), (3, 2), (3, 1), (3, 0)], maze.shortest_path(grid_1, second_exit_1))

grid_2 = [
["■", "■", "■", "■", "■", 1, "■"],
Expand Down Expand Up @@ -326,5 +325,3 @@ def test_shortest_path(self):

if __name__ == "__main__":
unittest.main()


0 comments on commit 1aa31d4

Please sign in to comment.