Class: Simplicard::CardDeck
- Inherits:
-
Object
- Object
- Simplicard::CardDeck
- Defined in:
- lib/simplicard.rb
Instance Attribute Summary collapse
-
#added_cards ⇒ Object
readonly
Returns the value of attribute added_cards.
-
#dealt_cards ⇒ Object
readonly
Returns the value of attribute dealt_cards.
-
#picked_cards ⇒ Object
readonly
Returns the value of attribute picked_cards.
Instance Method Summary collapse
-
#add_a_card(card) ⇒ Object
return the position that the card is inserted into return false if deck is full or card exists in the deck.
- #cards_left ⇒ Object
- #deal ⇒ Object
- #empty? ⇒ Boolean
- #find_card(card) ⇒ Object
-
#initialize ⇒ CardDeck
constructor
A new instance of CardDeck.
-
#is_complete? ⇒ Boolean
check to see if the deck has all 52 cards without duplicate.
- #is_full? ⇒ Boolean
- #my_deck ⇒ Object
- #pick_a_card ⇒ Object
- #put_back_cards ⇒ Object
- #show ⇒ Object
- #shuffle_it ⇒ Object
Constructor Details
#initialize ⇒ CardDeck
Returns a new instance of CardDeck.
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/simplicard.rb', line 123 def initialize @deck ||= [] (1..13).each do |value| 4.times do |suit| card = Card.new(value,suit) @deck << card end end @dealt_cards = [] @picked_cards = [] @added_cards = [] end |
Instance Attribute Details
#added_cards ⇒ Object (readonly)
Returns the value of attribute added_cards.
121 122 123 |
# File 'lib/simplicard.rb', line 121 def added_cards @added_cards end |
#dealt_cards ⇒ Object (readonly)
Returns the value of attribute dealt_cards.
121 122 123 |
# File 'lib/simplicard.rb', line 121 def dealt_cards @dealt_cards end |
#picked_cards ⇒ Object (readonly)
Returns the value of attribute picked_cards.
121 122 123 |
# File 'lib/simplicard.rb', line 121 def picked_cards @picked_cards end |
Instance Method Details
#add_a_card(card) ⇒ Object
return the position that the card is inserted into return false if deck is full or card exists in the deck
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/simplicard.rb', line 200 def add_a_card(card) if is_full? puts "Deck is full.You can't add a card" return false elsif empty? @deck << card @added_cards << card cards_left elsif !find_card(card) random = rand(cards_left) insert_card(random, card) @added_cards << card random else puts "Can't add a duplicate card to the Deck" return false end end |
#cards_left ⇒ Object
237 238 239 |
# File 'lib/simplicard.rb', line 237 def cards_left get_deck.length end |
#deal ⇒ Object
231 232 233 234 235 |
# File 'lib/simplicard.rb', line 231 def deal card = get_deck.pop @dealt_cards << card card end |
#empty? ⇒ Boolean
161 162 163 164 165 166 167 |
# File 'lib/simplicard.rb', line 161 def empty? if @deck.length == 0 return true else return false end end |
#find_card(card) ⇒ Object
249 250 251 252 253 254 255 256 |
# File 'lib/simplicard.rb', line 249 def find_card(card) if card_valid?(card) get_deck.each_with_index do |x,index| return index if (x.value == card.value) && (x.suit.value == card.suit.value) end return false end end |
#is_complete? ⇒ Boolean
check to see if the deck has all 52 cards without duplicate
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/simplicard.rb', line 174 def is_complete? if empty? puts "Deck is empty" return false elsif !is_full? puts "Deck is missing some cards" return false else myhash = {} @deck.each do |x| myhash[x.suit.value] ||= [] myhash[x.suit.value] << x.value end Suit::VALID_SUIT.each do |suit| return false if myhash[suit].length != 13 end return true end end |
#is_full? ⇒ Boolean
169 170 171 |
# File 'lib/simplicard.rb', line 169 def is_full? return true if @deck.length >=52 end |
#my_deck ⇒ Object
136 137 138 |
# File 'lib/simplicard.rb', line 136 def my_deck @deck end |
#pick_a_card ⇒ Object
140 141 142 143 144 145 146 147 |
# File 'lib/simplicard.rb', line 140 def pick_a_card if cards_left > 0 random = rand(cards_left) card = get_deck.delete_at(random) @picked_cards << card card end end |
#put_back_cards ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/simplicard.rb', line 149 def put_back_cards (@picked_cards + @dealt_cards).each do |x| if !find_card(x) @deck << x end end @dealt_cards = [] @picked_cards = [] @added_cards = [] @deck end |
#show ⇒ Object
241 242 243 244 245 246 247 |
# File 'lib/simplicard.rb', line 241 def show result = [] get_deck.each do |x| result << x.show end result end |
#shuffle_it ⇒ Object
221 222 223 224 225 226 227 228 229 |
# File 'lib/simplicard.rb', line 221 def shuffle_it cards_left.times do |i| random = rand(cards_left) temp = @deck[i] @deck[i] = @deck[random] @deck[random] = temp end get_deck end |