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
18
# 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
  @cards.sort!
end

Instance Attribute Details

#cardsObject (readonly)

Returns a shallow copy of the cards in the deck.



64
65
66
# File 'lib/rora/model/deck.rb', line 64

def cards
  @cards
end

Instance Method Details

#combination(number) ⇒ Object



128
129
130
# File 'lib/rora/model/deck.rb', line 128

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

#contains?(argument) ⇒ Boolean

Determines if the deck contains the given card.

Returns:

  • (Boolean)


110
111
112
113
114
115
# File 'lib/rora/model/deck.rb', line 110

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)


118
119
120
121
122
123
124
125
126
# File 'lib/rora/model/deck.rb', line 118

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.



69
70
71
72
73
74
75
# File 'lib/rora/model/deck.rb', line 69

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.



78
79
80
81
82
83
84
# File 'lib/rora/model/deck.rb', line 78

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.



54
55
56
# File 'lib/rora/model/deck.rb', line 54

def deal
  @cards.delete_at 0
end

#empty?Boolean

Determines if the deck is empty.

Returns:

  • (Boolean)


59
60
61
# File 'lib/rora/model/deck.rb', line 59

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.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rora/model/deck.rb', line 90

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

#remove_all(argument) ⇒ Object

Removes all cards with the given suit or rank, retaining all others.



32
33
34
35
36
37
38
39
40
# File 'lib/rora/model/deck.rb', line 32

def remove_all argument
  if argument.kind_of? Suit
    @cards = @cards.find_all{|card| !card.suit.eql?(argument) }
  end
  if argument.kind_of? Rank
    @cards = @cards.find_all{|card| !card.rank.eql?(argument) }
  end
  self
end

#retain_all(argument) ⇒ Object

Retains all cards with the given suit or rank, removing all others.



21
22
23
24
25
26
27
28
29
# File 'lib/rora/model/deck.rb', line 21

def retain_all argument
  if argument.kind_of? Suit
    @cards = @cards.find_all{|card| card.suit.eql?(argument) }
  end
  if argument.kind_of? Rank
    @cards = @cards.find_all{|card| card.rank.eql?(argument) }
  end
  self
end

#shuffleObject

Shuffles all cards in the deck.



43
44
45
46
# File 'lib/rora/model/deck.rb', line 43

def shuffle
  @cards.shuffle!
  self
end

#sizeObject

Returns the number of cards in the deck.



49
50
51
# File 'lib/rora/model/deck.rb', line 49

def size
  @cards.size
end