Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Created June 27, 2018 10:36
Show Gist options
  • Save rodloboz/ed1ab57714554bed882356a2fcd13ea6 to your computer and use it in GitHub Desktop.
Save rodloboz/ed1ab57714554bed882356a2fcd13ea6 to your computer and use it in GitHub Desktop.
Black Jack
def pick_bank_score
# TODO: Use Random to get a new random score
# rand(16...21) doesn't include 21
rand (16..21) # includes 21
end
def pick_player_card
# TODO: Use Random to get a new random card
rand(1..11)
end
require_relative 'black_jack'
def state_of_the_game(player_score, bank_score)
# TODO: return (not print!) a message containing the player's score and bank's score
"Your score is: #{player_score}, the bank score is #{bank_score}"
end
def end_game_message(player_score, bank_score)
# TODO: return (not print!) a message telling if the user won or lost.
if player_score > 21 || bank_score > player_score
"You lose."
elsif player_score == bank_score
"Push! Try again!"
elsif player_score == 21
"Blackjack! You win!"
elsif player_score > bank_score
"You won!"
end
end
def play_game
# TODO: make the user play from terminal in a while loop that will stop
# when the user will not be asking for a new card
player_score = 0
bank_score = pick_bank_score
answer = 'yes'
# until answer != 'y' || answer != 'yes'
p answer == 'y' || answer == 'yes'
# player chooses cards
while answer == 'y' || answer == 'yes'
# player_score = player_score + pick_player_card
player_score += pick_player_card
puts "Card? 'y' or 'yes' to get a new card"
print "> "
answer = gets.chomp
puts state_of_the_game(player_score, bank_score)
end
# calculate result
end_game_message(player_score, bank_score)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment