4. Ruby Methods
Activity 4.01: Blackjack Card Game
Solution
- Write a method to generate a deck of cards:
def generate_deck cards = (1..13) suits = ["Diamonds", "Clubs", "Spades", "Hearts"] deck = [] suits.each do |suit| cards.each do |card| deck << [card, suit] end end return deck end
- Write a method to shuffle the deck of cards:
def shuffle_deck(deck) shuffled_deck = [] while(deck.length > 0) do random_card_index = rand(deck.length) shuffled_deck << deck.delete_at(random_card_index) end return shuffled_deck end
- Write a method to identify the cards drawn in a hand:
def inspect_cards(cards, format: :short) hand = cards.map{|c| card_label(c, format: format...