Class: GlimmerKlondikeSolitaire::Model::FoundationPile

Inherits:
Object
  • Object
show all
Defined in:
app/glimmer_klondike_solitaire/model/foundation_pile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game, suit) ⇒ FoundationPile

Returns a new instance of FoundationPile.



6
7
8
9
10
# File 'app/glimmer_klondike_solitaire/model/foundation_pile.rb', line 6

def initialize(game, suit)
  @game = game
  @suit = suit
  reset!
end

Instance Attribute Details

#suitObject (readonly)

Returns the value of attribute suit.



4
5
6
# File 'app/glimmer_klondike_solitaire/model/foundation_pile.rb', line 4

def suit
  @suit
end

Instance Method Details

#add!(playing_card) ⇒ Object

adds a card validates if it is a card that belongs to the suit



18
19
20
21
22
23
24
25
26
27
28
# File 'app/glimmer_klondike_solitaire/model/foundation_pile.rb', line 18

def add!(playing_card)
  if playing_card.suit == suit &&
    (
      (playing_cards.empty? && playing_card.rank == 1) ||
      playing_card.rank == (playing_cards.last.rank + 1)
    )
    playing_cards.push(playing_card)
  else
    raise "Cannot add #{playing_card} to #{self}"
  end
end

#playing_cardsObject



30
31
32
# File 'app/glimmer_klondike_solitaire/model/foundation_pile.rb', line 30

def playing_cards
  @playing_cards ||= []
end

#reset!Object



12
13
14
# File 'app/glimmer_klondike_solitaire/model/foundation_pile.rb', line 12

def reset!
  playing_cards.clear
end

#to_sObject



34
35
36
# File 'app/glimmer_klondike_solitaire/model/foundation_pile.rb', line 34

def to_s
  "Foundation Pile #{suit} (#{playing_cards.map {|card| "#{card.rank}#{card.suit.to_s[0].upcase}"}.join(" | ")})"
end