Class: Hanoi::Jane::RegularTowers

Inherits:
Object
  • Object
show all
Extended by:
StackFinder
Defined in:
lib/hanoi/jane/towers/regular_towers.rb

Direct Known Subclasses

ConstrainedTowers

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StackFinder

find_stack

Constructor Details

#initialize(discs = 3) {|_self| ... } ⇒ RegularTowers

Returns a new instance of RegularTowers.

Yields:

  • (_self)

Yield Parameters:



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

#baseObject (readonly)

Returns the value of attribute base.



6
7
8
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 6

def base
  @base
end

#discObject (readonly)

Returns the value of attribute disc.



6
7
8
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 6

def disc
  @disc
end

#discsObject

Returns the value of attribute discs.



7
8
9
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 7

def discs
  @discs
end

#fromObject (readonly)

Returns the value of attribute from.



6
7
8
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 6

def from
  @from
end

#stacksObject (readonly)

Returns the value of attribute stacks.



6
7
8
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 6

def stacks
  @stacks
end

#toObject (readonly)

Returns the value of attribute to.



6
7
8
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 6

def to
  @to
end

#totalObject (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

#binaryObject



71
72
73
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 71

def binary
  rebased
end

#each {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



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

#inspectObject



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

#moveObject



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

#rebasedObject



75
76
77
# File 'lib/hanoi/jane/towers/regular_towers.rb', line 75

def rebased
  self.class.rebase @total, @base, @discs
end

#solvedObject



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_sObject



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