card_nine 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e44ebfb47bb898a6da17933023b5c828c79bef8
4
- data.tar.gz: 4510d3a7a56a91e9089044d81f01f9f4996d5a9c
3
+ metadata.gz: d801904950c45689dbe83317c5fcd4f025f36b9e
4
+ data.tar.gz: 9395b4a5fa1bc8672e94194846a2d03c30f4bc0e
5
5
  SHA512:
6
- metadata.gz: a7c30fd41bee2615f58b4c3d11c1504ca73d91e7367228e0137632ebb8d8941a875786132f18fb0498fd8f44f92ab0a9a5b42e97168a2b0012e2c897f976825e
7
- data.tar.gz: 00915ec32bfe7681c953d7dfaf2789acb403bf6315efc02d8ae4b2b40b6d1fc6909671fe0628112f00a3280728809a26f663ce9ebc670e09aebab3d285337d89
6
+ metadata.gz: 76354cab073b3b811ce4670ce2f4ac490e03977ee1b13fd54495c6bae822585c8322f80ecc4a30bed323846edaea9a03e5d147662146b233e138002dac998d51
7
+ data.tar.gz: abf13a96403a2a7649d0dc10afd73f298ff8c5ac3883cf20dff4668de4f54322181899c7611e68d3a87e336cf7f87f3d8163ced64574c236927f2a253eaa3843
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.1
1
+ 0.6.0
@@ -3,6 +3,7 @@ require 'card_nine/deck'
3
3
 
4
4
  module CardNine
5
5
  module Cards
6
+ # A class that represents the typical playing card from a 52 card deck.
6
7
  class PlayingCard
7
8
  # @note implements comparable
8
9
  include Comparable
@@ -1,3 +1,4 @@
1
+ require 'card_nine/card'
1
2
  module CardNine
2
3
  module Cards
3
4
  # The simplest card possible. A Nothing added class with Card included
@@ -1,17 +1,29 @@
1
1
  module CardNine
2
2
  class Dealer
3
3
 
4
+ # @!attribute [rw] deck
5
+ # @return [CardNine::Deck]
6
+ # @!attribute [rw] locations
7
+ # @return [Array<Symbol>]
8
+ # @!attribute [rw] players
9
+ # @return [Hash<Symbol => Proc>]
4
10
  attr_reader :deck, :locations, :stages
5
11
 
12
+ # @param [CardNine::Deck] deck
13
+ # @param [Array<Symbol>] locations
14
+ # @param [stages]
6
15
  def initialize(deck, locations, stages = {})
7
16
  @deck = deck
8
17
  @locations = locations
9
18
  @stages = stages
10
19
  end
11
20
 
21
+ # Create a CardNine::Table
22
+ # @param [Array<String,Object>] players
23
+ # @return [CardNine::Table] using deck, players, locations and stages
12
24
  def deal(players)
13
- CardNine::Table.new(deck.shuffle, players, locations, stages)
25
+ locs = (locations + players).reduce({}) { |h, e| h[e] = []; h }
26
+ CardNine::Table.new(deck.shuffle, locs, stages)
14
27
  end
15
-
16
28
  end
17
29
  end
@@ -4,8 +4,6 @@ require 'card_nine/dealer'
4
4
  module CardNine
5
5
  module Dealers
6
6
  class Simple < CardNine::Dealer
7
-
8
-
9
7
  end
10
8
  end
11
9
  end
@@ -23,6 +23,8 @@ module CardNine
23
23
  # wager(:resolve, hand)
24
24
  class TexasHoldem < CardNine::Dealer
25
25
 
26
+ # A hash of stages
27
+ # @return [Hash<Symbol => Proc>]
26
28
  STAGES = {
27
29
  deal_hole_cards: ->(t) { t.deal_players(2) },
28
30
  flop: ->(t) { t.discard; t.deal(3, to: :community) },
@@ -31,11 +33,10 @@ module CardNine
31
33
  fold: ->(t, p) { t.remove_player(p) }
32
34
  }
33
35
 
36
+ # create a new dealer using a PlayingCard Deck
34
37
  def initialize
