Class: Prisoner
- Inherits:
-
Object
- Object
- Prisoner
- Defined in:
- lib/prisoner.rb
Instance Attribute Summary collapse
-
#have_flipped ⇒ Object
Returns the value of attribute have_flipped.
-
#leader ⇒ Object
Returns the value of attribute leader.
-
#warden ⇒ Object
Returns the value of attribute warden.
Instance Method Summary collapse
- #declare_finished(warden) ⇒ Object
-
#initialize(switchbox, prisoner_count, leader = false) ⇒ Prisoner
constructor
A new instance of Prisoner.
- #visit ⇒ Object
Constructor Details
#initialize(switchbox, prisoner_count, leader = false) ⇒ Prisoner
Returns a new instance of 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_flipped ⇒ Object
Returns the value of attribute have_flipped.
2 3 4 |
# File 'lib/prisoner.rb', line 2 def have_flipped @have_flipped end |
#leader ⇒ Object
Returns the value of attribute leader.
2 3 4 |
# File 'lib/prisoner.rb', line 2 def leader @leader end |
#warden ⇒ Object
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 |
#visit ⇒ Object
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 |