Skip to content

Instantly share code, notes, and snippets.

@dominathan
Created January 8, 2015 03:58
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/d1d75f4649e9bc0c7eb2 to your computer and use it in GitHub Desktop.
Save dominathan/d1d75f4649e9bc0c7eb2 to your computer and use it in GitHub Desktop.
puts "Welcome to Blackjack!"
class BlackJack
def initialize
@player = Player.new
end
def play
while @player.money > 10 do
#I changed @player.cards.money to what's above. is it correct?
#CORRECT YOU CAN TELL BY ADDING puts @player.money to see how much @player has
@player.money -= 10
puts "You have $#{@player.money}"
#where is the correct place to put the line above? Where am I actually calling play
#PLAY IS DOWN AT THE VERY BOTTOM. I ADDED IT. To run the program, just run ruby 'your_file_name.rb' in terminal
card_one = @player.cards.deal
card_two = @player.cards.deal
card_three = @player.cards.hit
#is this an ok way to instantiate card_three?
#YES, HOWEVER, you might not want to instatiated it until the users says HIT
#ALSO - @player.cards.deal --- this returns two cards so you have four cards total before you call card_three.
puts "Your total is [#{card_one} + #{card_two}]"
puts "Would you like another card? (y or n)?"
user_answer = gets.chomp
if user_answer.downcase == y
puts "Okay, here's your new card. Good luck"
@player.cards.hit
puts "Your new total is [#{card_one} + #{card_two} + {card_three}]"
else
puts "You chose to stay! Thanks for playing."
end
end
end
#I know I still need to set it up for the game to continue...
def sum_player_hand(player_hand)
player_hand.reduce(:+)
end
end
class Player
attr_accessor :cards, :money, :total
def initialize
@cards = Deck.new
@money = 100
@total = 0
end
end
class Deck
attr_reader :cards
def initialize
faces = [10] * 4
aces = [11] * 4
@cards = ((2..9).to_a * 4).concat(faces).concat(aces).shuffle
end
def total
@cards.count
end
def shuffle
@cards = @cards.shuffle
end
def deal
@cards.shift(2)
end
def hit
@cards.shift
end
end
BlackJack.new.play
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment