Class: CellGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/willb-mazegen/cellgraph.rb

Overview

An undirected, unweighted graph of cells on a square grid; edges indicate spaces, non-edges (between neighbors) indicate walls

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ CellGraph

Returns a new instance of CellGraph.



7
8
9
10
11
12
13
# File 'lib/willb-mazegen/cellgraph.rb', line 7

def initialize(x,y)
  @x = x
  @y = y
  @node_coords = init_coords
  @node_edges = init_edges
  @node_neighbors = init_neighbors
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



15
16
17
# File 'lib/willb-mazegen/cellgraph.rb', line 15

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



15
16
17
# File 'lib/willb-mazegen/cellgraph.rb', line 15

def y
  @y
end

Instance Method Details

#add_edges(edges) ⇒ Object Also known as: add_edge



17
18
19
20
21
22
23
# File 'lib/willb-mazegen/cellgraph.rb', line 17

def add_edges(edges)
  edges.each do |source,dest|
    @node_edges[source] << dest
    # recall that this is an undirected graph
    @node_edges[dest] << source
  end
end

#cell_for_coords(x, y) ⇒ Object



49
50
51
# File 'lib/willb-mazegen/cellgraph.rb', line 49

def cell_for_coords(x,y)
  @x * y + x
end

#coords_for_cell(node) ⇒ Object



45
46
47
# File 'lib/willb-mazegen/cellgraph.rb', line 45

def coords_for_cell(node)
  @node_coords[node]
end

#edgesObject



35
36
37
38
39
# File 'lib/willb-mazegen/cellgraph.rb', line 35

def edges
  @node_edges.map do |source, dests|
    dests.map {|dest| [source, dest]}
  end.flatten(1)
end

#edges_from(node) ⇒ Object



31
32
33
# File 'lib/willb-mazegen/cellgraph.rb', line 31

def edges_from(node)
  @node_edges[node].map {|dest| [node, dest]}
end

#in_graph(px, py) ⇒ Object



53
54
55
# File 'lib/willb-mazegen/cellgraph.rb', line 53

def in_graph(px,py)
  px >= 0 && py >= 0 && py < @y && px < @x
end

#neighbors(node) ⇒ Object



41
42
43
# File 'lib/willb-mazegen/cellgraph.rb', line 41

def neighbors(node)
  @node_neighbors[node]
end

#reset_edgesObject



57
58
59
# File 'lib/willb-mazegen/cellgraph.rb', line 57

def reset_edges
  @node_edges = init_edges
end

#sizeObject



27
28
29
# File 'lib/willb-mazegen/cellgraph.rb', line 27

def size
  return x * y
end