Sha256: c3a9e75b25802fefcda3476f53e1ddcefdc897bfcc3e7ae25611580cc295ec34

Contents?: true

Size: 788 Bytes

Versions: 2

Compression:

Stored size: 788 Bytes

Contents

module TowersOfHanoi
  class Board
    class Tower
      def initialize(max_bricks: 3, bricks: 0)
        @max_bricks = max_bricks
        @bricks = Array.new(bricks).map.with_index do |el, idx|
          TowersOfHanoi::Board::Brick.new(width: max_bricks - idx)
        end
      end

      def height
        @bricks.length
      end

      def top
        brick(height) || TowersOfHanoi::Board::Base.new(width: @max_bricks + 1)
      end

      def full?
        height == @max_bricks
      end

      def brick(position)
        if (1..@max_bricks) === position
          @bricks[position - 1] || TowersOfHanoi::Board::Brick.new
        end
      end

      def add(new_brick)
        @bricks.push(new_brick)
      end

      def remove
        @bricks.pop
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
towers_of_hanoi-0.2.0 lib/towers_of_hanoi/board/tower.rb
towers_of_hanoi-0.1.0 lib/towers_of_hanoi/board/tower.rb