Sha256: dfd660cb377d285b1d56aea5e7745e970e2e4046d0dde87e5167da30de89c325

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

module Gobstones

  module Runner

    class Cell

      def initialize
        @values = { Azul => 0, Negro => 0, Rojo => 0, Verde => 0 }
      end

      def put(color)
        check color
        lookup(color) { |value| value + 1 }
      end

      def take_out(color)
        check color
        raise EmptyCellError unless are_there_balls?(color)
        lookup(color) { |value| value - 1 }
      end

      def are_there_balls?(color)
        check color
        number_of_balls(color) > 0
      end

      def number_of_balls(color)
        check color
        lookup color
      end

      def empty!
        initialize
      end

      def empty?
        @values.values.all? { |value| value.zero? }
      end

      private

      def check(color)
        raise "'#{color}' is not a color" \
        unless [Azul, Negro, Rojo, Verde].include? color.class
      end

      def lookup(color)
        value = @values.keys.detect { |col| col == color.class }
        if block_given?
          @values[value] = yield @values[value]
        else
          @values[value]
        end
      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gobstones-0.0.1.1 lib/gobstones/runner/cell.rb