Class: GamesAndRpgParadise::MazePuzzle::Maze

Inherits:
Object
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/maze.rb

Instance Method Summary collapse

Instance Method Details

#check_dimensionObject

check_dimension

This is very important.

With this calculated dimension the character will be exactly at the pixel you want it to be



24
25
26
27
28
# File 'lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/maze.rb', line 24

def check_dimension
  until $dimension/$cols % STEP_RATE == 0
    $dimension -= 1
  end
end

#generate_mazeObject



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
69
70
71
72
73
74
75
76
# File 'lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/maze.rb', line 30

def generate_maze

  # set initial specifications for the maze
  $dimension = DIMENSION
  check_dimension
  $cell_size = $dimension/$cols
  $speed_per_tick = $cell_size/STEP_RATE
  $player_size = 0.5 * $cell_size

  $cells = Array.new
  @stack = Array.new
  
  # I use one dimensional array to store all the cell... 
  # yeah, should not do that... feel regret now
  $rows.times do |row_index|
    $cols.times { |col_index|
      cell = Cell.new(col_index, row_index)
      $cells.push(cell)
    }
  end

  # Ok, here is where the heavy work begin
  # Let set visited for the top left cell, where we start
  $cells[0].visited = true
  @current_cell = $cells[0]
  # @current_cell.is_current = true
  @stack.push(@current_cell)

  # This is the same as "While there are unvisited cells"
  # at least, same result, see the wiki page for more
  while @stack.length > 0 do 
    next_cell = @current_cell.get_random_neighbor($cells)
    if next_cell
      remove_walls(@current_cell, next_cell)
      next_cell.visited = true
      @stack.push(@current_cell)
      # @current_cell.is_current = true
      @current_cell = next_cell
    elsif @stack.length > 0
      # @stack[-1].is_current = false
      @current_cell = @stack.pop
    end
  end

  # Remove more random walls to make more available way
  remove_extra_walls 
end

#remove_extra_wallsObject

remove_extra_walls

For now, it almost doesn’t remove horizontal walls



108
109
110
111
112
113
114
115
# File 'lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/maze.rb', line 108

def remove_extra_walls
  a_count_number = $cells.length/2/5
  while a_count_number > 0 do
    cell_index = rand(0..$cells.length/2)
    remove_walls($cells[cell_index], $cells[cell_index+1])
    a_count_number -= 1
  end
end

#remove_walls(current_cell, next_cell) ⇒ Object

remove_walls



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/games_and_rpg_paradise/gui/gosu/maze_puzzle/maze.rb', line 79

def remove_walls(current_cell, next_cell)
  if next_cell != nil

    # yeah I'd like to call it magic_number
    # it show me the relative position of two cell
    magic_number =  next_cell.cell_index_x - current_cell.cell_index_x
    
    if magic_number == 1 # next cell is on the right
      current_cell.walls[1] = next_cell.walls[3] = false

    elsif magic_number == -1 # next cell is on the left
      current_cell.walls[3] = next_cell.walls[1] = false

    elsif magic_number == 0 # next cell is either on top or bottom
      magic_number = next_cell.cell_index_y - current_cell.cell_index_y

      if magic_number == 1 #next cell is bottom
        current_cell.walls[2] = next_cell.walls[0] = false

      elsif magic_number ==-1#next cell is on top
        current_cell.walls[0] = next_cell.walls[2] = false
      end
    end
  end
end