Reputation: 1
I am writing a watered down Blackjack game for my Ruby class, and I am hanging up after implementing my method for the hit or stay function. It isn't initiating the while
statement below it. Hoping someone can tell me if the syntax is off or just an indentation error.
def prompt_user()
puts 'Would you like another card? Press y or n'
hit_stay = gets.chomp
while hit_stay == 'y' ||'n'
if hit_stay == 'y'
puts "Your cards are"
puts $cards.append(num())
puts "Your total card count is"
puts $cards.sum
return true
elsif hit_stay == 'n'
puts 'your total is'
puts $cards.sum
return true
else
puts ' This is not a valid option.'
return false
end
end
end
This seems to be where the program gets wacky a little bit.
prompt_user()
while prompt_user() == 'y' || 'n'
$cards.sum <= 15
prompt_user()
if prompt_user() == 'n'
puts 'Your final card count is.'
puts $cards.sum
end
if 15 < $cards.sum <21
puts 'Your final card count is.'
puts $cards.sum
elsif $cards.sum > 21
puts 'Game Over You Bust'
end
end
I am a little newer to writing code, I know I need to clean it up a bit but stressing out a little bit about this one.
Upvotes: 0
Views: 51
Reputation: 331
This is not directly answering your question but first off, prompt_user() == 'y' || 'n'
is probably not what you're trying to do - this will always return true because it is saying "do this if the result of prompt_user() is equal to 'y', or if 'n'" however, 'n' itself is just a string not a condition and a non-empty string will evaluate to true so this will always be true no matter what the user's input is. To see what I mean, try doing puts "hello!" if 'n'
and you'll see "hello!". What you probably want is hit_stay == 'y' || hit_stay == 'n'
or even better ['y','n'].include?(hit_stay)
.
In your second part of the code, you're calling prompt_user
all over the place and you're checking for the result of it to be equal to 'y' or 'n' but returning true/false in the actual method... I think what you're looking to do is just call prompt_user until it returns false in which case you'd want something closer to this:
while prompt_user
$cards.sum <= 15
if prompt_user() == 'n'
puts 'Your final card count is.'
puts $cards.sum
if 15 < $cards.sum <21
puts 'Your final card count is.'
puts $cards.sum
elsif $cards.sum > 21
puts 'Game Over You Bust'
end
end
end
However, this code will still not work because 15 < $cards.sum < 21
is not valid ruby and should be 15 < $cards.sum && $cards.sum < 21
. Here's a more patched-up version of what you had, it still needs work to make it perfect (like exiting after you bust) but I'll leave that on you:
$cards = []
def num
5
end
def prompt_user()
puts 'Would you like another card? Press y or n'
hit_stay = gets.chomp
while ['y','n'].include?(hit_stay)
if hit_stay == 'y'
puts "Your cards are"
puts $cards.append(num())
puts "Your total card count is"
puts $cards.sum
return true
elsif hit_stay == 'n'
puts 'your total is'
puts $cards.sum
return true
else
puts ' This is not a valid option.'
return false
end
end
end
while prompt_user
$cards.sum <= 15
puts 'Your final card count is.'
puts $cards.sum
if 15 < $cards.sum && $cards.sum < 21
puts 'Your final card count is.'
puts $cards.sum
elsif $cards.sum > 21
puts 'Game Over You Bust'
end
end
PS. I'd also point out that using $ signs in variable names is not a typical ruby thing, you should just use alphanumeric characters. @
is used for instance/class variables but that's presumably not what you're going for here.
Upvotes: 1