Class: World

Inherits:
Object
  • Object
show all
Defined in:
lib/zombie-chaser/world.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interface_type) ⇒ World

Returns a new instance of World.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/zombie-chaser/world.rb', line 30

def initialize(interface_type)
  @human = nil
  @zombie_list = nil
  @interface = case interface_type
    when :console_interface then ConsoleInterface.new
    when :no_interface then NoInterface.new
    when :gui_interface then GuiInterface.new
  end
  @view_update_threads = nil
  @collision_detection_lock = Monitor.new
end

Instance Attribute Details

#interfaceObject (readonly)

Returns the value of attribute interface.



10
11
12
# File 'lib/zombie-chaser/world.rb', line 10

def interface
  @interface
end

Class Method Details

.interface_typeObject



8
# File 'lib/zombie-chaser/world.rb', line 8

def self.interface_type; @interface_type end

.interface_type=(interface_type) ⇒ Object



7
# File 'lib/zombie-chaser/world.rb', line 7

def self.interface_type=(interface_type); @interface_type = interface_type end

.new_using_results(human_results, zombies_results) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/zombie-chaser/world.rb', line 12

def self.new_using_results(human_results, zombies_results)
  world = new(:no_interface)
  human = MockHuman.new_using_results(human_results, world)
  zombie_list = MockZombieList.new_using_results(zombies_results, world)
  world.set_human(human)
  world.set_zombie_list(zombie_list)
  world
end

.new_using_test_unit_handler(test_pattern) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/zombie-chaser/world.rb', line 21

def self.new_using_test_unit_handler(test_pattern)
  world = new(self.interface_type)
  human = Human.new_using_test_unit_handler(test_pattern, world)
  zombie_list = ZombieList.new_using_test_unit_handler(test_pattern, world)
  world.set_human(human)
  world.set_zombie_list(zombie_list)
  world
end

Instance Method Details

#human_dead?Boolean

Assumption: this is called after human is in a valid state

Returns:

  • (Boolean)


97
98
99
# File 'lib/zombie-chaser/world.rb', line 97

def human_dead?
  @human.dead?
end

#no_living_zombies_apart_from_me?(desired_step_count, actor) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/zombie-chaser/world.rb', line 101

def no_living_zombies_apart_from_me?(desired_step_count, actor)
  @interface.no_living_zombies_apart_from_me?(desired_step_count, actor)
end

#notify_human_eatenObject



92
93
94
# File 'lib/zombie-chaser/world.rb', line 92

def notify_human_eaten
  @human.get_eaten
end

#run_humanObject



66
67
68
69
# File 'lib/zombie-chaser/world.rb', line 66

def run_human
  @human.run
  ! @human.dead?
end

#run_next_zombieObject



71
72
73
74
75
76
77
78
# File 'lib/zombie-chaser/world.rb', line 71

def run_next_zombie
  # Since zombies can come from multiple directions, rather than queueing against each other,
  # separating out when they appear isn't required
  zombie = @zombie_list.supply_next_zombie
  @view_update_threads.enq(Thread.new{zombie.build_view_queue})
  @view_update_threads.enq(Thread.new{zombie.update_view})
  zombie.run_tests
end

#set_human(human) ⇒ Object



42
43
44
45
46
# File 'lib/zombie-chaser/world.rb', line 42

def set_human(human)
  raise "Already set" unless @human.nil?
  @human = human
  interface.human = human
end

#set_zombie_list(zombie_list) ⇒ Object



48
49
50
51
52
# File 'lib/zombie-chaser/world.rb', line 48

def set_zombie_list(zombie_list)
  raise "Already set" unless @zombie_list.nil?
  @zombie_list = zombie_list
  @interface.zombie_list = zombie_list
end

#sleep(duration) ⇒ Object



88
89
90
# File 'lib/zombie-chaser/world.rb', line 88

def sleep(duration)
  @interface.sleep(duration)
end

#something_happenedObject



84
85
86
# File 'lib/zombie-chaser/world.rb', line 84

def something_happened
  @interface.something_happened
end

#synchronize_for_collision_detectionObject



80
81
82
# File 'lib/zombie-chaser/world.rb', line 80

def synchronize_for_collision_detection
  @collision_detection_lock.synchronize {yield}
end

#while_world_runningObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/zombie-chaser/world.rb', line 54

def while_world_running
  @view_update_threads = Queue.new
  yield
  @view_update_threads.enq(:end_of_work)
  thread = @view_update_threads.deq
  until thread == :end_of_work
    thread.join
    thread = @view_update_threads.deq
  end
  @interface.finish_if_neccessary
end