Skip to content

Instantly share code, notes, and snippets.

@alacritythief
Created September 3, 2014 20:44
Show Gist options
  • Save alacritythief/72b5a543a5391c7732d6 to your computer and use it in GitHub Desktop.
Save alacritythief/72b5a543a5391c7732d6 to your computer and use it in GitHub Desktop.
Andy's Blackjack Game
class Card
attr_reader :suit, :numeral, :card
def initialize
@numeral = %w[1 2 3 4 5 6 7 8 9 10 J Q K A].sample
@suit = %w[♦ ♠ ♥ ♣].sample
@card = "#{@numeral}#{@suit}"
return @card
end
def value
@values = {"1" => 1, "2" => 2, "3" => 3, "4" => 4, "5" => 5, "6" => 6, "7" => 7, "8" => 8, "9" => 9, "10" => 10, "J" => 10, "Q" => 10, "K" => 10, "A" => "ACE"}
@value = @values[@numeral]
end
end
class Deck
attr_reader :cards, :taken_card
def initialize(amount = 52)
@cards = []
amount.times { @cards << Card.new }
end
def take
@taken_card = @cards.delete @cards.sample
end
end
class Hand
attr_reader :cards, :total, :newcard
def initialize(deck)
@match_deck = deck
@cards = []
@total = []
2.times { @cards << @match_deck.take }
end
def hit
@newcard = @match_deck.take
@cards << @newcard
end
def total
ace_counter = 0
@hand_total = 0
@cards.each do |card|
if card.value == "ACE"
ace_counter += 1
@hand_total += 11
else
@hand_total += card.value
end
end
while @hand_total > 21 && ace_counter > 0
@hand_total -= 10
ace_counter -=1
end
return @hand_total
end
end
class Game
attr_reader :deck, :player, :dealer
def initialize
@deck = Deck.new
@player = Hand.new(@deck)
@dealer = Hand.new(@deck)
puts "Welcome to Blackjack!"
player_begin
player_deals
dealer_begin
dealer_deals
who_won?
end
def player_begin
puts "\n-[ PLAYER'S TURN ]-"
@player.cards.each do |card|
puts "Player was dealt #{card.card}"
end
puts "\nPlayer Score: #{@player.total}"
end
def player_deals
choice = nil
print "\nHit or Stand (H/S): "
choice = gets.chomp.downcase
while @player.total < 22 && choice != "s"
if choice == "h"
@player.hit
puts "\nPlayer was dealt #{@player.newcard.card}"
puts "Player score: #{@player.total}"
if @player.total < 22
print "\nHit or Stand (H/S): "
choice = gets.chomp.downcase
else
break
end
else
puts "\nPlease enter H or S"
print "\nHit or Stand (H/S): "
choice = gets.chomp.downcase
end
end
if @player.total > 21
puts "\nPLAYER BUSTS!"
return @player.total
else
puts "Player stands."
puts "Player score: #{@player.total}"
return @player.total
end
end
def dealer_begin
puts "\n-[ DEALER'S TURN ]-"
@dealer.cards.each do |card|
puts "Dealer was dealt #{card.card}"
end
puts "\nDealer Score: #{@dealer.total}"
end
def dealer_deals
while @dealer.total < 17
@dealer.hit
puts "Dealer was dealt #{@dealer.newcard.card}"
end
if @dealer.total > 21
puts "\nDEALER BUSTS!"
puts "Dealer score: #{@dealer.total}"
return @dealer.total
else
puts "Dealer stands."
puts "Dealer score: #{@dealer.total}"
return @dealer.total
end
end
def who_won?
puts "\n-----------------------------------"
puts "Player score: #{@player.total}"
puts "Dealer score: #{@dealer.total}"
puts "-----------------------------------"
if @dealer.total > 21 && @player.total <= 21
puts "\nPLAYER WINS! GAME OVER!"
elsif @dealer.total > 21 && @player.total > 21
puts "\nDRAW! GAME OVER!"
elsif @dealer.total == @player.total
puts "\nDRAW! GAME OVER!"
elsif @dealer.total < @player.total && @dealer.total < 22 && @player.total <= 21
puts "\nPLAYER WINS! GAME OVER!"
else
puts "\nPLAYER LOSES! GAME OVER!"
end
end
end
game = Game.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment