Class: Game
- Inherits:
-
Object
- Object
- Game
- Defined in:
- lib/game/game.rb
Instance Method Summary collapse
- #generate_goal(dificulty) ⇒ Object
-
#initialize(maze) ⇒ Game
constructor
A new instance of Game.
- #select_goal_cell(dificulty) ⇒ Object
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 |
# File 'lib/game/game.rb', line 31 def generate_goal(dificulty) selected_cell = select_goal_cell(dificulty) i, j = @maze.xy_to_ij(selected_cell.x, selected_cell.y) @maze.matrix[i][j] = 2 @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_x, start_y = 0, 0 paths = solver.paths(start_x, start_y) 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*0.25).ceil] when :medium selected_depth = depths[(depths.size*0.5).ceil] when :hard selected_depth = depths.last else selected_depth = depths[rand((depths.size/2.0).ceil) + (depths.size/2.0).ceil] end leafs[selected_depth].content end |