Documentation for package maze

Contents:

class maze.BaseWall(room_pos, wall)[source]

A reference to the wall of a room.

A wall has an index, a direction, a span and a position.
  • The index is its position in the list of walls. This corresponds to its symbolic name.
  • The direction is a direction vector for the wall; up and right are positive directions. The direction is the movement vector in the maze room matrix and not necessarily an actual physical direction.
  • The span is the physical start and end angle of the wall.
  • The position is the coordinates of the room to which the wall belongs in the maze room matrix.
Furthermore, walls know their back and opposite.
  • The back is the same wall in the room on the other side of a wall. A wall will compare equally with its back.
  • The opposite is the wall opposing a wall in the same room. Depending on the layout of a room, there may be no opposite wall. Triangular mazes do not have opposite walls.
Walls have factory methods.
  • from_direction will create a wall when the room and the direction is known. The direction must be the direction vector of the room.
  • from_room_pos will create all walls for a room.
  • from_corner will create all walls that meet in a corner.
Parameters:
  • room_pos ((int, int)) – The position of the room in which this wall is.
  • wall (int) – The wall index.
back[source]

The wall on the other side of the wall.

corner_walls[source]

All walls in the corner that contains the start span of this wall.

direction[source]

The direction vector to move in when going through the wall.

classmethod from_corner(room_pos, wall_index)[source]

Generates all walls that meet in the corner where the wall has its start span.

The walls are generated counter-clockwise, starting with the wall described by the parameters.

Parameters:
  • room_pos ((int, int)) – The position of the room.
  • wall_index (int) – The index of the wall.
classmethod from_direction(room_pos, direction)[source]

Creates a new wall from a direction.

Parameters:
  • room_pos ((int, int)) – The position of the room.
  • direction (int) – The direction of the wall.
Returns:

a new wall

Return type:

self

Raises ValueError:
 

if the direction is invalid

classmethod from_room_pos(room_pos)[source]

Generates all walls of a room.

Parameters:room_pos ((int, int)) – The room coordinates.
opposite[source]

The opposite wall in the same room.

span[source]

The span of this wall

class maze.Room[source]

A room is a part of the maze.

A room has a set of walls. Walls in the set are considered to have doors.

In addition to the methods defined, the following constructs are allowed:

  • if room: => if bool(room.doors):
  • if wall in room: => if wall.has_door(wall):
  • if room[Wall.LEFT]: => if room.has_door(wall):
  • room[Wall.LEFT] = bool => room.set_door(Wall.LEFT, bool)
  • room += Wall.LEFT => room.add_door(Wall.LEFT)
  • room -= Wall.LEFT => room_remove_door(Wall.LEFT)
add_door(wall_index)[source]

Adds a door.

Parameters:wall_index (int) – The wall to which to add a door.
Raises IndexError:
 if wall_index is not a valid wall
has_door(wall_index)[source]

Returns whether a wall has a door.

Parameters:wall_index (int) – The wall to check.
Returns:whether the wall has a door
Return type:bool
Raises IndexError:
 if wall is not a valid wall
remove_door(wall_index)[source]

Removes a door.

Parameters:wall_index (int) – The wall from which to remove a door.
Raises IndexError:
 if wall_index is not a valid wall
set_door(wall_index, has_door)[source]

Adds or removes a door depending on has_door.

Parameters:
  • wall_index (int) – The wall to modify.
  • has_door (bool) – Whether to add or remove the door.
Raises IndexError:
 

if wall_index is not a valid wall

class maze.BaseMaze(width, height)[source]

A maze is a grid of rooms.

In addition to the methods defined, the following constructs are allowed:

  • maze[room_pos] => maze.rooms[room_pos[1]][room_pos[0]]
  • if room_pos in maze: => if room_pos[0] >= 0 and room_pos[1] >= 0 and room_pos[0] < maze.width and room_pos[1] < maze.height
  • maze[room_pos1:room_pos2] = True => maze.add_door(room_pos1, room_pos2)
  • maze[room_pos1:room_pos2] = True => maze.remove_door(room_pos1, room_pos2)
  • maze[room_pos1:room_pos2] => maze.walk_path(room_pos1, room_pos2)
  • for room_pos in maze: => for room_pos in (rp for rp in maze.room_positions if maze[rp]):
Parameters:
  • width (int) – The width of the maze.
  • height (int) – The height of the maze.
class Room

A room is a part of the maze.

A room has a set of walls. Walls in the set are considered to have doors.

In addition to the methods defined, the following constructs are allowed:

  • if room: => if bool(room.doors):
  • if wall in room: => if wall.has_door(wall):
  • if room[Wall.LEFT]: => if room.has_door(wall):
  • room[Wall.LEFT] = bool => room.set_door(Wall.LEFT, bool)
  • room += Wall.LEFT => room.add_door(Wall.LEFT)
  • room -= Wall.LEFT => room_remove_door(Wall.LEFT)
add_door(wall_index)

Adds a door.

Parameters:wall_index (int) – The wall to which to add a door.
Raises IndexError:
 if wall_index is not a valid wall
has_door(wall_index)

Returns whether a wall has a door.

Parameters:wall_index (int) – The wall to check.
Returns:whether the wall has a door
Return type:bool
Raises IndexError:
 if wall is not a valid wall
remove_door(wall_index)

Removes a door.

Parameters:wall_index (int) – The wall from which to remove a door.
Raises IndexError:
 if wall_index is not a valid wall
