Class: Battlefield

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, timeout, seed) ⇒ Battlefield

Returns a new instance of Battlefield.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rrobots/battlefield.rb', line 13

def initialize width, height, timeout, seed
  @width, @height = width, height
  @seed = seed
  @time = 0
  @robots = []
  @teams = Hash.new{|h,k| h[k] = [] }
  @bullets = []
  @explosions = []
  @timeout = timeout
  @game_over = false
  srand @seed
end

Instance Attribute Details

#bulletsObject (readonly)

Returns the value of attribute bullets.



6
7
8
# File 'lib/rrobots/battlefield.rb', line 6

def bullets
  @bullets
end

#explosionsObject (readonly)

Returns the value of attribute explosions.



7
8
9
# File 'lib/rrobots/battlefield.rb', line 7

def explosions
  @explosions
end

#game_overObject (readonly)

Returns the value of attribute game_over.



11
12
13
# File 'lib/rrobots/battlefield.rb', line 11

def game_over
  @game_over
end

#heightObject (readonly)

Returns the value of attribute height.



3
4
5
# File 'lib/rrobots/battlefield.rb', line 3

def height
  @height
end

#robotsObject (readonly)

Returns the value of attribute robots.



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

def robots
  @robots
end

#seedObject (readonly)

Returns the value of attribute seed.



9
10
11
# File 'lib/rrobots/battlefield.rb', line 9

def seed
  @seed
end

#teamsObject (readonly)

Returns the value of attribute teams.



5
6
7
# File 'lib/rrobots/battlefield.rb', line 5

def teams
  @teams
end

#timeObject (readonly)

Returns the value of attribute time.



8
9
10
# File 'lib/rrobots/battlefield.rb', line 8

def time
  @time
end

#timeoutObject (readonly)

how many ticks the match can go before ending.



10
11
12
# File 'lib/rrobots/battlefield.rb', line 10

def timeout
  @timeout
end

#widthObject (readonly)

Returns the value of attribute width.



2
3
4
# File 'lib/rrobots/battlefield.rb', line 2

def width
  @width
end

Instance Method Details

#<<(object) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rrobots/battlefield.rb', line 26

def << object
  case object
  when RobotRunner
    @robots << object
    @teams[object.team] << object
  when Bullet
    @bullets << object
  when Explosion
    @explosions << object
  end
end

#stateObject



63
64
65
66
67
# File 'lib/rrobots/battlefield.rb', line 63

def state
  {:explosions => explosions.map{|e| e.state},
   :bullets    => bullets.map{|b| b.state},
   :robots     => robots.map{|r| r.state}}
end

#tickObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rrobots/battlefield.rb', line 38

def tick
  explosions.delete_if{|explosion| explosion.dead}
  explosions.each{|explosion| explosion.tick}

  bullets.delete_if{|bullet| bullet.dead}
  bullets.each{|bullet| bullet.tick}

  robots.each do |robot|
    begin
      robot.send :internal_tick unless robot.dead
    rescue Exception => bang
      puts "#{robot} made an exception:"
      puts "#{bang.class}: #{bang}", bang.backtrace
      robot.instance_eval{@energy = -1}
    end
  end

  @time += 1
  live_robots = robots.find_all{|robot| !robot.dead}
  @game_over = (  (@time >= timeout) or # timeout reached
                  (live_robots.length == 0) or # no robots alive, draw game
                  (live_robots.all?{|r| r.team == live_robots.first.team})) # all other teams are dead
  not @game_over
end