Skip to content

Instantly share code, notes, and snippets.

@dominathan
Created January 8, 2015 02:40
Show Gist options
  • Save dominathan/dbb2da9fa100df964614 to your computer and use it in GitHub Desktop.
Save dominathan/dbb2da9fa100df964614 to your computer and use it in GitHub Desktop.
puts "Welcome to Blackjack!"
class Blackjack
def initialize
@player = Player.new
end
def play
#When you call @player.cards.money, you are asking Ruby to determine how much money the cards have.
# You just Ruby to determine how much money the player has.
while @player.cards.money > 10 do
#When you look at class Deck (around line 45), you no longer have a method that is "def draw", so you cannot call cards.draw
#You do, however, have a method call cards.deal and cards.hit. Try those!
card_one = @player.cards.draw
card_two = @player.cards.draw
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
#Your new (card_three)card has never been instantiated.
#So you are trying to add #{card_three}, but Ruby has no idea what card three is.
#I would suggest making a method that counts all the cards in the hand of a player.
#If you remember the method '.reduce', it could be something like this.
#(but the method needs to be moved to another spot (outside of def play))
#def sum_player_hand(player_hand)
#player_hand.reduce(:+)
#end
#player_hand is an array you will give the method, and it will sum all the elements in the array.
puts "Your new total is #{sum_player_hand(HAND_YOU_GIVE_IT)}"
else
puts "You chose to stay! Your total is #{sum_player_hand(HAND_YOU_GIVE_IT)}. Thanks for playing."
end
end
end
#You need something, so if @player wins/loses, and plays again..
#
# @player.money -= 10
#
#@player must lose $10.
#Maybe every time you call the method 'play', @player.money -= 10.
end
#Player looks good.
class Player
attr_accessor :cards, :money, :total
def initialize
@cards = Deck.new
@money = 100
@total = 0
end
end
#Cards looks good except you should think about the || operator, so that an ace = [11] OR [1]
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
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment