Class: Prisoner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#foolObject

Returns the value of attribute fool.



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

def fool
  @fool
end

#have_flippedObject

Returns the value of attribute have_flipped.



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

def have_flipped
  @have_flipped
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#leaderObject

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

Parameters:

  • value

    the value to set the attribute warden to.



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

#visitObject



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