Skip to content

Instantly share code, notes, and snippets.

@joeainsworth
Last active January 11, 2016 22:12
Show Gist options
  • Save joeainsworth/6174f6e5af9f6fc06141 to your computer and use it in GitHub Desktop.
Save joeainsworth/6174f6e5af9f6fc06141 to your computer and use it in GitHub Desktop.
require 'pry'
module Hand
attr_accessor :hand
def initialize
@hand = []
end
end
class Deck
SUIT = %w(C D H S)
RANK = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A']
attr_accessor :cards
def initialize
@cards = []
create_deck
shuffle_deck
end
def create_deck
SUIT.each do |suit|
RANK.each do |rank|
card = Card.new(suit, rank)
cards << card
end
end
end
def shuffle_deck
cards.shuffle!
end
def deal_cards(player, dealer)
2.times do
player.hand << cards.shift
dealer.hand << cards.shift
end
end
end
class Card
attr_reader :suit, :rank
def initialize(suit, rank)
@suit = suit
@rank = rank
end
end
class Player
include Hand
attr_accessor :name
def initialize
set_name
super
end
def set_name
puts 'Please enter your name:'
answer = nil
loop do
answer = gets.chomp
break unless answer.nil?
puts 'Please enter a valid name.'
end
@name = answer
end
end
class Dealer
include Hand
attr_reader :name
def initialize
@name = 'Dealer'
super
end
end
class GameEngine
attr_accessor :deck, :player, :dealer
def initialize
@deck = Deck.new
@player = Player.new
@dealer = Dealer.new
end
def play
deck.deal_cards(player, dealer)
p player
p dealer
end
end
GameEngine.new.play
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment