Created
September 27, 2015 02:35
-
-
Save falonofthetower/e52a342a196600eaebc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calculate_total(cards) | |
array = cards.map {|e| e[1]} | |
total = 0 | |
array.each do |value| | |
if value == "Ace" | |
total += 11 | |
elsif value == "Jack" || value == "Queen" || value == "King" | |
total += 10 | |
else | |
total += value.to_i | |
end | |
end | |
array.select {|e| e == "Ace"}.count.times do # Multiple Aces | |
total -= 10 if total > 21 | |
end | |
total | |
end | |
puts "Welcome to Blackjack!" | |
puts "Please enter your name" | |
name = gets.chomp.capitalize | |
puts "Good luck #{name}!" | |
puts | |
loop do | |
# Deck | |
suits = %w(Hearts Diamonds Clubs Spades) | |
cards = %w(2 3 4 5 6 7 8 9 10 Jack Queen King Ace) | |
deck = suits.product(cards) | |
deck.shuffle! | |
# Deal Cards | |
dealer_cards = [] | |
player_cards = [] | |
2.times do | |
player_cards << deck.pop | |
dealer_cards << deck.pop | |
end | |
dealer_total = calculate_total(dealer_cards) | |
player_total = calculate_total(player_cards) | |
# Show Cards | |
puts "Dealer has: #{dealer_cards[0].reverse.join(' of ')} and #{dealer_cards[1].reverse.join(' of ')} for a total of #{dealer_total}." | |
puts "#{name} you have: #{player_cards[0].reverse.join(' of ')} and #{player_cards[1].reverse.join(' of ')} for a total of #{player_total}." | |
puts | |
# Player turn | |
if player_total == 21 && dealer_total == 21 | |
puts "#{name.upcase}, you and the Dealer both hit Blackjack... It's a push." | |
elsif player_total == 21 | |
puts "BLACKJACK... YOU WON #{name.upcase}!" | |
else | |
while player_total < 21 | |
puts "Do you want to Hit or Stay? (h/s)" | |
hit_or_stay = gets.chomp.downcase | |
puts | |
if hit_or_stay == "s" | |
puts "#{name} you decided to stay." | |
puts | |
break | |
elsif hit_or_stay == "h" | |
new_card = deck.pop | |
puts "Dealing new card to #{name}: #{new_card.reverse.join(' of ')}" | |
player_cards << new_card | |
player_total = calculate_total(player_cards) | |
puts "#{name} your total is now #{player_total}." | |
puts | |
if player_total == 21 | |
puts "BLACKJACK... YOU WON #{name.upcase}!" | |
elsif player_total > 21 | |
puts "Sorry, #{name}, you busted and lost. :-(" | |
end | |
else | |
puts "Please pick 'h' to hit or 's' to stay." | |
end | |
end | |
end | |
# Dealer turn | |
if dealer_total == 21 | |
puts "Sorry #{name}, you lost... Dealer hit Blackjack. :-(" | |
elsif player_total < 21 | |
while dealer_total < 17 | |
new_card = deck.pop | |
puts "Dealing new card to Dealer: #{new_card.reverse.join(' of ')}" | |
dealer_cards << new_card | |
dealer_total = calculate_total(dealer_cards) | |
puts "Dealer total is now #{dealer_total}." | |
puts | |
if dealer_total == 21 | |
puts "Sorry #{name}, you lost... Dealer hit Blackjack. :-(" | |
elsif dealer_total > 21 | |
puts "Dealer busted... YOU WON #{name.upcase}!" | |
end | |
end | |
end | |
# Compare hands | |
if player_total < 21 && dealer_total < 21 | |
puts "Dealer's cards: " | |
dealer_cards.each do |card| | |
puts "#{card.reverse.join(' of ')}" | |
end | |
puts | |
puts "#{name}'s cards: " | |
player_cards.each do |card| | |
puts "#{card.reverse.join(' of ')}" | |
end | |
puts | |
if dealer_total > player_total | |
puts "Sorry, #{name}, you lost. :-(" | |
elsif | |
player_total > dealer_total | |
puts "YOU WON #{name.upcase}!" | |
else | |
puts "It's a push." | |
end | |
end | |
puts | |
puts "Do you want to play again? (y/n)" | |
break if gets.chomp.downcase != 'y' | |
puts | |
end | |
puts "Thnaks for playing #{name}." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment