Class: Rubots::Game

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

Constant Summary collapse

MAP_HEIGHT =
700
MAP_WIDTH =
1000
CELL_SIZE =

Positioning cell

40

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(robots) ⇒ Game

Returns a new instance of Game.



8
9
10
# File 'lib/rubots/game.rb', line 8

def initialize(robots)
  @robots = robots.map { |klass| Robot.new(klass, self, *random_location) }
end

Instance Attribute Details

#laser_beamsObject (readonly)

Returns the value of attribute laser_beams.



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

def laser_beams
  @laser_beams
end

#robotsObject (readonly)

Returns the value of attribute robots.



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

def robots
  @robots
end

Instance Method Details

#laser_fire(beam) ⇒ Object



26
27
28
# File 'lib/rubots/game.rb', line 26

def laser_fire(beam)
  @laser_beams << beam
end

#mapObject



22
23
24
# File 'lib/rubots/game.rb', line 22

def map
  OpenStruct.new width: MAP_WIDTH, height: MAP_HEIGHT
end

#over?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/rubots/game.rb', line 30

def over?
  @robots.count <= 1
end

#tickObject



12
13
14
15
16
17
18
19
20
# File 'lib/rubots/game.rb', line 12

def tick
  @laser_beams = []
  @robots.each { |robot| robot.process_command }
  @robots.each { |robot| robot.tick            }
  check_collisions
  check_out_of_area
  check_beam_hits
  clean_up_bodies
end

#winnerObject



34
35
36
37
38
# File 'lib/rubots/game.rb', line 34

def winner
  return nil unless over?

  @robots.first || OpenStruct.new(name: "Nobody")
end