Class: Deck

Inherits:
Object
  • Object
show all
Defined in:
lib/rora/model/deck.rb

Overview

A complete set of 52 playing cards.

A deck of cards may be used for playing a great variety of card games, with varying elements of skill and chance, some of which are played for money.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDeck

Returns a new instance of Deck.



10
11
12
13
14
15
16
17
# File 'lib/rora/model/deck.rb', line 10

def initialize
  @cards = Array.new
  Suit.values.each do |suit|
    Rank.values.each do |rank|
      @cards << Card.new(rank, suit)
    end
  end
end

Instance Attribute Details

#cardsObject (readonly)

Returns a shallow copy of the cards in the deck.



41
42
43
# File 'lib/rora/model/deck.rb', line 41

def cards
  @cards
end

Instance Method Details

#combination(number) ⇒ Object



105
106
107
# File 'lib/rora/model/deck.rb', line 105

def combination number
  cards.combination(number).to_a
end

#contains?(argument) ⇒ Boolean

Determines if the deck contains the given card.

Returns:

  • (Boolean)


87
88
89
90
91
92
# File 'lib/rora/model/deck.rb', line 87

def contains? argument
  if argument.kind_of? Card
    return @cards.include?(argument)
  end
  @cards.include? Card.new(argument)
end

#contains_any?(argument) ⇒ Boolean

Determines if the deck contains any of the given cards.

Returns:

  • (Boolean)


95
96
97
98
99
100
101
102
103
# File 'lib/rora/model/deck.rb', line 95

def contains_any? argument
  if argument.kind_of? Array
    argument.each {|card| return true if @cards.include? card}
  end
  if argument.kind_of? String
    Card.to_cards(argument).each {|card| return true if @cards.include? card}
  end
  false
end

#count_cards_with_rank(rank) ⇒ Object

Returns the total number of cards with the given rank in the deck.



46
47
48
49
50
51
52
# File 'lib/rora/model/deck.rb', line 46

def count_cards_with_rank rank
  count = 0;
  @cards.each do |card|
    count +=1 if card.rank.eql?(rank)
  end
  count
end

#count_cards_with_suit(suit) ⇒ Object

Returns the total number of cards with the given suit in the deck.



55
56
57
58
59
60
61
# File 'lib/rora/model/deck.rb', line 55

def count_cards_with_suit suit
  count = 0;
  @cards.each do |card|
    count +=1 if card.suit.eql?(suit)
  end
  count
end

#dealObject

Deals a single card from the deck.



31
32
33
# File 'lib/rora/model/deck.rb', line 31

def deal
  @cards.delete_at 0
end

#empty?Boolean

Determines if the deck is empty.

Returns:

  • (Boolean)


36
37
38
# File 'lib/rora/model/deck.rb', line 36

def empty?
  @cards.empty?
end

#remove(argument) ⇒ Object

Removes cards from the deck.

This method can remove a Card, an Enumerable (an Array or Hash of Cards) or a StartingHand from the deck.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rora/model/deck.rb', line 67

def remove argument
  if argument.kind_of? Card
    @cards.delete argument
  end
  if argument.kind_of? Enumerable
    argument.each { |card| @cards.delete card }
  end
  if argument.kind_of? StartingHand
    argument.cards.each { |card| @cards.delete card }
  end
  if argument.kind_of? Board
    argument.cards.each { |card| @cards.delete card }
  end
  if argument.kind_of? String
    Card.to_cards(argument).each { |card| @cards.delete card }
  end
  self
end

#shuffleObject

Shuffles all cards in the deck.



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

def shuffle
  @cards.shuffle!
  self
end

#sizeObject

Returns the number of cards in the deck.



26
27
28
# File 'lib/rora/model/deck.rb', line 26

def size
  @cards.size
end