35
38
  super CardNine::Cards::PlayingCard.deck, [:community], STAGES
36
39
  end
37
-
38
40
  end
39
-
40
41
  end
41
42
  end
@@ -3,6 +3,8 @@ module CardNine
3
3
  class Deck
4
4
  include Virtus.model
5
5
 
6
+ # @!attribute [rw] cards
7
+ # @return [Array<CardNine::Card>]
6
8
  attribute :cards, Array, default: []
7
9
 
8
10
  # @!attribute [rw] rng
@@ -1,48 +1,42 @@
1
1
  module CardNine
2
2
  class Table
3
- attr_reader :shoe, :players, :hands, :locations, :discards, :stages
3
+ attr :shoe, :discards, :stages
4
4
 
5
- def initialize(shoe, players, locations, stages = {})
6
- @shoe = shoe
7
- @players = players
5
+ def initialize(shoe, locations, stages: {})
6
+ @shoe = shoe
7
+ @discards = []
8
8
  @locations = locations
9
- @discards = []
10
- @stages = stages
11
- @hands = { shoe: @shoe, discards: @discards }
12
- (@players + @locations).each { |l| register_loc(l) }
9
+ @stages = stages
10
+ register_location(:shoe, shoe)
11
+ register_location(:discards, @discards)
13
12
  end
14
13
 
15
- # @return Array all locations that cards can be dealt to
16
- def locs
17
- @hands.keys
14
+ def register_location(loc_name, loc_container)
15
+ @locations[loc_name] = loc_container
16
+ end
17
+
18
+ def locations
19
+ @locations.keys
18
20
  end
19
21
 
20
22
  # @param [String,Symbol] loc which location to see
21
23
  # @return Array[Card] cards for that loc
22
24
  def cards_for(loc)
23
- @hands[loc]
25
+ @locations[loc]
24
26
  end
25
27
 
26
-
27
28
  def deal(count = 1, from: :shoe, to:)
28
29
  cards_for(to).push(*cards_for(from).pop(count))
29
30
  end
30
31
 
31
- def deal_each_player(count)
32
- count.times { @players.each { |p| deal(to: p) } }
32
+ def deal_each_matching(count = 1, &blk)
33
+ count.times { match_locations(&blk).each { |p| deal(to: p) } }
33
34
  end
34
35
 
35
- def deal_each_location(count)
36
- count.times { @locations.each { |p| deal(to: p) } }
36
+ def match_locations(&blk)
37
+ locations.select { |e| blk.call(e, cards_for(e)) }
37
38
  end
38
39
 
39
- def deal_stage(stage_name)
40
- @stages[stage_name].call(self)
41
- end
42
40
 
43
- private
44
- def register_loc(loc)
45
- @hands[loc] = []
46
- end
47
41
  end
48
42
  end
@@ -3,18 +3,17 @@ require 'card_nine/dealers/simple'
3
3
 
4
4
  describe CardNine::Dealers::Simple do
5
5
  let(:card_names) { %w{Batman Nightwing Batgirl Robin} }
6
- let(:cards) { card_names.map { |c| CardNine::Cards::SimpleCard.new(c) } }
7
6
  let(:deck) { CardNine::Deck.new(cards: card_names) }
8
7
 
9
8
  let(:players) { %w{Tim Dick Jason} }
10
- let(:locations) { [:batcave] }
11
- let(:expected_attributes) {
9
+ let(:locations) { { batcave: [] } }
10
+ let(:expected_attributes) do
12
11
  {
13
12
  deck: deck,
14
13
  locations: locations
15
14
  }
16
- }
15
+ end
17
16
  subject { CardNine::Dealers::Simple.new(deck, locations) }
18
-
19
-
17
+ it { is_expected.to be_a described_class }
18
+ it { is_expected.to be_a_kind_of CardNine::Dealer }
20
19
  end
@@ -2,78 +2,81 @@ require 'rspec'
2
2
  require 'card_nine/table'
3
3
 
4
4
  describe CardNine::Table do
5
- let(:card_names) { %w{Batman Nightwing Batgirl Robin} }
6
- let(:cards) { card_names.map { |c| CardNine::Cards::SimpleCard.new(c) } }
5
+ let(:card_names) { %w{Zorro Batman Batgirl Robin} }
7
6
  let(:shoe) { CardNine::Deck.new(cards: card_names).cards }
7
+ let(:locations) do
8
+ h = {}
9
+ h[:batplane] = []
10
+ h
11
+ end
12
+ subject { described_class.new(shoe, locations) }
8
13
 
9
- let(:players) { %w{Tim Dick Jason} }
10
- let(:locations) { [:batcave] }
11
- let(:expected_locs) { [:shoe, :discards] + locations + players }
12
- let(:expected_attributes) {
13
- {
14
- shoe: shoe,
15
- players: players,
16
- locations: locations,
17
- stages: {}
18
- }
19
- }
20
- subject { CardNine::Table.new(shoe, players, locations) }
21
-
22
- it { is_expected.to be_a CardNine::Table }
23
- it { is_expected.to have_attributes expected_attributes }
24
- it { expect(subject.locs).to include *expected_locs }
25
- it { expect(subject.discards).to eq [] }
26
- it { expect(subject.cards_for(:batcave)).to eq [] }
27
- it { expect(subject.cards_for('Tim')).to eq [] }
14
+ it { is_expected.to be_a described_class }
15
+ it { expect(subject.locations).to eq [:batplane, :shoe, :discards] }
28
16
 
29
- describe '.deal(count = 1, to:)' do
30
- let(:table) { CardNine::Table.new(shoe, players, locations) }
31
- subject { table.deal(to: :batcave); table }
32
- it { expect(subject.cards_for(:batcave)).to eq ['Robin'] }
33
- it { expect(subject.shoe).to eq %w{Batman Nightwing Batgirl } }
34
- context 'with a count of 2' do
35
- subject { table.deal(2, to: :batcave); table }
36
- it { expect(subject.cards_for(:batcave)).to eq ['Batgirl', 'Robin'] }
37
- it { expect(subject.shoe).to eq ['Batman', 'Nightwing'] }
17
+ describe 'easy stuff' do
18
+ it 'returns the container for a given location' do
19
+ subject.register_location(:tim, :drake_manor)
20
+ expect(subject.cards_for(:tim)).to eq :drake_manor
38
21
  end
39
- context 'with a from location' do
40
- subject do
41
- table.deal(2, to: :batcave)
42
- table.deal(from: :batcave, to: 'Tim')
43
- table
44
- end
45
- it { expect(subject.cards_for(:batcave)).to eq ['Batgirl'] }
46
- it { expect(subject.cards_for('Tim')).to eq ['Robin'] }
22
+ it 'registers locations' do
23
+ subject.register_location(:tim, [:drake_manor])
24
+ expect(subject.cards_for(:tim)).to eq [:drake_manor]
25
+ end
26
+ it 'reports locations' do
27
+ subject.register_location(:tim, [:drake_manor])
28
+ expect(subject.locations).to eq [:batplane, :shoe, :discards, :tim]
47
29
  end
48
30
  end
49
31
 
50
- describe '.deal_each_player(grp, count)' do
51
- let(:table) { CardNine::Table.new(shoe, players, locations) }
52
- subject do
53
- table.deal_each_player(1)
54
- table
32
+ describe '.deal(count = 1, from: :shoe, to:' do
33
+ context 'with default count' do
34
+ subject do
35
+ s = described_class.new(shoe, locations)
36
+ s.deal(to: :batplane)
37
+ s
38
+ end
39
+ it { expect(subject.shoe.count).to eq 3 }
40
+ it { expect(subject.cards_for(:batplane).first).to eq 'Robin' }
55
41
  end
