Class: Gambit::Tools::Cards::Deck

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

Overview

A Deck manager for any class inheriting from Card.

Instance Method Summary collapse

Methods included from Viewable

append_features, #view

Constructor Details

#initialize(card_type = StandardCard) ⇒ Deck

Create a new Deck filled with card_type (StandardCard by default).



33
34
35
36
# File 'lib/gambit/tools/cards/deck.rb', line 33

def initialize( card_type = StandardCard )
	@deck = Array.new
	card_type.each { |card| @deck << card }
end

Instance Method Details

#countObject

Returns the Deck size.



39
40
41
# File 'lib/gambit/tools/cards/deck.rb', line 39

def count(  )
	@deck.size
end

#cut!(count = rand(@deck.size / 2) + @deck.size / 4) ⇒ Object

Cuts the deck at the provided count or a random default.



44
45
46
# File 'lib/gambit/tools/cards/deck.rb', line 44

def cut!( count = rand(@deck.size / 2) + @deck.size / 4 )
	@deck = @deck.values_at(count..-1, 0...count)
end

#deal(*hands) ⇒ Object

Deals cards to hands in round robin fashion. If a Fixnum is included in the argument list, that many cards will be dealt to each hand. Otherwise, the entire deck is dealt.

You may use Hand objects in hands or anything that supports the << operator.



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gambit/tools/cards/deck.rb', line 56

def deal( *hands )
	if count = hands.find { |hand| hand.is_a? Fixnum }
		hands.delete(count)
	else
		count = @deck.size
	end
	count.times do 
		hands.each do |hand|
			hand << @deck.shift
		end
	end
end

#drawObject

Removes and returns a card from the top of the deck.



70
71
72
# File 'lib/gambit/tools/cards/deck.rb', line 70

def draw(  )
	@deck.shift
end

#each(&block) ⇒ Object

Iterate through the cards.



77
78
79
# File 'lib/gambit/tools/cards/deck.rb', line 77

def each( &block )
	@deck.each(&block)
end

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

Replace card on the bottom of the deck.



92
93
94
# File 'lib/gambit/tools/cards/deck.rb', line 92

def replace( card )
	@deck << card
end

#shuffle!Object

Randomize the deck.



82
83
84
# File 'lib/gambit/tools/cards/deck.rb', line 82

def shuffle!(  )
	@deck = @deck.sort_by { rand }
end

#sort!(&block) ⇒ Object

Sort the deck by card value.



87
88
89
# File 'lib/gambit/tools/cards/deck.rb', line 87

def sort!( &block )
	@deck.sort!(&block)
end