Skip to content

Instantly share code, notes, and snippets.

@emperorliu
Last active August 29, 2015 14:04
Show Gist options
  • Save emperorliu/f338a616b4ccec912d6b to your computer and use it in GitHub Desktop.
Save emperorliu/f338a616b4ccec912d6b to your computer and use it in GitHub Desktop.
blackjack
# Blackjack
# first, we have a deck of 52 cards (how to organize data?)
# - suits value worth 10
# - aces value worth 11 or 1
# dealer is dealt 2 cards at random
# player is dealt 2 cards at
# player goes: choose hit or stay
# - if hit, dealt another card (until stay)
# - take sum of cards
# - if sum more than 21, player bust
# - if stay, take total sum of cards
# dealer goes: choose hit or stay
# - if sum of cards <= 17, hit
# - if sum more than 21, dealer bust
# - else stay
# - compare sum of player and dealer
require 'pry'
puts "Welcome to Blackjack"
# shuffle deck
suits = ["s", "d", "c", "h"]
numbers = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"]
cards = suits.product(numbers)
cards.shuffle!
# deal cards
player_hand =[]
dealer_hand = []
2.times do
player_hand.push(cards.shift)
end
2.times do
dealer_hand.push(cards.shift)
end
puts "Dealer is dealt cards #{dealer_hand}"
puts "You are dealt cards #{player_hand}"
# game play
puts "Would you like to hit(H) or stay(S)?"
player_choice = gets.chomp.downcase
while player_choice == "h"
player_hand.push(cards.shift)
puts "Your hand is now #{player_hand}. Would you like to hit or stay?"
player_choice = gets.chomp.downcase
end
if player_choice == "s"
puts "Your hand is final at #{player_hand}"
end
# count method
def player_count(hand)
sum = 0
# pass in [["c", 3], ["d", 8]..] trying to get 11
# if aces, = 1 or 11, JQK = 10
hand.each do |card|
sum += card[1]
end
end
p sum
player_count(player_hand)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment