Class: LineFolder
- Inherits:
-
Object
- Object
- LineFolder
- Defined in:
- lib/threesmodel/line_folder.rb
Overview
The LineFolder can fold an array with four cells according to the rules of threes.
Instance Method Summary collapse
- #can_fold?(line) ⇒ Boolean
- #fold(line) ⇒ Object
-
#initialize ⇒ LineFolder
constructor
A new instance of LineFolder.
Constructor Details
#initialize ⇒ LineFolder
Returns a new instance of LineFolder.
5 6 7 8 9 10 11 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 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/threesmodel/line_folder.rb', line 5 def initialize Array.class_eval do # Determines the "wall" position and the collapses the first to collapsible cells inserting a zero at the end. Assumes a foldable line. def squash array = Array.new(self) wall_index = wall_index_of array[wall_index] = array[wall_index] + array[wall_index + 1] while wall_index < 2 array[wall_index + 1] = array[wall_index + 2] wall_index += 1 end array[3] = 0 return array end def wall_index_of return 3 if self == ([1,1,1,1] or [2,2,2,2]) return 3 if self[1..3] == [0,0,0] return 0 if self[0] == 0 return 0 if self[0] == 1 and self[1] == 2 return 0 if self[0] == 2 and self[1] == 1 if self[0] > 2 return 0 if self[0] == self[1] end return 3 if self[2..3] == [0,0] return 1 if self[1] == 0 return 1 if (self[0] != self[1]) and self[1] == 1 and self[2] == 2 return 1 if (self[0] != self[1]) and self[1] == 2 and self[2] == 1 return 1 if (self[0] == self[1]) and self[1] == 1 and self[2] == 2 return 1 if (self[0] == self[1]) and self[1] == 2 and self[2] == 1 if self[1] > 2 return 1 if self[1] == self[2] end return 2 if self[2] == 0 return 2 if self[2] == 1 and self[3] == 2 return 2 if self[2] == 2 and self[3] == 1 if self[2] > 2 return 2 if self[2] == self[3] end 3 end end end |
Instance Method Details
#can_fold?(line) ⇒ Boolean
58 59 60 |
# File 'lib/threesmodel/line_folder.rb', line 58 def can_fold?(line) return line.wall_index_of < 3 end |
#fold(line) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/threesmodel/line_folder.rb', line 50 def fold(line) if can_fold?(line) then return line.squash else return line end end |