Class: Tundengine::Cards::Card

Inherits:
Object
  • Object
show all
Includes:
AlgebraicDataType, Move
Defined in:
lib/tundengine/cards/card.rb

Direct Known Subclasses

Null

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Move

#self_or_yield, #yield_self_or_lock!

Methods included from AlgebraicDataType

#==, #hash

Constructor Details

#initialize(rank, suit) ⇒ Card

Returns a new instance of Card.



10
11
12
13
14
# File 'lib/tundengine/cards/card.rb', line 10

def initialize(rank, suit)
  @rank = rank
  @suit = suit
  super()
end

Instance Attribute Details

#rankObject (readonly)

Returns the value of attribute rank.



8
9
10
# File 'lib/tundengine/cards/card.rb', line 8

def rank
  @rank
end

#suitObject (readonly)

Returns the value of attribute suit.



8
9
10
# File 'lib/tundengine/cards/card.rb', line 8

def suit
  @suit
end

Instance Method Details

#beats?(other_card, trump_suit) ⇒ Boolean

assumes other_card is winning the current trick

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tundengine/cards/card.rb', line 33

def beats?(other_card, trump_suit)
  suit_powers = { other_card.suit => 1, trump_suit => 2 }
  suit_powers[Suits::Null.instance] = 0

  own_suit_power, other_suit_power = [@suit, other_card.suit]
    .map { |s| suit_powers.fetch(s, 0) }

  if own_suit_power == other_suit_power
    beats_same_suit?(other_card)
  else
    own_suit_power > other_suit_power
  end
end

#beats_same_suit?(other_card) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/tundengine/cards/card.rb', line 47

def beats_same_suit?(other_card)
  @rank > other_card.rank
end

#exists?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/tundengine/cards/card.rb', line 51

def exists?
  true
end

#is_of_any_rank?(ranks) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/tundengine/cards/card.rb', line 20

def is_of_any_rank?(ranks)
  ranks.include? @rank
end

#is_of_any_suit?(suits) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/tundengine/cards/card.rb', line 24

def is_of_any_suit?(suits)
  suits.include? @suit
end

#round_pointsObject



28
29
30
# File 'lib/tundengine/cards/card.rb', line 28

def round_points
  @rank.round_points
end

#to_sObject



16
17
18
# File 'lib/tundengine/cards/card.rb', line 16

def to_s
  "#{@rank.to_s} de #{@suit.to_s}"
end