Class: MazePuzzle::Cell
- Inherits:
-
Object
- Object
- MazePuzzle::Cell
- Defined in:
- lib/games_paradise/gui/gosu/maze_puzzle/cell.rb
Instance Attribute Summary collapse
-
#cell_index_x ⇒ Object
Returns the value of attribute cell_index_x.
-
#cell_index_y ⇒ Object
Returns the value of attribute cell_index_y.
-
#is_current ⇒ Object
Returns the value of attribute is_current.
-
#neigh_bors ⇒ Object
Returns the value of attribute neigh_bors.
-
#visited ⇒ Object
Returns the value of attribute visited.
-
#walls ⇒ Object
Returns the value of attribute walls.
Instance Method Summary collapse
-
#cell_index(i, j, cells_length) ⇒ Object
This function will convert two dimensional array index to one dimensional array index 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.
- #draw(cell_size, color) ⇒ Object
- #get_random_neighbor(cells) ⇒ Object
-
#initialize(cell_index_x, cell_index_y) ⇒ Cell
constructor
initialize.
Constructor Details
#initialize(cell_index_x, cell_index_y) ⇒ Cell
initialize
17 18 19 20 21 22 23 24 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/cell.rb', line 17 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_x ⇒ Object
Returns the value of attribute cell_index_x.
9 10 11 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/cell.rb', line 9 def cell_index_x @cell_index_x end |
#cell_index_y ⇒ Object
Returns the value of attribute cell_index_y.
10 11 12 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/cell.rb', line 10 def cell_index_y @cell_index_y end |
#is_current ⇒ Object
Returns the value of attribute is_current.
14 15 16 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/cell.rb', line 14 def is_current @is_current end |
#neigh_bors ⇒ Object
Returns the value of attribute neigh_bors.
13 14 15 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/cell.rb', line 13 def neigh_bors @neigh_bors end |
#visited ⇒ Object
Returns the value of attribute visited.
12 13 14 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/cell.rb', line 12 def visited @visited end |
#walls ⇒ Object
Returns the value of attribute walls.
11 12 13 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/cell.rb', line 11 def walls @walls end |
Instance Method Details
#cell_index(i, j, cells_length) ⇒ Object
This function will convert two dimensional array index to one dimensional array index 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
33 34 35 36 37 38 39 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/cell.rb', line 33 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
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/cell.rb', line 82 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
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 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/games_paradise/gui/gosu/maze_puzzle/cell.rb', line 41 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 |