Class: GlimmerKlondikeSolitaire::Model::ColumnPile

Inherits:
Object
  • Object
show all
Includes:
Glimmer::DataBinding::ObservableModel
Defined in:
app/glimmer_klondike_solitaire/model/column_pile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#countObject (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!(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_cardsObject



51
52
53
# File 'app/glimmer_klondike_solitaire/model/column_pile.rb', line 51

def playing_cards
  @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!(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) ⇒ Object



39
40
41
42
43
# 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) ⇒ 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 = playing_cards[index...playing_cards.size]
  playing_cards[index...playing_cards.size] = []
  removed_cards
end

#reset!Object



13
14
15
16
17
# 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_sObject



55
56
57
# 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