0

I'm coming to Ruby from Java and not really comfortable with it on the second day. What I'm doing is to initialize decks of cards with a given number of decks. Say I will be playing with 2 decks of cards, 52 cards in each deck, and thus 104 cards in total (Jokers are not used). My program goes:

def initialize(num)
    @num_of_decks = num
    # initialize my cards here...
end

After initialization, I want my cards to be:

cards = {"Diamond A", "Diamond 2", ... "Diamond K", 
         "Club A", ...
         ...
         "Spade A", ... "Spade K", // end of the first deck
         "Diamond A", ...
         ...
         "Spade A", ... "Spade K" // end of the second deck
        }

I want my program to be able to adapt to whatever num_of_decks is. I don't really have a clear feeling of how Ruby loops work by now, especially when it seems that for loops are discouraged in Ruby. Hopefully anyone can provide an example here so that I can study how it works.

2
  • 3
    You're mixing up your syntaxes, that's not valid Ruby. Arrays use [], hashes use {}. Commented Jan 28, 2014 at 19:29
  • If you're coming from a Java background, you should be able to think your way through this. It's relatively straight-forward coding problem. The steps you'd take would be identical, though the syntax will be different and the results might not be as idiomatic as they could be. Commented Jan 28, 2014 at 19:33

4 Answers 4

4
faces = %w( A 2 3 4 5 6 7 8 9 10 J Q K )
suits = %w( Diamond Club Heart Spade )
deck = suits.product(faces).map { |p| p.join ' ' }

def initialize(num)
  @num_of_decks = num
  cards = deck * num
end
Sign up to request clarification or add additional context in comments.

Comments

3

Hmmm, I'm not entirely sure what you're asking, but perhaps something like this will work?

class DrawPile
  CARD_SUITS = ["Diamond", "Heart", "Spade", "Club"]
  CARD_FACES = ["A", *(2..10).map(&:to_s), "J", "Q", "K"]
  A_DECK = CARD_SUITS.product(CARD_FACES).map{|x| x.join " "}
  def initialize(num)
    @num_of_decks = num
    @cards = (A_DECK * num).shuffle
  end

  attr_reader :num_of_decks, :cards
end

Usage:

pile = DrawPile.new(2)
pile.num_of_decks #=> 2
pile.cards #=> ["Spade 2", "Spade K", "Heart 4", "Heart 7", ...]

Comments

0
suits = ['spades', 'diamonds', 'clubs', 'hearts']
values = ['Ace', '2', '3', '4', 'Jack', 'Queen']
cards = suits.product(values).map{|suit, value| "#{value} of #{suit}"}

p cards #=> ["Ace of spades", "2 of spades", "3 of spades", "4 of spades", "Jack of spades"

Comments

0

You can use:

a = ['Spade', 'Diamond', 'Club', 'Heart']
b = ['A', *('2'..'10'), 'J', 'Q', 'K']
cards = a.product(b).collect { |a| a.join(' ') }
total_cards = [*(cards * num_of_decks)]
# => ["Spade A", "Spade 2", "Spade 3", "Spade 4", ..., "Diamond A", "Diamond 2", "Diamond 3", ..., "Spade A", "Spade 2", "Spade 3", "Spade 4", ...]

And here is where you can play with it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.