Module: Cardlike

Defined in:
lib/cardlike.rb,
lib/cardlike/turn.rb,
lib/cardlike/score.rb,
lib/cardlike/version.rb

Defined Under Namespace

Classes: Card, Deck, Hand

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.begin_new_turn(*args) ⇒ Object

Call the block defined by Cardlike.define_turn. You can pass arguments to the block as well.



24
25
26
# File 'lib/cardlike/turn.rb', line 24

def self.begin_new_turn(*args)
  @turn.call(*args)
end

.card(name, &block) ⇒ Object

Create a new Card with the given name and block evaluated in the context of the Card. Returns the Card (that can also be accessed with the_card). You may use the Card DSL in the block.

Cardlike.card "Fire Monster"


102
103
104
105
106
107
# File 'lib/cardlike.rb', line 102

def self.card(name, &block)
  c = Card.create(name, &block)
  @cards ||= {}
  @cards[name] = c
  c
end

.clear_scoresObject

Clear all scores.



16
17
18
# File 'lib/cardlike/score.rb', line 16

def self.clear_scores
  @scores = {}
end

.deck(name, &block) ⇒ Object

Set up a new Deck with the given name. Returns the created Deck which can also be accessed with the_deck. Within the block you may use the Deck DSL.

Cardlike.deck "Poker Deck" do
  include_card "King of Diamonds"
  include_card "Three of Clubs"
end


39
40
41
42
43
44
45
# File 'lib/cardlike.rb', line 39

def self.deck(name, &block)
  @decks ||= {}
  d = Deck.new(name: name)
  d.instance_eval(&block) if block_given?
  @decks[name] = d
  d
end

.define_turn(&block) ⇒ Object

Define a turn block that can be run with Cardlike.begin_new_turn.

Cardlike.define_turn do |current_player|
  puts "#{current_player}'s turn"
end


9
10
11
# File 'lib/cardlike/turn.rb', line 9

def self.define_turn(&block)
  @turn = block
end

.game(&block) ⇒ Object

Optionally use this to wrap Cardlike DSL methods to avoid having to prefix them with Cardlike. This is mainly useful for large blocks.

Cardlike.game do
  type_of_card :playing_card { has :suit }
end

is the same as

Cardlike.type_of_card :playing_card { has :suit }


26
27
28
# File 'lib/cardlike.rb', line 26

def self.game(&block)
  self.class_eval(&block)
end

.hand(name, &block) ⇒ Object

Set up a new Hand with the given name. Returns the created Hand which can also be accessed with the_deck. Within the block you may use the Hand DSL.

Cardlike.hand "Poker Hand" do
  include_card "King of Diamonds"
  include_card "Three of Clubs"
end


56
57
58
59
60
61
62
# File 'lib/cardlike.rb', line 56

def self.hand(name, &block)
  @hands ||= {}
  d = Hand.new(name: name)
  d.instance_eval(&block) if block_given?
  @hands[name] = d
  d
end

.score(target) ⇒ Object

Increment the score for a target (usually a string or symbol).

Cardlike.score "player 1" # => sets the score to 1


7
8
9
10
11
# File 'lib/cardlike/score.rb', line 7

def self.score(target)
  @scores ||= {}
  @scores[target] ||= 0
  @scores[target] += 1
end

.scoresObject

Return the hash of the form {target: score}.



23
24
25
26
# File 'lib/cardlike/score.rb', line 23

def self.scores
  @scores ||= {}
  @scores
end

.set_score(target, value) ⇒ Object

Set the score for a target to a particular value.



43
44
45
46
# File 'lib/cardlike/score.rb', line 43

def self.set_score(target, value)
  @scores ||= {}
  @scores[target] = value
end

.the_card(name) ⇒ Object

Return a Card created by the card method. Also will return cards created by the custom new_ card methods (see type_of_card).

Cardlike.the_card("Six of Diamonds").name # => "Six of Diamonds"


90
91
92
93
# File 'lib/cardlike.rb', line 90

def self.the_card(name)
  @cards ||= {}
  @cards[name]
end

.the_deck(name) ⇒ Object

Return a Deck created by the deck method by name.

Cardlike.the_deck("Blackjack Deck").size # => 52


69
70
71
72
# File 'lib/cardlike.rb', line 69

def self.the_deck(name)
  @decks ||= {}
  @decks[name]
end

.the_hand(name) ⇒ Object

Return a Hand created by the hand method by name.

Cardlike.the_hand("Poker Hand").size # => 5


79
80
81
82
# File 'lib/cardlike.rb', line 79

def self.the_hand(name)
  @hands ||= {}
  @hands[name]
end

.the_score(target) ⇒ Object

Retrieve the score for a target (usually a string or symbol).

Cardlike.score "player 1" # => sets the score to 1
Cardlike.score "player 1" # => sets the score to 2
Cardlike.the_score "player 1" # => 2


35
36
37
38
# File 'lib/cardlike/score.rb', line 35

def self.the_score(target)
  @scores ||= {}
  @scores[target] || 0
end

.the_turnObject

Return the turn block set by Cardlike.define_turn.



16
17
18
# File 'lib/cardlike/turn.rb', line 16

def self.the_turn
  @turn
end

.type_of_card(name, &block) ⇒ Object

Create a new subclass of Card with its own properties as defined in the block. You may use the Card class DSL in the block. Automatically defines a method for creating new objects of that class, prefixed by new_. For example, a type_of_card :fun_card defines the method new_fun_card (which operates like the card method) that can be used in either a deck block or directly.

Cardlike.type_of_card :playing_card do
  has :value
  has :suit
end

You can then access the card with:

Cardlike.new_playing_card "Six of Spades" do
  value 6
  suit 'spades'
end

or:

Cardlike.deck "Poker Deck" do
  new_playing_card "Six of Spades" do
    value 6
    suit 'spades'
  end
end


138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/cardlike.rb', line 138

def self.type_of_card(name, &block)
  klass = Class.new(Card)
  klass_name = name.to_s.camelize
  name_underscored = name.to_s.downcase.underscore
  Object.const_set(klass_name, klass) if not Object.const_defined?(klass_name)
  c = Object.const_get(klass_name)
  c.class_eval(&block) if block_given?

  Deck.send(:define_method, "new_#{name_underscored}", lambda { |arg, &blk| card = c.create(arg, &blk); card.card_type = name_underscored.to_sym; self << card; card })
  self.class.send(:define_method, "new_#{name_underscored}", lambda { |arg, &blk| card = c.create(arg, &blk); card.card_type = name_underscored.to_sym; @cards ||= {}; @cards[arg] = card; card })

  c
end

.versionObject



10
11
12
# File 'lib/cardlike.rb', line 10

def self.version
  "#{self} version #{VERSION}"
end