Class: Gambit::Tools::Cards::Pile

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Viewable
Defined in:
lib/gambit/tools/cards/pile.rb

Overview

A tool for managing Piles of cards.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Viewable

append_features, #view

Constructor Details

#initializePile

Creates an empty Pile of cards.



41
42
43
44
45
46
47
48
# File 'lib/gambit/tools/cards/pile.rb', line 41

def initialize(  )
	@pile            = Array.new

	@add_to_top      = true
	@remove_from_top = true
	
	@face_up         = lambda { |index| true }
end

Instance Attribute Details

#add_to_topObject

When true (default), add() will place cards on the top of the pile. Otherwise, they go to the bottom.



54
55
56
# File 'lib/gambit/tools/cards/pile.rb', line 54

def add_to_top
  @add_to_top
end

#face_upObject

This method contains a Proc object that is passed card indices when rendering the default views. The Proc is excepted to return true or false indicating if the card at that index is face up or face down.



66
67
68
# File 'lib/gambit/tools/cards/pile.rb', line 66

def face_up
  @face_up
end

#remove_from_topObject

When true (default), remove() will pull cards from the top of the pile. Otherwise, they come from the bottom.



59
60
61
# File 'lib/gambit/tools/cards/pile.rb', line 59

def remove_from_top
  @remove_from_top
end

Instance Method Details

#[](index) ⇒ Object

Fetch a card by index.



69
70
71
# File 'lib/gambit/tools/cards/pile.rb', line 69

def []( index )
	@pile[index]
end

#add(card) ⇒ Object Also known as: <<

Add card to this Pile.



74
75
76
77
78
79
80
81
82
# File 'lib/gambit/tools/cards/pile.rb', line 74

def add( card )
	if @add_to_top
		@pile.unshift(card)
	else
		@pile << card
	end
	
	self
end

#eachObject

Iterate through the cards.



88
89
90
91
92
93
94
# File 'lib/gambit/tools/cards/pile.rb', line 88

def each(  )
	@pile.each_index do |index|
		next unless @face_up[index]
		
		yield @pile[index]
	end
end

#removeObject

Remove and return a card from this Pile.



97
98
99
100
101
102
103
# File 'lib/gambit/tools/cards/pile.rb', line 97

def remove(  )
	if @remove_from_top
		@pile.shift
	else
		@pile.pop
	end
end