Skip to content

Instantly share code, notes, and snippets.

@dominathan
Created January 8, 2015 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominathan/07c696f42af62f058fb4 to your computer and use it in GitHub Desktop.
Save dominathan/07c696f42af62f058fb4 to your computer and use it in GitHub Desktop.
class Deck
attr_reader :cards
def initialize
#ACES == 11 || 1, However, there are only 4 of them.
#FACE/10 CARDS - there are 16 of them total
#You can check the total number of cards by call @cards.count to make sure you have 52
aces = [11] * 4
face = [10] * 16
#You are currently shuffling only the face cards. You will need to wrap them all in parentheses to shuffle them all.
@cards = ((2..9).to_a * 4) + (aces) + (face).shuffle
end
def count
@cards.count
end
def shuffle
@cards = @cards.shuffle
end
def draw
@cards.shift
end
end
class Player
attr_accessor :cards, :dollars
def initialize
@cards = Deck.new
@dollars = 100
end
end
class Blackjack
def initialize
@user = Player.new
@dealer = Player.new
end
def play
while @user.dollars > 10 do
#Dealer is drawing 3 cards instead of 2
card_one = @dealer.cards.draw
card_two = @user.cards.draw
card_three = @dealer.cards.draw
card_four = @dealer.cards.draw
@user.dollars -= 10
#Seems you reverse the names of @dealer and @user, not a big deal to you if you remember.
#But would be to another person reading your code.
puts "Player shows [#{card_one}] and [#{card_three}] and the dealer shows [#{card_two}]"
puts "Do you want to stay or hit?"
stay_or_hit = gets.chomp.downcase
if stay_or_hit == "hit"
card_five = @user.cards.draw
puts "Player shows [#{card_five}], and [#{card_one}] and [#{card_three}]"
elsif stay_or_hit == "stay"
puts "OK, your total is (#{card_one} + #{card_three}), and the dealer has (#{card_two} + #{card_four})"
end
end
end
end
#I moved all of these to the end, outside of the BlackJack class.
puts "Let's play some Blackjack!"
puts "Would you like to play a hand?"
play_again = gets.chomp.downcase
if play_again == "yes"
Blackjack.new.play
else
puts "Thanks for playing!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment