grid module

Generate random hexagonal cell grids.

Grids are composed of hexagonal Cells. Groups of adjacent Cells are assigned to adjacent Areas of random size and shape.

All Cell and Area references are indices into the Grid.cells and Grid.areas tuples, respectively.

Grid instances are immutable and intended for Game generation, but may be useful for other hex-map based games as well. They provide coordinates for convenient frontend map rendering.

class dicewars.grid.Grid(grid_width=28, grid_height=32, max_num_areas=30, min_area_size=5)

Generate a grid and assign Cells to Areas.

Parameters
  • grid_width (int) – number of cell columns

  • grid_height (int) – number of cell rows

  • max_num_areas (int) – maximal number of areas to create

  • min_area_size (int) – minimal number of cells per area

Raises
  • TypeError – if a parameter is not int

  • ValueError – if a parameter is out of range

Note

The number of created areas is less than max_num_areas if there are not enough cells left to assign.

DEFAULT_GRID_WIDTH = 28

Default for the grid_width parameter. (int)

DEFAULT_GRID_HEIGHT = 32

Default for the grid_height parameter. (int)

DEFAULT_MAX_NUM_AREAS = 30

Default for the max_num_areas parameter. (int)

DEFAULT_MIN_AREA_SIZE = 5

Default for the min_area_size parameter. (int)

property grid_size

The number of cell columns (grid_width) and rows (grid_height). (tuple(int, int))

property map_size

The size of the grid in pixels. (tuple(int, int))

For frontend map rendering. map_size is the bounding box size of all Cell.bboxes and may be used for proper map scaling.

property cells

All Cell instances created by the grid. (tuple(Cell))

property areas

All Area instances created by the grid. (tuple(Area))

dump()

Dump the grid (area indices) to the console.

class dicewars.grid.Cell(idx, grid_x, grid_y, area, border, bbox)

A hexagonal cell - the basic building block of each Grid. (namedtuple)

Cell instances are created by Grid and used for Area assignment. They are not used by the game logic, but may be useful for frontends.

idx: int

The cell’s index in Grid.cells.

grid_x: int

The cell’s grid column.

grid_y: int

The cell’s grid row.

area: int

The index of the Area the cell is assigned to, -1 if not assigned.

border: tuple(tuple(int, int))

For frontend map rendering. A 6-tuple of (x, y) pixel coordinates of the hexagonal cell polygon on the map (in counter-clockwise order, starting at the top center point).

bbox: tuple(tuple(int, int), tuple(int, int))

The upper left (bbox[0]) and lower right (bbox[1]) (x, y) pixel coordinates of the cell’s bounding box on the map.

class dicewars.grid.Area(idx, cells, neighbors, center, border, bbox)

A group of adjacent Cells in a Grid. (namedtuple)

Area instances are created by Grid and used by the game logic. They may be useful for frontend map rendering as well.

idx: int

The area’s index in Grid.areas.

cells: tuple(int)

The indices of all Cells assigned to the area.

neighbors: tuple(int)

The indices of all areas adjacent to the area.

center: int

For frontend map rendering. The index of the area’s center Cell.

border: tuple(tuple(int, int))

For frontend map rendering. A tuple of (x, y) pixel coordinates of the area polygon on the map (outer Cell edges, in counter-clockwise order).

bbox: tuple(tuple(int, int), tuple(int, int))

The upper left (bbox[0]) and lower right (bbox[1]) (x, y) pixel coordinates of the area’s bounding box on the map.