cards_lib 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/CODE_OF_CONDUCT.md +3 -0
- data/Guardfile +4 -0
- data/README.md +12 -1
- data/cards_lib.gemspec +2 -1
- data/lib/cards_lib/card.rb +11 -1
- data/lib/cards_lib/ranker.rb +14 -9
- data/lib/cards_lib/standard/evaluators/blackjack_evaluator.rb +38 -0
- data/lib/cards_lib/standard/evaluators.rb +8 -0
- data/lib/cards_lib/standard/rankers/blackjack_ranker.rb +30 -0
- data/lib/cards_lib/standard/rankers.rb +2 -0
- data/lib/cards_lib/standard/rules/{poker.rb → poker_rule.rb} +1 -1
- data/lib/cards_lib/standard/rules.rb +1 -1
- data/lib/cards_lib/standard.rb +6 -2
- data/lib/cards_lib/version.rb +1 -1
- data/lib/cards_lib.rb +1 -1
- metadata +24 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 116128e753df7d33bdbbf92dff21cc56f44e2712
|
4
|
+
data.tar.gz: eb7424abd1a29cd3d0940bcc5eca8e7e3d8303f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 786ec4e653b8bc177b9f34800960aeb76410c2a28c12595d353426fb71326f85bc55f5922b30163c71315313b68982af9ac8e820af71ffad408134677749b125
|
7
|
+
data.tar.gz: 602402ad77e0bc9318b8be79bc7d82ab2b633f303c24dc2e5781c926e0caf9b97746876bb35e2fb6b180176c6e7f383ef03638b2c81fc1fce3ba421d1298ea8f
|
data/.gitignore
CHANGED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,3 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
For you, brethren, have been called to liberty; only do not use liberty as an opportunity for the flesh, but through love serve one another. For all the law is fulfilled in one word, even in this: “You shall love your neighbor as yourself.” But if you bite and devour one another, beware lest you be consumed by one another! - [Galatians 5:13-15](https://www.biblegateway.com/passage/?search=Gal.5.13-Gal.5.15&version=NKJV)
|
data/Guardfile
CHANGED
@@ -19,6 +19,10 @@ guard :minitest do
|
|
19
19
|
# with Minitest::Unit
|
20
20
|
watch(%r{^test/(.*)\/?(.*)_test\.rb$})
|
21
21
|
watch(%r{^lib/cards_lib/([^/]+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
|
22
|
+
watch(%r{^lib/cards_lib/standard/([^/]+)\.rb$}) { |m| "test/standard/#{m[1]}_test.rb" }
|
23
|
+
watch(%r{^lib/cards_lib/standard/evaluators/([^/]+)\.rb$}) { |m| "test/standard/evaluators/#{m[1]}_test.rb" }
|
24
|
+
watch(%r{^lib/cards_lib/standard/rules/([^/]+)\.rb$}) { |m| "test/standard/rules/#{m[1]}_test.rb" }
|
25
|
+
watch(%r{^lib/cards_lib/standard/rankers/([^/]+)\.rb$}) { |m| "test/standard/rankers/#{m[1]}_test.rb" }
|
22
26
|
watch(%r{^test/minitest_helper\.rb$}) { 'test' }
|
23
27
|
watch(%r{^lib/cards_lib/card.rb$}) { 'test' }
|
24
28
|
|
data/README.md
CHANGED
@@ -15,6 +15,15 @@ I'm willing to bet you that there is no other Ruby card game library
|
|
15
15
|
out there as easy to use, as well thought out, and as simple to
|
16
16
|
understand as mine. Come on, I dare you to use it and review it! ;-)
|
17
17
|
|
18
|
+
##Scope
|
19
|
+
|
20
|
+
The scope of this project aims to be a game agnostic card management
|
21
|
+
and evaluation system. Game logic itself will not be provided with
|
22
|
+
this library. The creation, grouping, comparison, and evaluation of
|
23
|
+
cards is the sole purpose of this gem.
|
24
|
+
|
25
|
+
Feel free to contribute or raise suggestions via the issue tracker.
|
26
|
+
I welcome participation of all kinds! :-)
|
18
27
|
|
19
28
|
##Install
|
20
29
|
|
@@ -61,7 +70,9 @@ CardsLib::IsSet.verify(
|
|
61
70
|
|
62
71
|
* In **lib/cards_lib/standard.rb** there are some basic deck templates.
|
63
72
|
|
64
|
-
* In **lib/cards_lib/standard/rules/
|
73
|
+
* In **lib/cards_lib/standard/rules/poker_rule.rb** there are some working Poker hand verification methods.
|
74
|
+
|
75
|
+
* In **lib/cards_lib/standard/evaluators/blackjack_evaluator.rb** there is a Blackjack hand worth evaluation tool. See the **test/standard/evaluators/blackjack_evaluator_test.rb** for examples.
|
65
76
|
|
66
77
|
##License
|
67
78
|
|
data/cards_lib.gemspec
CHANGED
@@ -21,9 +21,10 @@ Gem::Specification.new do |spec|
|
|
21
21
|
|
22
22
|
spec.required_ruby_version = '>= 2.0.0'
|
23
23
|
|
24
|
-
spec.add_development_dependency "bundler", "~> 1.
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.11.0"
|
25
25
|
spec.add_development_dependency "rake", "~> 10.4"
|
26
26
|
spec.add_development_dependency "color_pound_spec_reporter", "~> 0.0.5"
|
27
27
|
spec.add_development_dependency "minitest", "~> 5.8.1"
|
28
28
|
spec.add_development_dependency "minitest-reporters", "~> 1.1.4"
|
29
|
+
spec.add_development_dependency "minitest-byebug", "~> 0.0.3"
|
29
30
|
end
|
data/lib/cards_lib/card.rb
CHANGED
@@ -8,7 +8,7 @@ module CardsLib
|
|
8
8
|
@face = face_from_rank_and_suit(@rank, @suit) if face.is_a? Hash
|
9
9
|
|
10
10
|
@face ||= face
|
11
|
-
@ranker = ranker.new(rank)
|
11
|
+
@ranker = ranker.new(self.rank)
|
12
12
|
end
|
13
13
|
|
14
14
|
def face
|
@@ -23,14 +23,24 @@ module CardsLib
|
|
23
23
|
@rank || @face[0]
|
24
24
|
end
|
25
25
|
|
26
|
+
def value
|
27
|
+
@ranker.ranker
|
28
|
+
end
|
29
|
+
|
26
30
|
def pair?(other)
|
27
31
|
self.rank == other.rank
|
28
32
|
end
|
29
33
|
|
34
|
+
# equal by both rank and suit
|
30
35
|
def ==(other)
|
31
36
|
pair?(other) && self.suit == other.suit
|
32
37
|
end
|
33
38
|
|
39
|
+
# equal by value
|
40
|
+
def eql?(other)
|
41
|
+
value == other.value
|
42
|
+
end
|
43
|
+
|
34
44
|
def <=>(other)
|
35
45
|
@ranker.<=>(other)
|
36
46
|
end
|
data/lib/cards_lib/ranker.rb
CHANGED
@@ -1,21 +1,26 @@
|
|
1
1
|
module CardsLib
|
2
2
|
class Ranker
|
3
3
|
include Comparable
|
4
|
-
attr :rank, :
|
5
|
-
def initialize
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
attr :rank, :rank_lookup
|
5
|
+
def initialize(rank = nil, ranks = nil, rank_lookup = nil)
|
6
|
+
@rank, @ranks, @rank_lookup = rank, ranks, rank_lookup
|
7
|
+
end
|
8
|
+
|
9
|
+
def ranks
|
10
|
+
@ranks || Standard::RANKS
|
11
|
+
end
|
12
|
+
|
13
|
+
def ranker(rank_face = @rank)
|
14
|
+
@rank_lookup ? @rank_lookup.(rank_face) :
|
15
|
+
ranks.index(rank_face).to_i + 1
|
11
16
|
end
|
12
17
|
|
13
18
|
def <=>(item)
|
14
|
-
|
19
|
+
ranker(self.rank) <=> ranker(item.rank)
|
15
20
|
end
|
16
21
|
|
17
22
|
def sequential?(item)
|
18
|
-
(
|
23
|
+
(ranker(self.rank) - ranker(item.rank)).abs == 1
|
19
24
|
end
|
20
25
|
end
|
21
26
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module CardsLib
|
2
|
+
module Standard
|
3
|
+
module Evaluators
|
4
|
+
class BlackjackEvaluator
|
5
|
+
def initialize(cards)
|
6
|
+
@cards = cards
|
7
|
+
@reduced_ace_count = 0
|
8
|
+
end
|
9
|
+
|
10
|
+
def worth
|
11
|
+
@worth ||= @cards.map(&:value).inject(:+)
|
12
|
+
if @worth > 21
|
13
|
+
attempt = reduce_by_ace
|
14
|
+
until @worth < 22 || !attempt
|
15
|
+
attempt = reduce_by_ace
|
16
|
+
end
|
17
|
+
end
|
18
|
+
@worth
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def reduce_by_ace
|
23
|
+
if count_aces > @reduced_ace_count
|
24
|
+
@worth -= 10
|
25
|
+
@reduced_ace_count += 1
|
26
|
+
true
|
27
|
+
else
|
28
|
+
false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def count_aces
|
33
|
+
@cards.count {|c| c.rank == "A"}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module CardsLib
|
2
|
+
module Standard
|
3
|
+
module Rankers
|
4
|
+
class BlackjackRanker < CardsLib::Ranker
|
5
|
+
def initialize(card)
|
6
|
+
super(
|
7
|
+
card,
|
8
|
+
{
|
9
|
+
"2" => 2,
|
10
|
+
"3" => 3,
|
11
|
+
"4" => 4,
|
12
|
+
"5" => 5,
|
13
|
+
"6" => 6,
|
14
|
+
"7" => 7,
|
15
|
+
"8" => 8,
|
16
|
+
"9" => 9,
|
17
|
+
"T" => 10,
|
18
|
+
"J" => 10,
|
19
|
+
"Q" => 10,
|
20
|
+
"K" => 10,
|
21
|
+
"A" => 11
|
22
|
+
},
|
23
|
+
->rank{@ranks[rank]}
|
24
|
+
)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
data/lib/cards_lib/standard.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
require_relative 'standard/rules'
|
2
|
+
require_relative 'standard/rankers'
|
3
|
+
require_relative 'standard/evaluators'
|
2
4
|
|
3
5
|
module CardsLib
|
4
6
|
module Standard
|
5
|
-
|
6
|
-
|
7
|
+
RANKS = 'A23456789TJQK'.chars
|
8
|
+
SUITS = 'cdhs'.chars
|
9
|
+
PLAYING_CARDS = RANKS.product(SUITS ).map(&:join)
|
10
|
+
ASCII_CARDS = RANKS.product('♣♦♥♠'.chars).map(&:join)
|
7
11
|
end
|
8
12
|
end
|
data/lib/cards_lib/version.rb
CHANGED
data/lib/cards_lib.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cards_lib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel P. Clark
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 1.
|
19
|
+
version: 1.11.0
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 1.
|
26
|
+
version: 1.11.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 1.1.4
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: minitest-byebug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.0.3
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.0.3
|
83
97
|
description: Card Game Library. Testable and Object Oriented.
|
84
98
|
email:
|
85
99
|
- 6ftdan@gmail.com
|
@@ -89,6 +103,7 @@ extra_rdoc_files: []
|
|
89
103
|
files:
|
90
104
|
- ".gitignore"
|
91
105
|
- ".travis.yml"
|
106
|
+
- CODE_OF_CONDUCT.md
|
92
107
|
- Gemfile
|
93
108
|
- Guardfile
|
94
109
|
- LICENSE.txt
|
@@ -104,8 +119,12 @@ files:
|
|
104
119
|
- lib/cards_lib/ranker.rb
|
105
120
|
- lib/cards_lib/refinements.rb
|
106
121
|
- lib/cards_lib/standard.rb
|
122
|
+
- lib/cards_lib/standard/evaluators.rb
|
123
|
+
- lib/cards_lib/standard/evaluators/blackjack_evaluator.rb
|
124
|
+
- lib/cards_lib/standard/rankers.rb
|
125
|
+
- lib/cards_lib/standard/rankers/blackjack_ranker.rb
|
107
126
|
- lib/cards_lib/standard/rules.rb
|
108
|
-
- lib/cards_lib/standard/rules/
|
127
|
+
- lib/cards_lib/standard/rules/poker_rule.rb
|
109
128
|
- lib/cards_lib/version.rb
|
110
129
|
homepage: http://github.com/danielpclark/CardsLib
|
111
130
|
licenses:
|