Skip to content

Instantly share code, notes, and snippets.

@falonofthetower
Last active August 29, 2015 14:24
Show Gist options
  • Save falonofthetower/9019bcc7938fcb0d15a9 to your computer and use it in GitHub Desktop.
Save falonofthetower/9019bcc7938fcb0d15a9 to your computer and use it in GitHub Desktop.
# Interactive command line blackjack game
def calculate_total(cards)
# [['H', '3'], ['S', 'Q'], ... ]
arr = cards.map{|e| e[1] }
total = 0
arr.each do |value|
if value == "A"
total += 11
elsif value.to_i == 0 # J, Q, K
total += 10
else
total += value.to_i
end
end
#correct for Aces
arr.select{|e| e == "A"}.count.times do
total -= 10 if total > 21
end
total
end
# Start Game
def play_game
first_game = false
puts "Welcome to Blackjack!"
suits = ['H', 'D', 'S', 'C']
cards = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
deck = suits.product(cards)
deck.shuffle!
# Deal Cards
mycards = []
dealercards = []
mycards << deck.pop
dealercards << deck.pop
mycards << deck.pop
dealercards << deck.pop
dealertotal = calculate_total(dealercards)
mytotal = calculate_total(mycards)
# Show Cards
puts "Dealer has: #{dealercards[0]} and #{dealercards[1]}, for a total of #{dealertotal}"
puts "You have: #{mycards[0]} and #{mycards[1]}, for a total of: #{mytotal}"
puts ""
# Player turn
if mytotal == 21
puts "Congratulations, you hit blackjack! You win!"
play_again
end
while mytotal < 21
puts "What would you like to do? 1) hit 2) stay"
hit_or_stay = gets.chomp
if !['1', '2'].include?(hit_or_stay)
puts "Error: you must enter 1 or 2"
next
end
if hit_or_stay == "2"
puts "You chose to stay."
break
end
#hit
new_card = deck.pop
puts "Dealing card to player: #{new_card}"
mycards << new_card
mytotal = calculate_total(mycards)
puts "Your total is now: #{mytotal}"
if mytotal == 21
puts "Congratulations, you hit blackjack! You win!"
play_again
elsif mytotal > 21
puts "Sorry, it looks like you busted!"
play_again
end
end
# Dealer turn
if dealertotal == 21
puts "Sorry, dealer hit blackjack. You lose."
play_again
end
while dealertotal < 17
#hit
new_card = deck.pop
puts "Dealing new card for dealer: #{new_card}"
dealercards << new_card
dealertotal = calculate_total(dealercards)
puts "Dealer total is now: #{dealertotal}"
if dealertotal == 21
puts "Sorry, dealer hit blackjack. You lose."
play_again
elsif dealertotal > 21
puts "Congratulations, dealer busted! You win!"
play_again
end
end
# Compare hands
puts "Dealer's cards: "
dealercards.each do |card|
puts "=> #{card}"
end
puts ""
puts "Your cards:"
mycards.each do |card|
puts "=> #{card}"
end
puts ""
if dealertotal > mytotal
puts "Sorry, dealer wins."
elsif dealertotal < mytotal
puts "Congratulations, you win!"
else
puts "It's a tie!"
end
play_again
end
def play_again
puts "Play again? Y/N"
if gets.chomp.downcase == "n"
exit
else
play_game
end
end
play_game
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment