Class: Card

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

Constant Summary collapse

VALID_SUITS =
[:"E", :"P", :"O", :"C"]
VALID_VALUES =
[:"1", :"2", :"3", :"4", :"5", :"6", :"7",
:"10", :"11", :"12"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(suit, value) ⇒ Card

Returns a new instance of Card.



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

def initialize(suit, value)
  raise "Invalid Card" unless VALID_SUITS.include?(suit)
  @suit = suit
  @value = value
end

Instance Attribute Details

#suitObject

Returns the value of attribute suit.



43
44
45
# File 'lib/card.rb', line 43

def suit
  @suit
end

#valueObject

Returns the value of attribute value.



43
44
45
# File 'lib/card.rb', line 43

def value
  @value
end

Instance Method Details

#<=>(card) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/card.rb', line 49

def <=>(card)
  @value = :"7E" if is_7_e?
  card.value = :"7E" if card.is_7_e?


  @value = :"7O" if is_7_o?
  card.value = :"7O" if card.is_7_o?


  @value = :"1C" if is_1_c?
  card.value = :"1C" if card.is_1_c?

  @value = :"1O" if is_1_o?
  card.value = :"1O" if card.is_1_o?

  if value == card.value
    if suit.to_value > card.suit.to_value
      return 1
    else 
      return -1
    end
  end
  if value.to_value > card.value.to_value
    return 1
  else
    return -1
  end
end

#is_1_c?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/card.rb', line 87

def is_1_c?
  (value == :"1" && suit == :"C") or (value == :"1C")
end

#is_1_o?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/card.rb', line 91

def is_1_o?
  (value == :"1" && suit == :"O") or (value == :"1O")
end

#is_7_e?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/card.rb', line 79

def is_7_e?
  value == :"7" && suit == :"E"
end

#is_7_o?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/card.rb', line 83

def is_7_o?
  value == :"7" && suit == :"O"
end

#to_sObject



45
46
47
# File 'lib/card.rb', line 45

def to_s
  self.suit.to_s + " "  +  self.value.to_s
end