Class: Prisoner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(switchbox, prisoner_count, leader = false) ⇒ Prisoner



3
4
5
6
7
8
9
10
# File 'lib/prisoner.rb', line 3

def initialize switchbox, prisoner_count, leader = false
  @have_flipped = false
  @switchbox = switchbox
  @leader = leader
  @warden = nil
  @prisoner_count = prisoner_count
  @count = 0
end

Instance Attribute Details

#have_flippedObject

Returns the value of attribute have_flipped.



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

def have_flipped
  @have_flipped
end

#leaderObject

Returns the value of attribute leader.



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

def leader
  @leader
end

#wardenObject

Returns the value of attribute warden.



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

def warden
  @warden
end

Instance Method Details

#declare_finished(warden) ⇒ Object



38
39
40
# File 'lib/prisoner.rb', line 38

def declare_finished warden
  warden.finish
end

#visitObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/prisoner.rb', line 12

def visit
  if @leader
    if @count == @prisoner_count + 1
      puts "I am the leader of this prison gang, and I say we are free."
      self.declare_finished(@warden)
    elsif @switchbox.left == :OFF
      @count += 1
      @switchbox.left_flip
      puts "Alright, one more prisoner has been here. That's #{@count} so far."
    else
      @switchbox.right_flip
    end


  else
    if @switchbox.left == :ON and @have_flipped == false
      @switchbox.left_flip
      have_flipped == true
      puts "New room!"
    else
      @switchbox.right_flip
      puts "Oh. This room again."        
    end
  end
end