Class: Patience::Area

Inherits:
Object show all
Defined in:
lib/patience/area.rb

Overview

Patience::Area provides area objects, that consist of piles. The goal of this class is to assemble piles into a logical bundle with a few common methods, so they could be controlled via only one interface. Basically, this class is useless on its own. The purpose of Area is to be inherited by other classes, which are more verbose in their intentions. By default, Area object instantiates without any cards and only with one pile. Every new pile would be created empty, even though you feed some cards to the new object. All the cards you provide to the new object, wouldn’t be placed anywhere. You have to allocate them manually.

field = Area.new
field.piles #=> [#<Patience::Pile>]
filed.piles[0].pos #=> (0, 0)
field.pos #=> (0, 0)

Direct Known Subclasses

Foundation, Stock, Tableau, Waste

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cards = [], piles_num = 1) ⇒ Area

Returns a new instance of Area.



21
22
23
24
25
# File 'lib/patience/area.rb', line 21

def initialize(cards=[], piles_num=1)
  @piles = []
  @cards = Pile.new(cards)
  piles_num.times { @piles << Pile.new }
end

Instance Attribute Details

#pilesObject (readonly)

Returns an array of piles in the area.



19
20
21
# File 'lib/patience/area.rb', line 19

def piles
  @piles
end

Instance Method Details

#add_from(outer_pile, card) ⇒ Object

Adds card from outer_pile to the very first pile of the area, flipping it en route.



57
58
59
# File 'lib/patience/area.rb', line 57

def add_from(outer_pile, card)
  card.flip! and piles.first << outer_pile.remove(card)
end

#cardsObject

Collects all cards in every pile and returns the array of these cards. If there are no cards in the area, returns an empty array.



40
41
42
# File 'lib/patience/area.rb', line 40

def cards
  piles.inject([]) { |cards, pile| cards << pile.cards }.flatten
end

#draw_on(win) ⇒ Object

Draws each pile of the area in the window.



45
46
47
# File 'lib/patience/area.rb', line 45

def draw_on(win)
  piles.each { |pile| pile.draw_on(win) }
end

#hit?(mouse_pos) ⇒ Boolean

Returns pile in the area, which has been clicked. If there is no such, returns nil.

Returns:

  • (Boolean)


51
52
53
# File 'lib/patience/area.rb', line 51

def hit?(mouse_pos)
  piles.find { |pile| pile.hit?(mouse_pos) }
end

#posObject

Shows position of the area. The position of the very first pile in the area, counts as its actual position.



29
30
31
# File 'lib/patience/area.rb', line 29

def pos
  piles.first.pos
end

#pos=(pos) ⇒ Object

Sets the position of every pile in the area to the same value.



34
35
36
# File 'lib/patience/area.rb', line 34

def pos=(pos)
  piles.each { |pile| pile.pos = *pos }
end