0

I am trying to write a Ruby program to deal two cards to one player. Here is my code:

def get_card (card)
  suit = (card - 1)/13
  if (card == 0)
    type = " of clubs"
  elsif (card == 1)
    type = " of diamonds"
  elsif (card == 2)
    type = " of hearts"
  elsif (card == 3)
    type = " of spades"
  end

  (card) % 13
  if (card == 1)
    return "ace" + type
  elsif (card == 11)
    return "jack" + type
  elsif (card == 0)
    return "king" + type
  elsif (card == 2)
    return "2" + type
  elsif (card == 3)
    return "3" + type
  elsif (card == 4)
    return "4" + type
  elsif (card == 5)
    return "5" + type
  elsif (card == 6)
    return "6" + type
  elsif (card == 7)
    return "7" + type
  elsif (card == 8)
    return "8" + type
  elsif (card == 9)
    return "9" + type
  elsif (card == 10)
    return "10" + type                      
  elsif (card == 12)
    return "queen" + type
  end
end

def deal_cards
  total_cards = (1..52).to_a.shuffle

  player_value = [total_cards.pop, total_cards.pop]

  puts "Your cards are " + get_card(player_value[0]).to_s + " and " + get_card(player_value[1]).to_s
end

deal_cards();

The part:

puts "Your cards are " + get_card(player_value[0]).to_s + " and " + get_card(player_value[1]).to_s

does not show the value of

get_card(player_value[0]).to_s

or

get_card(player_value[1]).to_s

I would appreciate any help with this.

sawa
  • 165,429
  • 45
  • 277
  • 381
DBL
  • 45
  • 1
  • 1
  • 5

2 Answers2

7

The problem is that (card) % 13 doesn't mutate card. What you probably wanted was:

card %= 13


However, note that your abstraction is a little confusing and you have a lot of repetition. Why not try something like this:
suits  = [:clubs, :diamonds, :hearts, :spades]
values = [*2..10, :jack, :queen, :king, :ace]

deck = suits.product(values).shuffle.map do |suite, value|
  {suite: suite, value: value}
end

player_cards = deck.pop(2)

puts "Your cards are #{player_cards.first[:value]} of #{player_cards.first[:suite]} and #{player_cards.last[:value]} of #{player_cards.last[:suite]}"
  # => Your cards are jack of spades and 7 of clubs
ndnenkov
  • 35,425
  • 9
  • 72
  • 104
  • Not a big deal, but I don't see the reason of using symbols in `suits` and `values`. Isn't it more straightforward to use strings? – sawa Dec 13 '15 at 03:41
  • Here's why symbols: http://stackoverflow.com/questions/8189416/why-use-symbols-as-hash-keys-in-ruby – Tilo Dec 13 '15 at 06:00
0

Try:

def get_card (card)
  type = case ((card-1)/13)
  when 0 then "of clubs"
  when 1 then "of diamonds"
  when 2 then "of hearts"
  when 3 then "of spades"
  end
  card = case (card%13)
  when 0 then "king #{type}"
  when 1 then "ace #{type}"                 
  when 11 then "jack #{type}"
  when 12 then "queen #{type}"
  else card%13
  end
  "#{card} #{type}"
end

def deal_cards
  total_cards = (1..52).to_a.shuffle
  player_value = [total_cards.pop, total_cards.pop]
  puts "Your cards are #{get_card(player_value[0]).to_s} and #{get_card(player_value[1]).to_s}"
end

deal_cards()
Veets
  • 171
  • 6