Class: RubyLabs::RandomLab::Card
- Inherits:
-
Object
- Object
- RubyLabs::RandomLab::Card
- Defined in:
- lib/randomlab.rb
Overview
Card
A Card object represents a single card from a standard 52-card deck. The two attributes of a card are its rank (represented by a symbol such as :ace, :king, :three, etc) and its suit (:spades, :hearts, etc).
Constant Summary collapse
- Suits =
[:spades, :hearts, :diamonds, :clubs]
- Ranks =
[:ace, :king, :queen, :jack, :ten, :nine, :eight, :seven, :six, :five, :four, :three, :two]
- @@outputform =
:utf8
Instance Attribute Summary collapse
-
#rank ⇒ Object
Returns the value of attribute rank.
-
#suit ⇒ Object
Returns the value of attribute suit.
Class Method Summary collapse
Instance Method Summary collapse
-
#<=>(x) ⇒ Object
Define the sort ordering for Card objects.
-
#==(x) ⇒ Object
Compare this Card with Card x.
-
#initialize(id = nil) ⇒ Card
constructor
Create a new Card object.
-
#inspect ⇒ Object
(also: #to_s)
Make a string to display a Card objects on the terminal.
Constructor Details
#initialize(id = nil) ⇒ Card
Create a new Card object. Cards are ordered from 0 to 51, with 0 being the ace of spades and 51 being the two of clubs. If no argument is passed to new, a random card is chosen. If an id
between 0 and 51 is passed the specified card is created.
Example:
>> Card.new
=> 5H
>> Card.new(0)
=> AS
>> Card.new(1)
=> KS
>> Card.new(51)
=> 2C
265 266 267 268 269 270 |
# File 'lib/randomlab.rb', line 265 def initialize(id = nil) id = rand(52) if id.nil? raise "card must be between 0 and 51" if id < 0 || id > 51 @suit = Suits[id / 13] @rank = Ranks[id % 13] end |
Instance Attribute Details
#rank ⇒ Object
Returns the value of attribute rank.
241 242 243 |
# File 'lib/randomlab.rb', line 241 def rank @rank end |
#suit ⇒ Object
Returns the value of attribute suit.
241 242 243 |
# File 'lib/randomlab.rb', line 241 def suit @suit end |
Class Method Details
.print_latex ⇒ Object
335 336 337 |
# File 'lib/randomlab.rb', line 335 def Card.print_latex @@outputform = :latex end |
.print_utf8 ⇒ Object
339 340 341 |
# File 'lib/randomlab.rb', line 339 def Card.print_utf8 @@outputform = :utf8 end |
Instance Method Details
#<=>(x) ⇒ Object
Define the sort ordering for Card objects. Cards are compared first by suit, and then by rank. The result of sorting a hand (and array of Card objects) is the common situation for most card games, where cards are grouped by suit.
Example (assuming d
is a complete deck of 52 cards):
>> h = permute!(d).first(13)
=> [2C, QS, 8S, 6C, 10H, JS, AD, AS, 7D, 8D, JC, 4C, AH]
>> h.sort
=> [AS, QS, JS, 8S, AH, 10H, AD, 8D, 7D, JC, 6C, 4C, 2C]
289 290 291 292 293 294 295 296 297 |
# File 'lib/randomlab.rb', line 289 def <=>(x) r0 = Ranks.index(@rank); r1 = Ranks.index(x.rank) s0 = Suits.index(@suit); s1 = Suits.index(x.suit) if (res = (s0 <=> s1)) == 0 return (r0 <=> r1) else return res end end |
#==(x) ⇒ Object
Compare this Card with Card x. Two cards are equal if they have the same suit and same rank.
275 276 277 |
# File 'lib/randomlab.rb', line 275 def ==(x) return @suit == x.suit && @rank == x.rank end |
#inspect ⇒ Object Also known as: to_s
Make a string to display a Card objects on the terminal.
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/randomlab.rb', line 303 def inspect s = "" s << case @rank when :ace then "A" when :king then "K" when :queen then "Q" when :jack then "J" when :ten then "10" when :nine then "9" when :eight then "8" when :seven then "7" when :six then "6" when :five then "5" when :four then "4" when :three then "3" when :two then "2" end if @@outputform == :utf8 s << case @suit when :spades then "\xe2\x99\xa0" when :hearts then "\xe2\x99\xa5" when :clubs then "\xe2\x99\xa3" when :diamonds then "\xe2\x99\xa6" end else s << "!irb" + @suit.to_s.chop end return s end |