Class: AMazeIng::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/a_maze_ing/cell.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cell_index_x, cell_index_y) ⇒ Cell

Returns a new instance of Cell.



4
5
6
7
8
9
10
11
12
# File 'lib/a_maze_ing/cell.rb', line 4

def initialize(cell_index_x, cell_index_y)
  @cell_index_x = cell_index_x
  @cell_index_y = cell_index_y
  
  @walls = [true, true, true, true]
  @visited = false
  @color = Color::GREEN
  @is_current = false
end

Instance Attribute Details

#cell_index_xObject

Returns the value of attribute cell_index_x.



3
4
5
# File 'lib/a_maze_ing/cell.rb', line 3

def cell_index_x
  @cell_index_x
end

#cell_index_yObject

Returns the value of attribute cell_index_y.



3
4
5
# File 'lib/a_maze_ing/cell.rb', line 3

def cell_index_y
  @cell_index_y
end

#is_currentObject

Returns the value of attribute is_current.



3
4
5
# File 'lib/a_maze_ing/cell.rb', line 3

def is_current
  @is_current
end

#neigh_borsObject

Returns the value of attribute neigh_bors.



3
4
5
# File 'lib/a_maze_ing/cell.rb', line 3

def neigh_bors
  @neigh_bors
end

#visitedObject

Returns the value of attribute visited.



3
4
5
# File 'lib/a_maze_ing/cell.rb', line 3

def visited
  @visited
end

#wallsObject

Returns the value of attribute walls.



3
4
5
# File 'lib/a_maze_ing/cell.rb', line 3

def walls
  @walls
end

Instance Method Details

#cell_index(i, j, cells_length) ⇒ Object

Get cell_index by cell_index_x and cell_index_y if the given indexes is invalid (outside the maze) it will return cells_length, which will cause the returned cell (neighbor) to be equal to nil



21
22
23
24
25
26
27
# File 'lib/a_maze_ing/cell.rb', line 21

def cell_index(i, j, cells_length)
  if i < 0 || j < 0 || i > $cols-1 || j > $rows-1
    return cells_length # cause the cell to be (neighbor) equal to nil
  else
    return i + j * $cols
  end
end

#draw(cell_size, color) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/a_maze_ing/cell.rb', line 70

def draw(cell_size, color)
  x = @cell_index_x * cell_size
  y = @cell_index_y * cell_size

  if @walls[0] 
    draw_line x,             y,             color,
              x + cell_size, y,             color
  end

  if @walls[1] 
    draw_line x + cell_size, y,             color,
              x + cell_size, y + cell_size, color
  end

  if @walls[2] 
    draw_line x + cell_size, y + cell_size, color,
              x,             y + cell_size, color
  end

  if @walls[3] 
    draw_line x,             y + cell_size, color,
              x,             y,             color
  end
end

#get_random_neighbor(cells) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/a_maze_ing/cell.rb', line 29

def get_random_neighbor(cells)
  @neigh_bors = Array.new

  top    = cells[cell_index(cell_index_x,     cell_index_y - 1, cells.length)]
  right  = cells[cell_index(cell_index_x + 1, cell_index_y,     cells.length)]
  bottom = cells[cell_index(cell_index_x,     cell_index_y + 1, cells.length)]
  left   = cells[cell_index(cell_index_x - 1, cell_index_y,     cells.length)]

  if top
    if !top.visited
      @neigh_bors.push(top);
    end
  end

  if right
    if !right.visited
      @neigh_bors.push(right);
    end
  end

  if bottom
    if !bottom.visited
      @neigh_bors.push(bottom);
    end
  end

  if left
    if !left.visited
      @neigh_bors.push(left);
    end
  end

  if @neigh_bors.length > 0
    max = @neigh_bors.length - 1
    random_index = rand(0..max)
    return @neigh_bors[random_index]
  else 
    return nil
  end
end