Class: ConsoleInterface

Inherits:
Interface show all
Defined in:
lib/interface.rb

Direct Known Subclasses

NoInterface

Instance Attribute Summary

Attributes inherited from Interface

#current_zombie, #human

Instance Method Summary collapse

Constructor Details

#initializeConsoleInterface

Returns a new instance of ConsoleInterface.



7
8
9
10
# File 'lib/interface.rb', line 7

def initialize
  @representations = []
  @current_zombie = nil
end

Instance Method Details

#adjust_for_screen_width(step_count) ⇒ Object



45
46
47
48
# File 'lib/interface.rb', line 45

def adjust_for_screen_width(step_count)
  max_position = 78.0
  (step_count * max_position / [@human.test_suite_size, max_position].max).round
end

#current_representationObject



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

def current_representation
  if @current_zombie.nil?
    "." * human_position + @human.current_symbol
  elsif human_position > zombie_position
    "." * zombie_position + @current_zombie.current_symbol + "." * (human_position - zombie_position - 1) + @human.current_symbol
  else
    "." * zombie_position + @current_zombie.current_symbol
  end
end

#display_representation(representation) ⇒ Object



27
28
29
30
31
# File 'lib/interface.rb', line 27

def display_representation(representation)
  print "\r", representation
  STDOUT.flush
  sleep 0.2
end

#finishObject



33
34
35
# File 'lib/interface.rb', line 33

def finish
  puts
end

#human_positionObject



37
38
39
# File 'lib/interface.rb', line 37

def human_position
  adjust_for_screen_width(@human.successful_step_count)
end

#something_happenedObject



22
23
24
25
# File 'lib/interface.rb', line 22

def something_happened
  @representations << current_representation
  display_representation(@representations.last)
end

#zombie_positionObject



41
42
43
# File 'lib/interface.rb', line 41

def zombie_position
  adjust_for_screen_width(@current_zombie.successful_step_count)
end