Skip to content

Instantly share code, notes, and snippets.

@emperorliu
Created August 15, 2014 02:25
Show Gist options
  • Save emperorliu/d52c66e2204da54d8354 to your computer and use it in GitHub Desktop.
Save emperorliu/d52c66e2204da54d8354 to your computer and use it in GitHub Desktop.
# Blackjack in OO
# major nouns/verbs in blackjack:
# - deck
# - deal
#-scramble
# - card
# - suit / face value
# - dealer
# - hit/stay
# - name
# - total
# - player
# - hit/stay
# - name
# - total
# - gameplay
# - compare total
# first, we have a deck of 52 cards (how to organize data?)
# - suits value worth 10
# - aces value worth 11 or 1
# dealer is dealt 2 cards at random
# player is dealt 2 cards at
# player goes: choose hit or stay
# - if hit, dealt another card (until stay)
# - take sum of cards
# - if sum more than 21, player bust
# - if stay, take total sum of cards
# dealer goes: choose hit or stay
# - if sum of cards <= 17, hit
# - if sum more than 21, dealer bust
# - else stay
# - compare sum of player and dealer
require 'rubygems'
require 'pry'
class Deck
attr_accessor :cards
def initialize
@cards = []
['Hearts','Diamonds','Clubs','Spades'].each do |suit|
[ 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A'].each do |value|
@cards<<Card.new(suit,value)
end
end
scramble!
end
def scramble!
cards.shuffle!
end
def deal_one
cards.pop
end
def size
cards.size
end
end
class Card
attr_accessor :suit, :value
def initialize(s,v)
@suit = s
@value = v
end
def to_s
"This is the #{value} of #{suit}"
end
end
class Dealer
include Hand
attr_accessor :name, :cards
def initialize
@name = "Dealer"
@cards = []
end
end
class Player
include Hand
attr_accessor :name, :cards
def initialize(n)
@name = n
@cards = []
end
end
module Hand
def show_hand
puts " #{name}'s hand "
cards.each do |card|
puts "=> #{card}"
end
puts "=> Total: #{total}"
end
def total
"some total"
end
def add_card(new_card)
cards << new_card
end
end
# class Game
# attr_accessor :dealer, :player, :deck
# def initialize
# @dealer = Dealer.new
# @player = Player.new("Jeff")
# @deck = Deck.new
# end
# def play
# end
# end
deck = Deck.new
player = Player.new("Jeff")
player.add_card(deck.deal_one)
player.add_card(deck.deal_one)
player.show_hand
dealer = Dealer.new
dealer.add_card(deck.deal_one)
dealer.add_card(deck.deal_one)
dealer.show_hand
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment