Created
December 5, 2014 20:48
-
-
Save bramdemoor/46088972663b3628fe55 to your computer and use it in GitHub Desktop.
Estimations card game - simple first draft
This file contains 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 'logger' | |
$LOG = Logger.new(STDOUT) | |
$LOG.level = Logger::INFO | |
class GameRuleException < StandardError | |
end | |
class Card | |
RANKS = %w(2 3 4 5 6 7 8 9 10 J Q K A) | |
SUITS = %w(Spade Heart Club Diamond) | |
attr_accessor :rank, :suit | |
def initialize(id) | |
self.rank = RANKS[id % 13] | |
self.suit = SUITS[id % 4] | |
end | |
def to_s | |
"#{rank} #{suit}" | |
end | |
end | |
class Deck | |
attr_accessor :cards | |
def initialize | |
self.cards = (0..51).to_a.shuffle.collect { |id| Card.new(id) } | |
end | |
def first_card | |
self.cards[0] | |
end | |
def take | |
self.cards.shift | |
end | |
def has_cards | |
not self.cards.empty? | |
end | |
end | |
class Player | |
attr_accessor :name | |
attr_accessor :cards | |
def initialize(name) | |
@estimate = 0 | |
@cards = [] | |
self.name = name | |
end | |
def give_card(card) | |
@cards << card | |
end | |
def estimate(troef) | |
aantal_troeven = @cards.select {|c| c.suit == troef}.length | |
aantal_andere_hoge = @cards.select {|c| c.suit != troef and %w(10 J Q K A).include?(c.rank)}.length | |
@estimate = aantal_troeven + aantal_andere_hoge | |
$LOG.info("#{@name} estimates #{@estimate}") | |
end | |
end | |
class Game | |
def initialize(name) | |
@name = name | |
@players = [] | |
@game_status = :lobby | |
@troef = 'Spade' | |
@deck = Deck.new | |
end | |
def join(player_name) | |
new_player = Player.new(player_name) | |
@players << new_player | |
$LOG.info("Player #{player_name} joined") | |
new_player | |
end | |
def start | |
raise GameRuleException, "Game can only be started in Lobby state" if @game_status != :lobby | |
raise GameRuleException, "At least 2 players required to start the game!" if @players.length < 2 | |
@game_status = :playing | |
deal | |
$LOG.info("Game started") | |
estimate | |
speel_ronde | |
end | |
def deal | |
@troef = @deck.first_card.suit | |
$LOG.info("#{@troef} troef!") | |
player_index = 0 | |
while @deck.has_cards | |
p = @players[player_index] | |
p.give_card(@deck.take) | |
if player_index < @players.length - 1 | |
player_index += 1 | |
else | |
player_index = 0 | |
end | |
end | |
$LOG.info("Dealing done.") | |
@players.each do |p| | |
$LOG.info("#{p.name} cards: #{p.cards.to_s}") | |
end | |
end | |
def estimate | |
@players.each do |p| | |
p.estimate(@troef) | |
end | |
end | |
def speel_ronde | |
player_index = 0 | |
#en dees is voor de volgende keer! | |
end | |
end | |
class World | |
def initialize | |
@games = [] | |
end | |
def create_game(name) | |
new_game = Game.new(name) | |
@games << new_game | |
new_game | |
end | |
end | |
w = World.new | |
my_game = w.create_game('Mijn spel') | |
p1 = my_game.join('Jefke') | |
p2 = my_game.join('Joske') | |
p3 = my_game.join('Rita') | |
p4 = my_game.join('Guust') | |
my_game.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment