Class: Card

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/rora/model/card.rb

Overview

One of 52 of cards that are used to play card games.

A card is comprised of one rank and one suit, with 13 ranks and 4 suits there are 52 unique playing cards. A card can be created in one of two ways:

The first method uses the Rank and Suit enumerators card = Card.new(Rank::KING, Suit::HEART)

The second method uses rank and suit values. card = Card.new(“KH”)

Rank and suit values are case insensitive, so these work as well card = Card.new(“Kh”) card = Card.new(“kH”)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Card

Returns a new instance of Card.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rora/model/card.rb', line 22

def initialize(*args)
  if(args.size == 2)
    @rank = args[0]
    @suit = args[1]
  end
  if(args.size == 1)
    raise ArgumentError, "#{args[0]} is an invalid card sequence" if args[0].length != 2
    @rank = Rank.get(args[0][0].chr)
    @suit = Suit.get(args[0][1].chr)
  end
  @key =  @rank.key + @suit.key
  @card_repository = CardRepository.instance
  @uid = @card_repository.get(@key)
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



20
21
22
# File 'lib/rora/model/card.rb', line 20

def key
  @key
end

#rankObject (readonly)

Returns the value of attribute rank.



20
21
22
# File 'lib/rora/model/card.rb', line 20

def rank
  @rank
end

#suitObject (readonly)

Returns the value of attribute suit.



20
21
22
# File 'lib/rora/model/card.rb', line 20

def suit
  @suit
end

#uidObject (readonly)

Returns the value of attribute uid.



20
21
22
# File 'lib/rora/model/card.rb', line 20

def uid
  @uid
end

Class Method Details

.to_cards(string) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/rora/model/card.rb', line 59

def self.to_cards string
  cards = Array.new
  if !string.include?(",") && !string.include?(" ")
    string.scan(/../).each { |chars| cards << Card.new(chars) }
  else
    string.split(string.include?(",") ? "," : " ").each { |chars| cards << Card.new(chars) }
  end
  cards
end

Instance Method Details

#<=>(card) ⇒ Object



37
38
39
40
# File 'lib/rora/model/card.rb', line 37

def <=>(card)
  return self.rank <=> card.rank if card.rank != self.rank
  self.suit <=> card.suit
end

#==(card) ⇒ Object



50
51
52
53
# File 'lib/rora/model/card.rb', line 50

def == card
  return false if !card.kind_of? Card
  uid == card.uid
end

#eql?(card) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/rora/model/card.rb', line 46

def eql? card
  self == card
end

#hashObject



55
56
57
# File 'lib/rora/model/card.rb', line 55

def hash
   uid
end

#to_sObject



69
70
71
# File 'lib/rora/model/card.rb', line 69

def to_s
  "#{value}"
end

#valueObject



42
43
44
# File 'lib/rora/model/card.rb', line 42

def value
  "#{@rank.value} of #{@suit.value}s"
end