Skip to content

Commit 7284ff4

Browse files
committed
Added all 4 parts of 10. Maze Generator
1 parent 5f82223 commit 7284ff4

File tree

12 files changed

+656
-0
lines changed

12 files changed

+656
-0
lines changed

10.1 Maze Generator/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Maze Generator Part 1
2+
A maze generator. In this part, we draw the cells and their walls. This is the 1st part of the tenth challenge in [The Coding Train's](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw) [Coding Challenges](https://thecodingtrain.com/CodingChallenges/).
3+
4+
## Demo:
5+
![Demo of the program](./demo.gif)
6+
7+
[Link to challenge](https://thecodingtrain.com/CodingChallenges/010.1-maze-dfs-p5.html)
8+
9+
This code was contributed by Soumitra Shewale ([@soumitradev](https://github.com/soumitradev))
10+
11+
## License
12+
[MIT License](../LICENSE)

10.1 Maze Generator/demo.gif

33 KB
Loading

10.1 Maze Generator/maze_generator.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Definition of Cell object, will hold all the properties of cell
2+
class Cell:
3+
def __init__(self, row, col, walls, surface):
4+
"""
5+
Create a cell
6+
params
7+
row : Row index of cell in 2D array
8+
col : Column index of cell in 2D array
9+
walls : List of booleans in the order [top, right, bottom, left] representing if the respective walls of that cell exist
10+
surface : Surface on which cell should be drawn
11+
"""
12+
self.row = row
13+
self.col = col
14+
self.walls = walls
15+
self.surface = surface
16+
def show(self):
17+
"""
18+
Draws each cell, according to if it's visited, and drwas each wall (if the wall exists)
19+
"""
20+
pygame.draw.rect(self.surface, CELL_COLOR, pygame.Rect(self.col * CELL_WIDTH, self.row * CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT))
21+
22+
if self.walls[0]:
23+
pygame.draw.line(self.surface, WALL_COLOR, (self.col * CELL_WIDTH, self.row * CELL_HEIGHT), ((self.col + 1) * CELL_WIDTH, self.row * CELL_HEIGHT))
24+
if self.walls[1]:
25+
pygame.draw.line(self.surface, WALL_COLOR, ((self.col + 1) * CELL_WIDTH, self.row * CELL_HEIGHT), ((self.col + 1) * CELL_WIDTH, (self.row + 1) * CELL_HEIGHT))
26+
if self.walls[2]:
27+
pygame.draw.line(self.surface, WALL_COLOR, ((self.col + 1) * CELL_WIDTH, (self.row + 1) * CELL_HEIGHT), (self.col * CELL_WIDTH, (self.row + 1) * CELL_HEIGHT))
28+
if self.walls[3]:
29+
pygame.draw.line(self.surface, WALL_COLOR, (self.col * CELL_WIDTH, (self.row + 1) * CELL_HEIGHT), (self.col * CELL_WIDTH, self.row * CELL_HEIGHT))
30+
31+
if __name__ == "__main__":
32+
import pygame
33+
import random
34+
import sys
35+
36+
# Set for Window size
37+
HORIZONTAL_BLOCKS = 32
38+
VERTICAL_BLOCKS = 18
39+
40+
CELL_WIDTH = 50
41+
CELL_HEIGHT = 50
42+
43+
# Set FPS
44+
FPS = 60
45+
46+
# Color constants
47+
CELL_COLOR = 255, 255, 255
48+
WALL_COLOR = 0, 0, 0
49+
BG_COLOR = 0, 0, 0
50+
51+
# Initialise PyGame
52+
pygame.init()
53+
54+
# Create screen for showing viusalisation
55+
size = width, height = HORIZONTAL_BLOCKS * CELL_HEIGHT, VERTICAL_BLOCKS * CELL_HEIGHT
56+
screen = pygame.display.set_mode(size)
57+
pygame.display.set_caption("Maze Generator")
58+
59+
# Create a 1D list of cells, and append cells to it
60+
cells = []
61+
62+
for i in range(VERTICAL_BLOCKS):
63+
for j in range(HORIZONTAL_BLOCKS):
64+
cells.append(Cell(i, j, [True, True, True, True], screen))
65+
66+
# While game is running, for every frame,
67+
while True:
68+
69+
# Store events to process quit and KeyDown events
70+
events = pygame.event.get()
71+
72+
# Handle exit event
73+
for event in events:
74+
# Let user exit
75+
if event.type == pygame.QUIT: sys.exit()
76+
77+
# Fill the screen with BG_COLOR
78+
screen.fill(BG_COLOR)
79+
80+
# Show every cell
81+
for k in cells:
82+
k.show()
83+
84+
# Update the display
85+
pygame.display.update()
86+
87+
# Tick the display as per FPS
88+
pygame.time.Clock().tick(FPS)
89+

10.2 Maze Generator/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Maze Generator Part 2
2+
A maze generator. In this part, we build the forward tracking part of the algorithm. This is the 2nd part of the tenth challenge in [The Coding Train's](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw) [Coding Challenges](https://thecodingtrain.com/CodingChallenges/).
3+
4+
## Demo:
5+
![Demo of the program](./demo.gif)
6+
7+
[Link to challenge](https://thecodingtrain.com/CodingChallenges/010.2-maze-dfs-p5.html)
8+
9+
This code was contributed by Soumitra Shewale ([@soumitradev](https://github.com/soumitradev))
10+
11+
## License
12+
[MIT License](../LICENSE)

10.2 Maze Generator/demo.gif

62.4 KB
Loading

10.2 Maze Generator/maze_generator.py

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Definition of Cell object, will hold all the properties of cell
2+
class Cell:
3+
def __init__(self, row, col, walls, surface):
4+
"""
5+
Create a cell
6+
params
7+
row : Row index of cell in 2D array
8+
col : Column index of cell in 2D array
9+
walls : List of booleans in the order [top, right, bottom, left] representing if the respective walls of that cell exist
10+
surface : Surface on which cell should be drawn
11+
visited : Boolean to show if the cell has been visited by the cursor
12+
"""
13+
self.row = row
14+
self.col = col
15+
self.walls = walls
16+
self.surface = surface
17+
self.visited = False
18+
19+
def show(self):
20+
"""
21+
Draws each cell, according to if it's visited, and drwas each wall (if the wall exists)
22+
"""
23+
if self.visited:
24+
pygame.draw.rect(self.surface, VISITED, pygame.Rect(self.col * CELL_WIDTH, self.row * CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT))
25+
else:
26+
pygame.draw.rect(self.surface, CELL_COLOR, pygame.Rect(self.col * CELL_WIDTH, self.row * CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT))
27+
28+
if self.walls[0]:
29+
pygame.draw.line(self.surface, WALL_COLOR, (self.col * CELL_WIDTH, self.row * CELL_HEIGHT), ((self.col + 1) * CELL_WIDTH, self.row * CELL_HEIGHT))
30+
if self.walls[1]:
31+
pygame.draw.line(self.surface, WALL_COLOR, ((self.col + 1) * CELL_WIDTH, self.row * CELL_HEIGHT), ((self.col + 1) * CELL_WIDTH, (self.row + 1) * CELL_HEIGHT))
32+
if self.walls[2]:
33+
pygame.draw.line(self.surface, WALL_COLOR, ((self.col + 1) * CELL_WIDTH, (self.row + 1) * CELL_HEIGHT), (self.col * CELL_WIDTH, (self.row + 1) * CELL_HEIGHT))
34+
if self.walls[3]:
35+
pygame.draw.line(self.surface, WALL_COLOR, (self.col * CELL_WIDTH, (self.row + 1) * CELL_HEIGHT), (self.col * CELL_WIDTH, self.row * CELL_HEIGHT))
36+
37+
def check_neighbours(self):
38+
"""
39+
Return a random unvisited neighbour
40+
"""
41+
neigbours = []
42+
43+
# Get the cell if the neighbour cell exists
44+
top = cells[self.index(self.row - 1, self.col)] if self.index(self.row - 1, self.col) else None
45+
right = cells[self.index(self.row, self.col + 1)] if self.index(self.row, self.col + 1) else None
46+
bottom = cells[self.index(self.row + 1, self.col)] if self.index(self.row + 1, self.col) else None
47+
left = cells[self.index(self.row, self.col - 1)] if self.index(self.row - 1, self.col - 1) else None
48+
49+
# For every neighbour, if it is exists and is not visited, add it to the list of neighbours
50+
for i in [top, right, bottom, left]:
51+
if i and (not i.visited):
52+
neigbours.append(i)
53+
54+
# If it has any candidate unvisited neighbours, return a random neighbour. Else, return None
55+
if neigbours:
56+
return random.choice(neigbours)
57+
else:
58+
return None
59+
60+
@staticmethod
61+
def index(row, col):
62+
"""
63+
Get the 1D array index of an element in a 2D array. Rows are stacked next to each other without seperation in the 1D array
64+
params
65+
row : Row index of the cell in 2D array
66+
col : Column index of the cell in 2D array
67+
"""
68+
if (row < 0) or (col < 0) or (row >= VERTICAL_BLOCKS) or (col >= HORIZONTAL_BLOCKS):
69+
return None
70+
return col + (row * HORIZONTAL_BLOCKS)
71+
72+
if __name__ == "__main__":
73+
import pygame
74+
import random
75+
import sys
76+
77+
# Set for Window size
78+
HORIZONTAL_BLOCKS = 32
79+
VERTICAL_BLOCKS = 18
80+
81+
CELL_WIDTH = 50
82+
CELL_HEIGHT = 50
83+
84+
# Set FPS
85+
FPS = 60
86+
87+
# Color constants
88+
CELL_COLOR = 255, 255, 255
89+
VISITED = 200, 200, 200
90+
WALL_COLOR = 0, 0, 0
91+
BG_COLOR = 0, 0, 0
92+
93+
# Initialise PyGame
94+
pygame.init()
95+
96+
# Create screen for showing viusalisation
97+
size = width, height = HORIZONTAL_BLOCKS * CELL_HEIGHT, VERTICAL_BLOCKS * CELL_HEIGHT
98+
screen = pygame.display.set_mode(size)
99+
pygame.display.set_caption("Maze Generator")
100+
101+
# Create a 1D list of cells, and append cells to it
102+
cells = []
103+
104+
for i in range(VERTICAL_BLOCKS):
105+
for j in range(HORIZONTAL_BLOCKS):
106+
cells.append(Cell(i, j, [True, True, True, True], screen))
107+
108+
# Start at the top left corner
109+
cursor = cells[0]
110+
111+
# While game is running, for every frame,
112+
while True:
113+
114+
# Store events to process quit and KeyDown events
115+
events = pygame.event.get()
116+
117+
# Handle exit event
118+
for event in events:
119+
# Let user exit
120+
if event.type == pygame.QUIT: sys.exit()
121+
122+
# Fill the screen with BG_COLOR
123+
screen.fill(BG_COLOR)
124+
125+
# Show every cell
126+
for k in cells:
127+
k.show()
128+
129+
# Mark the current cell as visited
130+
cursor.visited = True
131+
132+
# Grab a random neighbour to current cell, and mark it as visited
133+
next = cursor.check_neighbours()
134+
if next:
135+
next.visited = True
136+
137+
# Move to the neighbour cell, and repeat
138+
cursor = next
139+
140+
# Update the display
141+
pygame.display.update()
142+
143+
# Tick the display as per FPS
144+
pygame.time.Clock().tick(FPS)
145+

10.3 Maze Generator/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Maze Generator Part 3
2+
A maze generator. In this part, we write a way to remove the walls that seperate 2 cells. This is the 3rd part of the tenth challenge in [The Coding Train's](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw) [Coding Challenges](https://thecodingtrain.com/CodingChallenges/).
3+
4+
## Demo:
5+
![Demo of the program](./demo.gif)
6+
7+
[Link to challenge](https://thecodingtrain.com/CodingChallenges/010.3-maze-dfs-p5.html)
8+
9+
This code was contributed by Soumitra Shewale ([@soumitradev](https://github.com/soumitradev))
10+
11+
## License
12+
[MIT License](../LICENSE)

10.3 Maze Generator/demo.gif

52.2 KB
Loading

0 commit comments

Comments
 (0)