Skip to content

Commit fea1922

Browse files
committed
Sidewinder algorithm
1 parent b421716 commit fea1922

File tree

5 files changed

+53
-6
lines changed

5 files changed

+53
-6
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22

33
## Introduction
44

5-
I'm reading the [Mazes for Programmers](http://www.mazesforprogrammers.com) book, but source code comes in Ruby and I like Python, so I decided to rewrite them as I read. And along the way add tests, both to make sure the conversion is ok and to see a more continuous way than having to write all basic stuff and an "ASCII renderer" to see anything.
5+
I'm reading the [Mazes for Programmers](http://www.mazesforprogrammers.com) book, but source code comes in Ruby and I like Python, so I decided to rewrite them as I read. And along the way add tests, both to make sure the conversion is ok and to see a more continuous way than having to write all basic stuff and an "ASCII renderer" before being able to see anything.
6+
7+
## Implemented algorithms
8+
9+
- Binary Tree
10+
- Sidewinder
11+
12+
Note: This list will grow as I progress with the book.
613

714
## Setup
815

algorithms/binary_tree.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
from random import choice
22

3-
from typing import List, Union # noqa: F401
4-
5-
from base.cell import Cell # noqa: F401
63
from base.grid import Grid
74

85
"""
9-
A binary tree visits each cell in the grid and chooses to carve a passage either north or east.
6+
A binary tree visits each cell in the grid and chooses to carve a passage either north or east with a simple random.
7+
Causes topmost row and rightmost column to always be straight lines.
108
"""
119

1210

@@ -15,7 +13,7 @@ class BinaryTree:
1513
@staticmethod
1614
def on(grid: Grid) -> Grid:
1715
for cell in grid.each_cell():
18-
neighbors = [] # type: List[Union[None, Cell]]
16+
neighbors = []
1917
if cell.north:
2018
neighbors.append(cell.north)
2119
if cell.east:

algorithms/sidewinder.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from random import randint, choice
2+
3+
from base.grid import Grid
4+
5+
"""
6+
A sidewinder visits each cell in the grid and chooses to carve a passage either north or east (similar to Binary Tree),
7+
but running row by row.
8+
Causes topmost row to always be a straight line.
9+
"""
10+
11+
12+
class Sidewinder:
13+
14+
@staticmethod
15+
def on(grid: Grid) -> Grid:
16+
for row in grid.each_row():
17+
run = []
18+
for cell in row:
19+
run.append(cell)
20+
at_eastern_boundary = cell.east is None
21+
at_northen_boundary = cell.north is None
22+
should_close_out = at_eastern_boundary or (not at_northen_boundary and randint(0, 1) == 0)
23+
if should_close_out:
24+
member = choice(run)
25+
if member.north:
26+
member.link(member.north)
27+
run = []
28+
else:
29+
cell.link(cell.east)
30+
return grid

base/cell.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ def neighbors(self) -> List[Union[None, "Cell"]]:
5858
neighbors_list.append(self.west)
5959
return neighbors_list
6060

61+
# The following methods actually lie because don't take into account neighbors/linked-cells, but for now is enough
62+
6163
def __hash__(self) -> int:
6264
return hash((self.column, self.row))
6365

demos/sidewinder_demo.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
from base.grid import Grid
3+
from algorithms.sidewinder import Sidewinder
4+
from renderers.ascii_renderer import ASCIIRenderer
5+
6+
7+
if __name__ == "__main__":
8+
grid = Grid(6, 6)
9+
grid = Sidewinder.on(grid)
10+
ASCIIRenderer.render(grid)

0 commit comments

Comments
 (0)