56
- it { expect(subject.shoe).to eq ['Batman'] }
57
- it { expect(subject.cards_for('Tim')).to eq ['Robin'] }
58
-
59
- context 'with a count over 1' do
60
- let(:card_names) { %w{Batman Nightwing Batgirl Robin Spoiler Huntress} }
61
- subject { table.deal_each_player(2); table }
62
- it { expect(subject.shoe).to eq [] }
63
- it { expect(subject.cards_for('Dick')).to eq ['Spoiler', 'Nightwing'] }
42
+ context 'with given count' do
43
+ subject do
44
+ s = described_class.new(shoe, locations)
45
+ s.deal(2, to: :batplane)
46
+ s
47
+ end
48
+ it { expect(subject.shoe.count).to eq 2 }
49
+ it { expect(subject.cards_for(:batplane).first).to eq 'Batgirl' }
50
+ it { expect(subject.cards_for(:batplane).last).to eq 'Robin' }
64
51
  end
65
52
  end
53
+ describe '.deal_each_matching(count = 1){|location, container|}' do
54
+ context 'with default count' do
55
+ subject do
56
+ locations['Alfred'] = []
57
+ locations['James'] = []
58
+ s = described_class.new(shoe, locations)
59
+ s.deal_each_matching { |location, container| location.is_a? String }
60
+ s
61
+ end
62
+ it { expect(subject.shoe.count).to eq 2 }
63
+ it { expect(subject.cards_for('Alfred').first).to eq 'Robin' }
64
+ it { expect(subject.cards_for('James').first).to eq 'Batgirl' }
65
+ it { expect(subject.discards).to eq [] }
66
+ it { expect(subject.cards_for(:batplane)).to eq [] }
66
67
 
67
- describe '.deal_stage(stage_name)' do
68
- it 'should yield to the named proc' do
69
- canary = double('canary')
70
- expect(canary).to receive(:call)
71
- stages = { deal_1: canary }
72
- table = CardNine::Table.new(shoe, players, locations, stages)
73
- table.deal_stage(:deal_1)
74
68
  end
75
-
69
+ context 'with given count' do
70
+ subject do
71
+ s = described_class.new(shoe, locations)
72
+ s.deal_each_matching(2) { |l, c| l.is_a?(Symbol) && l != :shoe }
73
+ s
74
+ end
75
+ it { expect(subject.shoe.count).to eq 0 }
76
+ it { expect(subject.cards_for(:discards)).to eq ['Batgirl', 'Zorro'] }
77
+ it { expect(subject.cards_for(:batplane)).to eq ['Robin', 'Batman'] }
78
+ end
76
79
  end
80
+ end
77
81
 
78
82
 
79
- end
@@ -5,17 +5,14 @@ require 'card_nine/dealers/simple'
5
5
  describe CardNine do
6
6
 
7
7
  describe 'texas holdem dealer' do
8
-
9
8
  end
10
9
 
11
10
  describe 'Simple dealer' do
12
11
  let(:deck) { CardNine::Deck.new(cards: %w{Batman Nightwing Batgirl Robin}) }
13
12
 
14
13
  let(:players) { %w{Tim Dick Jason} }
15
- subject { CardNine::Dealers::Simple.new(deck, [:batcave]) }
14
+ subject { CardNine::Dealers::Simple.new(deck, { batcave: [] }) }
16
15
 
17
16
  it { is_expected.to be_a CardNine::Dealers::Simple }
18
17
  end
19
-
20
-
21
18
  end
data/spec/spec_helper.rb CHANGED
@@ -16,7 +16,7 @@ RSpec.configure do |config|
16
16
 
17
17
  config.filter_run :focus
18
18
  config.run_all_when_everything_filtered = true
19
- config.filter_run_excluding :rewrite => true
19
+ config.filter_run_excluding rewrite: true
20
20
  config.order = :random
21
21
  # rspec-mocks config goes here. You can use an alternate test double
22
22
  # library (such as bogus or mocha) by changing the `mock_with` option here.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: card_nine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott M Parrish
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-20 00:00:00.000000000 Z
11
+ date: 2014-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus
@@ -235,7 +235,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
235
235
  version: '0'
236
236
  requirements: []
237
237
  rubyforge_project:
238
- rubygems_version: 2.4.2
238
+ rubygems_version: 2.4.3
239
239
  signing_key:
240
240
  specification_version: 4
241
241
  summary: Game Dev Toolbox. Cards