Class: Hanoi::Jane::RegularTowers
- Inherits:
-
Object
- Object
- Hanoi::Jane::RegularTowers
- Extended by:
- StackFinder
- Defined in:
- lib/hanoi/jane/towers/regular_towers.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#disc ⇒ Object
readonly
Returns the value of attribute disc.
-
#discs ⇒ Object
Returns the value of attribute discs.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#stacks ⇒ Object
readonly
Returns the value of attribute stacks.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Class Method Summary collapse
- .diff(this, that) ⇒ Object
- .find_disc(stacks, disc) ⇒ Object
- .rebase(value, base, width) ⇒ Object
- .starter_stacks(discs) ⇒ Object
Instance Method Summary collapse
- #binary ⇒ Object
- #each {|_self| ... } ⇒ Object
-
#initialize(discs = 3) {|_self| ... } ⇒ RegularTowers
constructor
A new instance of RegularTowers.
- #inspect ⇒ Object
- #move ⇒ Object
- #rebased ⇒ Object
- #solved ⇒ Object
- #to_s ⇒ Object
Methods included from StackFinder
Constructor Details
#initialize(discs = 3) {|_self| ... } ⇒ RegularTowers
Returns a new instance of RegularTowers.
9 10 11 12 13 14 15 16 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 9 def initialize discs = 3 @discs = discs @total = 0 @base = 2 @stacks = self.class.starter_stacks @discs yield self if block_given? end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
6 7 8 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 6 def base @base end |
#disc ⇒ Object (readonly)
Returns the value of attribute disc.
6 7 8 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 6 def disc @disc end |
#discs ⇒ Object
Returns the value of attribute discs.
7 8 9 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 7 def discs @discs end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
6 7 8 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 6 def from @from end |
#stacks ⇒ Object (readonly)
Returns the value of attribute stacks.
6 7 8 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 6 def stacks @stacks end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
6 7 8 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 6 def to @to end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
6 7 8 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 6 def total @total end |
Class Method Details
.diff(this, that) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 83 def RegularTowers.diff this, that this.chars.reverse.each_with_index do |bit, index| if bit < that.chars.reverse[index] return index end end end |
.find_disc(stacks, disc) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 95 def RegularTowers.find_disc stacks, disc stacks.each_with_index do |stack, index| return index if stack.index disc end raise SearchException.new '%s not found in stacks' % disc end |
.rebase(value, base, width) ⇒ Object
91 92 93 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 91 def RegularTowers.rebase value, base, width '%0*d' % [width, value.to_s(base)] end |
.starter_stacks(discs) ⇒ Object
79 80 81 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 79 def RegularTowers.starter_stacks discs [(0...discs).to_a.reverse, [], []] end |
Instance Method Details
#binary ⇒ Object
71 72 73 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 71 def binary rebased end |
#each {|_self| ... } ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 52 def each yield self if @total == 0 until solved move yield self end end |
#inspect ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 39 def inspect { stacks: @stacks, moves: @total, binary: rebased, moved: { disc: @disc, from: @from, to: @to } } end |
#move ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 23 def move before = binary @total += 1 after = binary @disc = self.class.diff before, after @from = self.class.find_disc @stacks, @disc @to = self.class.find_stack stacks: @stacks, from: @from, disc: @disc, total: @total @stacks[@to].push @stacks[@from].pop end |
#rebased ⇒ Object
75 76 77 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 75 def rebased self.class.rebase @total, @base, @discs end |
#solved ⇒ Object
35 36 37 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 35 def solved rebased.chars.all? { |digit| digit.to_i == @base - 1 } end |
#to_s ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 60 def to_s s = '' @stacks.each do |stack| s += stack.to_s s += "\n" end s += '---' s end |