class GlimmerKlondikeSolitaire::Model::ColumnPile
Attributes
count[R]
Public Class Methods
new(game, count)
click to toggle source
# File app/glimmer_klondike_solitaire/model/column_pile.rb, line 7 def initialize(game, count) @game = game @count = count reset! end
Public Instance Methods
add!(new_playing_card)
click to toggle source
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
# File app/glimmer_klondike_solitaire/model/column_pile.rb, line 29 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
playing_cards()
click to toggle source
# File app/glimmer_klondike_solitaire/model/column_pile.rb, line 51 def playing_cards @playing_cards ||= [] end
populate!(new_playing_cards)
click to toggle source
this method does not validate
# File app/glimmer_klondike_solitaire/model/column_pile.rb, line 20 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
remove!(card)
click to toggle source
# File app/glimmer_klondike_solitaire/model/column_pile.rb, line 39 def remove!(card) remove_cards_starting_at(playing_cards.index(card)).tap do |result| playing_cards.last&.hidden = false end end
remove_cards_starting_at(index)
click to toggle source
# File app/glimmer_klondike_solitaire/model/column_pile.rb, line 45 def remove_cards_starting_at(index) removed_cards = playing_cards[index...playing_cards.size] playing_cards[index...playing_cards.size] = [] removed_cards end
reset!()
click to toggle source
# File app/glimmer_klondike_solitaire/model/column_pile.rb, line 13 def reset! playing_cards.clear populate!(count.times.map { @game.deck.pop }) notify_observers(:playing_cards) end
to_s()
click to toggle source
# File app/glimmer_klondike_solitaire/model/column_pile.rb, line 55 def to_s "Column Pile #{count} (#{playing_cards.map {|card| "#{card.rank}#{card.suit.to_s[0].upcase}"}.join(" | ")})" end