Skip to content

Instantly share code, notes, and snippets.

@jmoon90
Created November 26, 2013 02:52
Show Gist options
  • Select an option

  • Save jmoon90/7652775 to your computer and use it in GitHub Desktop.

Select an option

Save jmoon90/7652775 to your computer and use it in GitHub Desktop.
require "pry"
##!/usr/bin/env ruby
## encoding: UTF-8
SUITS = ['♠', '♣', '♥', '♦']
VALUES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
def build_deck
deck = []
SUITS.each do |suit|
VALUES.each do |value|
deck.push(value + suit)
end
end
deck.shuffle
end
def symbols
@symbol_values = {
"J" => 10,
"Q" => 10,
"K" => 10,
"A" => 11
}
end
def num_of_players
puts "How many players are playing?"
num_players = gets.chomp
@total_players = {}
i = 1
(num_players.to_i+1).times do |player|
@total_players[i] = []
i += 1
end
end
def initial_hand(total_players)
2.times do
total_players.each do |player, card|
card << @current_deck.shift
if player == total_players.count
puts "Dealer was dealt #{card}"
else
puts "#{player} was dealt #{card}"
end
end
end
end
@player_sum = {}
def count_card_score(cards, player)
@total_score = 0
i=0
cards.count.times do
@total_score += cards[i][/[\A\d?]+/].to_i if cards[i][/[\A\d?]+/]
@total_score += @symbol_values[cards[i][/[a-zA-Z]/]] if cards[i][/[a-zA-Z]/]
i += 1
end
@player_sum[player] = @total_score
end
def display_current_score
@total_players.each do |player, cards|
count_card_score(cards, player)
binding.pry
if player == @total_players.count
puts "Dealer score: #{@total_score}"
if @total_score == 21
result(player)
end
else
puts "Player #{player} score: #{@total_score}"
if @total_score == 21
result(player)
end
end
end
end
def prompt_player
@total_players.each do |player, cards|
print "Hit or stand (H/S): "
answer = 0
answer = gets.chomp.downcase
hit_or_stand(answer, player)
end
end
def hit_or_stand(answer,player)
if answer == "h"
hits(player)
elsif answer == "s"
puts "You're standing at #{@player_sum[player]}"
else
puts "I'm just going to take your money"
end
end
def hits(player)
new_card = @current_deck.shift
puts "You are dealth #{new_card}"
@total_players[player] << new_card
count_card_score(@total_players[player], player)
result(player)
end
def busted(player)
puts "Player #{player}: You busted!"
exit
end
def twentyone_or_blackjack(player)
puts "Player #{player}: You win!"
end
def result(player)
if @player_sum[player] > 21
busted (player)
elsif @player_sum[player] = 21
twentyone_or_blackjack(player)
else
puts "You now have #{@player_sum[player]}"
prompt_player
end
end
def dealer
#Has to hit until 17
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment