Class: Prisoner
- Inherits:
-
Object
- Object
- Prisoner
- Defined in:
- lib/prisoner.rb
Instance Attribute Summary collapse
-
#fool ⇒ Object
Returns the value of attribute fool.
-
#have_flipped ⇒ Object
Returns the value of attribute have_flipped.
-
#id ⇒ Object
Returns the value of attribute id.
-
#leader ⇒ Object
Returns the value of attribute leader.
-
#warden ⇒ Object
writeonly
Sets the attribute warden.
Instance Method Summary collapse
- #declare_finished(warden) ⇒ Object
-
#initialize(switchbox = nil, prisoner_count = 0, leader = false, fool = false, foolishness = 0) ⇒ Prisoner
constructor
A new instance of Prisoner.
- #visit ⇒ Object
Constructor Details
#initialize(switchbox = nil, prisoner_count = 0, leader = false, fool = false, foolishness = 0) ⇒ Prisoner
Returns a new instance of Prisoner.
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/prisoner.rb', line 4 def initialize switchbox=nil, prisoner_count=0, leader = false, fool = false, foolishness = 0 @have_flipped = false @switchbox = switchbox @leader = leader @warden = nil @prisoner_count = prisoner_count @count = 0 @fool = fool @foolishness = foolishness @id = nil end |
Instance Attribute Details
#fool ⇒ Object
Returns the value of attribute fool.
3 4 5 |
# File 'lib/prisoner.rb', line 3 def fool @fool end |
#have_flipped ⇒ Object
Returns the value of attribute have_flipped.
3 4 5 |
# File 'lib/prisoner.rb', line 3 def have_flipped @have_flipped end |
#id ⇒ Object
Returns the value of attribute id.
3 4 5 |
# File 'lib/prisoner.rb', line 3 def id @id end |
#leader ⇒ Object
Returns the value of attribute leader.
3 4 5 |
# File 'lib/prisoner.rb', line 3 def leader @leader end |
#warden=(value) ⇒ Object (writeonly)
Sets the attribute warden
2 3 4 |
# File 'lib/prisoner.rb', line 2 def warden=(value) @warden = value end |
Instance Method Details
#declare_finished(warden) ⇒ Object
48 49 50 51 |
# File 'lib/prisoner.rb', line 48 def declare_finished warden warden.finish @count = 0 end |
#visit ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/prisoner.rb', line 16 def visit if @leader if @fool and rand(100)+1 < @foolishness @count += 1 end 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 print "." end if @fool and rand(100)+1 < @foolishness have_flipped = false end end end |