Created
February 3, 2012 06:55
-
-
Save krainboltgreene/1728585 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ostruct' | |
module Person | |
attr_accessor :name, :hand | |
NAMES = %w[ | |
James | |
William | |
Smith | |
Alexander | |
] | |
end | |
class StateError < StandardError | |
end | |
class Game | |
attr_accessor :deck, :dealer, :players | |
def initialize | |
@dealer = Dealer.new(self) | |
@players = (1..5).map { Player.new game: self } | |
dealer.start | |
end | |
def restart | |
[@dealer, @players].flatten.each { |person| person.hand = [] } | |
dealer.start | |
end | |
end | |
class Dealer | |
include Person | |
attr_accessor :decks, :game | |
def initialize(game) | |
@game = game | |
@decks = (1..3).map { Deck.new(self) } | |
@name = NAMES.sample | |
end | |
def open_deck | |
if decks.any? | |
decks.pop | |
else | |
raise StateError | |
end | |
end | |
def start | |
game.deck = open_deck | |
game.deck.shuffle | |
game.players.each do |player| | |
hand = (1..7).map { game.deck.cards.pop } | |
deal player, hand | |
end | |
end | |
def deal(player, draw) | |
player.hand = draw | |
end | |
end | |
class Deck | |
TYPES = %w[club heart spade diamond].map(&:to_sym) | |
VALUES = %w[1 2 3 4 5 6 7 8 9 10 11 12 13].map(&:to_i) | |
attr_accessor :cards, :dealer | |
def initialize(dealer) | |
@dealer = dealer | |
@cards = [] | |
for type in TYPES | |
for value in VALUES | |
@cards << Card.new(type: type, value: value, holder: self) | |
end | |
end | |
end | |
def shuffle | |
cards.shuffle! | |
end | |
def size | |
cards.size | |
end | |
end | |
class Player < OpenStruct | |
include Person | |
def initialize(table) | |
super(table) | |
@name = NAMES.sample | |
end | |
def play(index) | |
hand[index] | |
end | |
end | |
class Card < OpenStruct | |
def name | |
case value | |
when 1 then "Ace" | |
when 2 then "Two" | |
when 3 then "Three" | |
when 4 then "Four" | |
when 5 then "Five" | |
when 6 then "Six" | |
when 7 then "Seven" | |
when 8 then "Eight" | |
when 9 then "Nine" | |
when 10 then "Ten" | |
when 11 then "Jack" | |
when 12 then "Queen" | |
when 13 then "King" | |
end | |
end | |
def to_s | |
"#{name} of #{type}s" | |
end | |
end | |
game = Game.new | |
game.players.each do |player| | |
puts player.name | |
puts player.hand.map(&:to_s).join ", " | |
end |
I'm thinking in the future doing card total sums.
…On Feb 3, 2012, at 6:24 AM, Ricardo ***@***.*** wrote:
would something along these lines be useful?
class Card < OpenStruct
NAMES = %w{Ace Two Three Four Five Six Seven Eight Nine Ten Jack Queen King}
def name
NAMES[value]
end
end
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1728585
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
would something along these lines be useful?