Class: Game

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

Instance Method Summary collapse

Constructor Details

#initialize(maze) ⇒ Game

Returns a new instance of Game.



4
5
6
# File 'lib/game/game.rb', line 4

def initialize(maze)
  @maze = maze
end

Instance Method Details

#generate_goal(dificulty) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/game/game.rb', line 31

def generate_goal(dificulty)
  selected_cell = select_goal_cell dificulty
  indices = @maze.coords_to_indices *selected_cell.coords
       params = indices.clone
       params.push 2
  @maze.set_raw_value *params
  @maze
end

#select_goal_cell(dificulty) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/game/game.rb', line 8

def select_goal_cell(dificulty)
  dificulty = dificulty.to_sym
  solver = TreeSolver.new @maze
  start_coords = [0]*@maze.dimensions.length
  paths = solver.paths *start_coords
  leafs = {}
  paths.each_leaf do |leaf|
    leafs[leaf.node_depth] = leaf
  end
  depths = leafs.keys.sort
  case dificulty
  when :easy
    selected_depth = depths[(depths.size/4).ceil]
  when :medium
    selected_depth = depths[(depths.size/2).ceil]
  when :hard
    selected_depth = depths.last
  else
    selected_depth = depths[(depths.size/2.0).floor + rand((depths.size/2.0).round)]
  end
  leafs[selected_depth].content
end