Class: LineFolder

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

Overview

The LineFolder can fold an array with four cells according to the rules of threes.

Instance Method Summary collapse

Constructor Details

#initializeLineFolder

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
# File 'lib/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 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 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
    end

  end
end

Instance Method Details

#can_fold?(line) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
# File 'lib/line_folder.rb', line 55

def can_fold?(line)
  foldable = has_adjacent_one_and_two?(line)
  foldable = (foldable or has_adjacent_equals?(line))
  foldable = (foldable or has_non_trailing_zeros?(line))
  return foldable
end

#fold(line) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/line_folder.rb', line 46

def fold(line)
  if can_fold?(line) then
    if(line == [0,0,0,0]) then return line end
    return line.squash
  else
    return line
  end
end

#has_adjacent_equals?(line) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
# File 'lib/line_folder.rb', line 83

def has_adjacent_equals?(line)
  pairs = line.each_cons(2).to_a
  index = pairs.each {|pair|
    if(pair[0] == pair[1] and (pair[0] > 2))
      return true
    end
  }
  false
end

#has_adjacent_one_and_two?(line) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
# File 'lib/line_folder.rb', line 73

def has_adjacent_one_and_two?(line)
  pairs = line.each_cons(2).to_a
  index = pairs.each_index {|i|
    if (pairs[i].sort == [1, 2])
      return true
    end
  }
  false
end

#has_non_trailing_zeros?(line) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
# File 'lib/line_folder.rb', line 62

def has_non_trailing_zeros?(line)
  first_non_zero_from_end = index_of_first_non_trailing_zero(line)
  if (first_non_zero_from_end == nil) then return true end
  if (line.index(0) == nil) then return false end
  return (line.index(0) < first_non_zero_from_end)
end

#index_of_first_non_trailing_zero(line) ⇒ Object



69
70
71
# File 'lib/line_folder.rb', line 69

def index_of_first_non_trailing_zero(line)
  first_non_zero_from_end = line.rindex {|element| element != 0}
end