Class: Gambit::Tools::Cards::StandardCard

Inherits:
Card
  • Object
show all
Defined in:
lib/gambit/tools/cards/card.rb

Constant Summary collapse

@@suit_order =
[:clubs, :diamonds, :hearts, :spades]
@@value_order =
[ :ace, 2, 3, 4, 5, 6, 7, 8, 9, 10,
:jack, :queen, :king ]
@@use_jokers =
false
@@jokers_are_high =
true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Viewable

append_features, #view

Constructor Details

#initialize(value, suit) ⇒ StandardCard

Creates a new StandardCard of value and suit.



106
107
108
# File 'lib/gambit/tools/cards/card.rb', line 106

def initialize( value, suit )
	@value, @suit = value, suit
end

Instance Attribute Details

#suitObject (readonly)

The suit of this card.



113
114
115
# File 'lib/gambit/tools/cards/card.rb', line 113

def suit
  @suit
end

#valueObject (readonly)

The value or name of this card.



111
112
113
# File 'lib/gambit/tools/cards/card.rb', line 111

def value
  @value
end

Class Method Details

.eachObject

Yields one of each possible card, in turn for Deck creation.



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/gambit/tools/cards/card.rb', line 92

def self.each(  )
	@@suit_order.each do |suit|
		@@value_order.each do |value|
			yield new(value, suit)
		end
	end
	
	if @@use_jokers
		yield new(:joker, :red)
		yield new(:joker, :black)
	end
end

.jokers_are_highObject

Determine if jokers are high.



89
# File 'lib/gambit/tools/cards/card.rb', line 89

def self.jokers_are_high(  )      @@jokers_are_high        end

.jokers_are_high=(high) ⇒ Object

When set (default), the jokers are considered high cards in sorting.



87
# File 'lib/gambit/tools/cards/card.rb', line 87

def self.jokers_are_high=( high ) @@jokers_are_high = high end

.suit_orderObject

Fetch the current suit order.



63
# File 'lib/gambit/tools/cards/card.rb', line 63

def self.suit_order(  )       @@suit_order         end

.suit_order=(suits) ⇒ Object

Set a suit order. Default is:

[:clubs, :diamonds, :hearts, :spades]


61
# File 'lib/gambit/tools/cards/card.rb', line 61

def self.suit_order=( suits ) @@suit_order = suits end

.use_jokersObject

Determine if jokers are being used.



80
# File 'lib/gambit/tools/cards/card.rb', line 80

def self.use_jokers(  )     @@use_jokers       end

.use_jokers=(use) ⇒ Object

When set (not the default), jokers will be used.



78
# File 'lib/gambit/tools/cards/card.rb', line 78

def self.use_jokers=( use ) @@use_jokers = use end

.value_orderObject

Fetch the current card value order.



74
# File 'lib/gambit/tools/cards/card.rb', line 74

def self.value_order(  )        @@value_order          end

.value_order=(values) ⇒ Object

Set a card value order. Default is:

[ :ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king ]


72
# File 'lib/gambit/tools/cards/card.rb', line 72

def self.value_order=( values ) @@value_order = values end

Instance Method Details

#<=>(other) ⇒ Object

Support for ranking (and sorting) the cards.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/gambit/tools/cards/card.rb', line 116

def <=>( other )
	if self == other
		0
	elsif @value == :joker or other.value == :joker
		if @value == :joker and other.value == :joker
			if @suit == :red then -1 else 1 end
		elsif @value == :joker
			if @@jokers_are_high then 1 else -1 end
		else
			if @@jokers_are_high then -1 else 1 end
		end
	else
		suit_rank       = self.class.suit_order.index(@suit)
		other_suit_rank =
			self.class.suit_order.index(other.suit)
	
		if suit_rank != other_suit_rank
			suit_rank <=> other_suit_rank
		else
			self.class.value_order.index(@value) <=>
				self.class.value_order.index(other.value)
		end
	end
end

#==(other) ⇒ Object

Compares this card with other for equality.



142
143
144
# File 'lib/gambit/tools/cards/card.rb', line 142

def ==( other )
	@value == other.value and @suit == other.suit
end

#nameObject

Returns the String name of this card (human readable).



147
148
149
150
151
152
153
154
# File 'lib/gambit/tools/cards/card.rb', line 147

def name(  )
	if @value == :joker
		"The #{@suit.to_s.capitalize} #{@value.to_s.capitalize}"
	else
		"The #{@value.to_s.capitalize} " +
		"of #{@suit.to_s.capitalize}"
	end
end