set_door(wall_index, has_door)

Adds or removes a door depending on has_door.

Parameters:
  • wall_index (int) – The wall to modify.
  • has_door (bool) – Whether to add or remove the door.
Raises IndexError:
 

if wall_index is not a valid wall

BaseMaze.Wall

alias of BaseWall

BaseMaze.add_door(from_pos, to_pos)[source]

Adds a door between two rooms.

Parameters:
  • from_pos ((int, int)) – The coordinate of the first room.
  • to_pos ((int, int)) – The coordinate of the second room.
Raises:
  • IndexError – if a room lies outside of the maze
  • ValueError – if the rooms are not adjacent
BaseMaze.adjacent(room1_pos, room2_pos)[source]

Returns whether two rooms are adjacent.

The rooms are adjacent if the room at room1_pos has a wall that leads to the room at room2_pos.

Parameters:
  • room1_pos ((int, int)) – The coordinate of the first room.
  • room2_pos ((int, int)) – The coordinate of the second room.
Returns:

whether there is a wall between the two rooms

Return type:

bool

BaseMaze.connected(room1_pos, room2_pos)[source]

Returns whether two rooms are connected by a single wall containing a door.

Parameters:
  • room1_pos ((int, int)) – The coordinate of the first room.
  • room2_pos ((int, int)) – The coordinate of the second room.
Returns:

whether the two rooms are adjacent and have doors

Return type:

bool

BaseMaze.doors(room_pos)[source]

Generates all walls with doors.

Parameters:room_pos ((int, int)) – The coordinates of the room.
Raises IndexError:
 if a room lies outside of the maze
BaseMaze.edge(wall)[source]

Returns whether a wall is on the edge of the maze.

Parameters:wall (BaseWall) – The wall.
Returns:whether the wall is on the edge of the maze
Return type:bool
BaseMaze.edge_walls[source]

A generator that yields all walls on the edge of the maze; the order is undefined

BaseMaze.get_center(room_pos)[source]

Returns the physical coordinates of the centre of a room.

If lines are drawn on a circle with radius 1.0 centred on this point between the angles corresponding to the spans of the walls, the lines of adjacent rooms will cover each other.

Parameters:room_pos ((int, int)) – The position of the room.
Returns:the coordinates of the room
Return type:(float, float)
Raises IndexError:
 if a room lies outside of the maze
BaseMaze.remove_door(from_pos, to_pos)[source]

Removes a door between two rooms.

Parameters:
  • from_pos ((int, int)) – The coordinate of the first room.
  • to_pos ((int, int)) – The coordinate of the second room.
Raises:
  • IndexError – if a room lies outside of the maze
  • ValueError – if the rooms are not adjacent
BaseMaze.room_positions[source]

A generator that yields the positions of all rooms

BaseMaze.set_door(room_pos, wall, has_door)[source]

Adds or removes a door.

Parameters:
  • room_pos ((int, int)) – The coordinates of the room.
  • wall (BaseWall) – The wall to modify.
  • has_door (bool) – True to add the door and False to remove it.
Raises IndexError:
 

if room_pos lies outside of the maze

BaseMaze.walk(wall, require_door=False)[source]

Returns the coordinates of the room through the specified wall.

Parameters:
  • wall (BaseWall) – The wall to walk through.
  • require_door (bool) – Whether to require a door in the specified direction.
Returns:

the destination coordinates

Return type:

(int, int)

Raises:
  • ValueError – if require_door is True and there is no door on the wall
  • IndexError – if the destination room lies outside of the maze
BaseMaze.walk_from(room_pos, wall, require_door=False)[source]

Returns the coordinates of the room through the specified wall.

The starting room, room_pos, may be outside of the maze if it is immediately on the edge and the movement is inside the maze.

Parameters:
  • room_pos ((int, int)) – The room from which to walk.
  • wall (BaseWall or int) – The wall to walk through.
  • require_door (bool) – Whether to require a door in the specified direction.
Returns:

the destination coordinates

Return type:

(int, int)

Raises:
  • ValueError – if require_door is True and there is no door on the wall
  • IndexError – if the destination room lies outside of the maze
BaseMaze.walk_path(from_pos, to_pos, visitor=<function <lambda> at 0xb66dff44>)[source]

Generates all rooms on the shortest path between two rooms.

Parameters:
  • from_pos – The room in which to start. This room is included.
  • to_pos ((int, int)) – The last room to visit.
  • visitor (func((int, int))) – A callback to call for every room encountered. This callback may raise StopIteration to cancel the walking.
Raises ValueError:
 

if there is no path between the rooms

BaseMaze.walls(room_pos)[source]

Generates all walls of a room.

Parameters:room_pos ((int, int)) – The coordinates of the room.
Raises IndexError:
 if a room lies outside of the maze
class maze.quad.Maze(width, height)[source]

A maze with square rooms.

This is the traditional maze. Maze coordinates correspond to physical coordinates after a simple scale operation.

class maze.tri.TriMaze(width, height)[source]

A maze with triangular rooms.

The rooms alternate between having a wall facing downwards and a corner facing downwards. The room at x, y will have a corner facing downwards if x + y is odd.

Walls in a triangular maze do not have an opposite wall.

class maze.hex.HexMaze(width, height)[source]

A maze with hexagonal rooms.

The rooms are laid out in rows, where every odd row is moved a half room in the positive horizontal direction; the appearance of the maze is that of a honey comb.

Indices and tables

Table Of Contents

This Page