Class: GlimmerKlondikeSolitaire::Model::ColumnPile
- Inherits:
-
Object
- Object
- GlimmerKlondikeSolitaire::Model::ColumnPile
- Includes:
- Glimmer::DataBinding::ObservableModel
- Defined in:
- app/glimmer_klondike_solitaire/model/column_pile.rb
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
Instance Method Summary collapse
-
#add!(new_playing_card) ⇒ Object
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.
-
#initialize(game, count) ⇒ ColumnPile
constructor
A new instance of ColumnPile.
- #playing_cards ⇒ Object
-
#populate!(new_playing_cards) ⇒ Object
this method does not validate.
- #remove!(card) ⇒ Object
- #remove_cards_starting_at(index) ⇒ Object
- #reset! ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(game, count) ⇒ ColumnPile
Returns a new instance of ColumnPile.
7 8 9 10 11 |
# File 'app/glimmer_klondike_solitaire/model/column_pile.rb', line 7 def initialize(game, count) @game = game @count = count reset! end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
5 6 7 |
# File 'app/glimmer_klondike_solitaire/model/column_pile.rb', line 5 def count @count end |
Instance Method Details
#add!(new_playing_card) ⇒ Object
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
29 30 31 32 33 34 35 36 37 |
# File 'app/glimmer_klondike_solitaire/model/column_pile.rb', line 29 def add!() bottom_card = .last if (.empty? && .rank == 13) || (.color != bottom_card.color && .rank == (bottom_card.rank - 1)) .push() else raise "Cannot add #{} to #{self}" end end |
#playing_cards ⇒ Object
51 52 53 |
# File 'app/glimmer_klondike_solitaire/model/column_pile.rb', line 51 def @playing_cards ||= [] end |
#populate!(new_playing_cards) ⇒ Object
this method does not validate
20 21 22 23 24 25 |
# File 'app/glimmer_klondike_solitaire/model/column_pile.rb', line 20 def populate!() .each_with_index do |, index| .hidden = true if index < (.size - 1) .push() end end |
#remove!(card) ⇒ Object
39 40 41 42 43 |
# File 'app/glimmer_klondike_solitaire/model/column_pile.rb', line 39 def remove!(card) remove_cards_starting_at(.index(card)).tap do |result| .last&.hidden = false end end |
#remove_cards_starting_at(index) ⇒ Object
45 46 47 48 49 |
# File 'app/glimmer_klondike_solitaire/model/column_pile.rb', line 45 def remove_cards_starting_at(index) removed_cards = [index....size] [index....size] = [] removed_cards end |
#reset! ⇒ Object
13 14 15 16 17 |
# File 'app/glimmer_klondike_solitaire/model/column_pile.rb', line 13 def reset! .clear populate!(count.times.map { @game.deck.pop }) notify_observers(:playing_cards) end |
#to_s ⇒ Object
55 56 57 |
# File 'app/glimmer_klondike_solitaire/model/column_pile.rb', line 55 def to_s "Column Pile #{count} (#{.map {|card| "#{card.rank}#{card.suit.to_s[0].upcase}"}.join(" | ")})" end |