Sha256: 5bd809684f4d5f014867a3847219ef6402929966888a8cdcf4505ebab0e39a69

Contents?: true

Size: 1.83 KB

Versions: 32

Compression:

Stored size: 1.83 KB

Contents

class KlondikeSolitaire
  module Model
    class ColumnPile
      include Glimmer::DataBinding::ObservableModel
      attr_reader :count
    
      def initialize(game, count)
        @game = game
        @count = count
        reset!
      end
      
      def reset!
        playing_cards.clear
        populate!(count.times.map { @game.deck.pop })
        notify_observers(:playing_cards)
      end
    
      # this method does not validate
      def populate!(new_playing_cards)
        new_playing_cards.each_with_index do |playing_card, index|
          playing_card.hidden = true if index < (new_playing_cards.size - 1)
          playing_cards.push(playing_card)
        end
      end
      
      # this method validates that playing card fits at the bottom of the column (opposite color and one number smaller)
      # throws an error if it does not fit
      def add!(new_playing_card)
        bottom_card = playing_cards.last
        if (playing_cards.empty? && new_playing_card.rank == 13) ||
            (new_playing_card.color != bottom_card.color && new_playing_card.rank == (bottom_card.rank - 1))
          playing_cards.push(new_playing_card)
        else
          raise "Cannot add #{new_playing_card} to #{self}"
        end
      end
      
      def remove!(card)
        remove_cards_starting_at(playing_cards.index(card)).tap do |result|
          playing_cards.last&.hidden = false
        end
      end
      
      def remove_cards_starting_at(index)
        removed_cards = playing_cards[index...playing_cards.size]
        playing_cards[index...playing_cards.size] = []
        removed_cards
      end
      
      def playing_cards
        @playing_cards ||= []
      end
      
      def to_s
        "Column Pile #{count} (#{playing_cards.map {|card| "#{card.rank}#{card.suit.to_s[0].upcase}"}.join(" | ")})"
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
glimmer-dsl-swt-4.22.0.0 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.20.16.0 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.21.2.5 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.21.2.4 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.21.2.3 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.21.2.2 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.21.2.1 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.21.2.0 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.21.1.1 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.21.1.0 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.21.0.1 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.21.0.0 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.20.15.5 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.20.15.4 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.20.15.3 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.20.15.2 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.20.15.1 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.20.15.0 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.20.14.2 samples/elaborate/klondike_solitaire/model/column_pile.rb
glimmer-dsl-swt-4.20.14.1 samples/elaborate/klondike_solitaire/model/column_pile.rb