Daquon Daquon - 3 months ago 14
Ruby Question

Error messages in my blackjack cli in ruby

I'm working on a learn.co lab Blackjack cli the line is https://learn.co/tracks/web-development-fundamentals/intro-to-ruby/looping/blackjack-cli?batch_id=166&track_id=10415 however of the 15 examples required to pass i keep getting an error for 6 i'am mainly having trouble with the initial_round method and the hit? method.

i keep getting an error in the initial round method asking me to call on the display_card_total method to print the sum of cards and the hit? confuses me a little as to what exact its asking

def deal_card
rand(11) + 1
end

def display_card_total(card)
puts "Your cards add up to #{card}"
end

def prompt_user
puts "Type 'h' to hit or 's' to stay"
end

def get_user_input
gets.chomp
end

def end_game(card_total)
puts "Sorry, you hit #{card_total}. Thanks for playing!"
end

def initial_round
deal_card
deal_card
return deal_card + deal_card
puts display_card_total
end

def hit?
prompt_user
end

def invalid_command
puts "Please enter a valid command"
end


hope this is enough information

Answer

You haven't followed the assignment.

It specifies that the hit? method should take an argument of the current card total, so it should be...

  def hit?(current_card_total)

It then specified that you should do prompt_user and get_user_input and then test the result for "h" or "s" or other and take appropriate action.

If you do a "h" for hit, the current_card_total will be increased, otherwise if you do an "s" it's unchanged, but you need to return the value, regardless of whether it's changed or not.

If the user enters something else beside "h" or "s" you call the invalid_command method and prompt again for the correct value (prompt_user) and you can try to get a response again with get_user_input

So, something like this...

def hit?(current_card_value)
  prompt_user
  user_input = get_user_input
  while user_input != "h" && user_input != "s"
    invalid_command
    prompt_user
    user_input = get_user_input
  end
  if user_input == "h"
    current_card_value += deal_card
  end
  return current_card_value
end

There's a few things wrong with your initial_deal but just to start with, you need to keep track of the deal_card results in a variable

current_card_total = deal_card
current_card_total += deal_card

That way current_card_total has the accumulated total. Just doing

deal_card
deal_Card

doesn't store the results of deal_card anywhere.

Comments