cribbage 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 200cb7218063ba97c8e79f52e32b12e869ec0224
4
+ data.tar.gz: 02226a8ceda8219eac3bc0d17d0cf9e9f73467ea
5
+ SHA512:
6
+ metadata.gz: 60093aa38cbcbe88620f2f9a3f34cf55975a9931ace6931005ef15353e0e4daf2cfdfcd75f3a4edd75e5d10eff07518b65c81e2d536a09c65cd1ad4c95a28eaa
7
+ data.tar.gz: 363119ae1e3a7c1ddb34c6d83301ddb53d90cd7a2d2dcd051cd3f093611c42b21068db42ed62f038415448cf35a32c39aa039c11155a00af68106bf640ad9e33
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cribbage.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 davycro
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ - Add suit unicode
2
+ - publish
3
+ - DRY
4
+
5
+ # Cribbage
6
+
7
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cribbage`. To experiment with that code, run `bin/console` for an interactive prompt.
8
+
9
+ TODO: Delete this and the text above, and describe your gem
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'cribbage'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install cribbage
26
+
27
+ ## Usage
28
+
29
+ TODO: Write usage instructions here
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
34
+
35
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cribbage.
40
+
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
45
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cribbage"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/cribbage.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cribbage/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cribbage"
8
+ spec.version = Cribbage::VERSION
9
+ spec.authors = ["davycro"]
10
+ spec.email = ["davycro1@gmail.com"]
11
+
12
+ spec.summary = %q{score a cribbage hand from the command line}
13
+ spec.license = "MIT"
14
+
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_dependency "thor"
25
+ spec.add_dependency "awesome_print"
26
+ end
data/exe/cribbage ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'cribbage/cli'
4
+ Cribbage::CLI.start
@@ -0,0 +1,12 @@
1
+ require 'thor'
2
+ require 'cribbage'
3
+ module Cribbage
4
+ class CLI < Thor
5
+ desc "count START CARD_1 CARD_2 CARD_3 CARD_4", "count points in hand of five cards"
6
+ def count(*cards)
7
+
8
+ hand = Cribbage::Hand.new(cards)
9
+ hand.print_score
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Cribbage
2
+ VERSION = "0.1.0"
3
+ end
data/lib/cribbage.rb ADDED
@@ -0,0 +1,223 @@
1
+ require "cribbage/version"
2
+ require 'awesome_print'
3
+
4
+ module Cribbage
5
+ # Input:
6
+
7
+ # H = heart S = spade D = diamond C = club
8
+
9
+ # AH 5C 6D 7S 8C QD 9C
10
+
11
+ class Card
12
+
13
+ # Str format:
14
+ # "5h" = five of hearts
15
+ # "AD" = ace of diamonds
16
+ # "10c" = ten of clubs
17
+
18
+ attr_accessor :value
19
+ attr_accessor :face_value
20
+ attr_accessor :suit
21
+ attr_accessor :card_str
22
+ attr_accessor :index # index in a hand. Ie A = 0,
23
+
24
+ INDEX = %w(A 2 3 4 5 6 7 8 9 10 J Q K)
25
+
26
+ def initialize(str)
27
+ raise "String cannot be blank!" if str.nil?
28
+ raise "Must be a string!" unless str.is_a?(String)
29
+ raise "Invalid length" if (str.size < 2 or str.size > 3)
30
+
31
+ @value = self.compute_value(str.chop)
32
+ @face_value = str.chop.upcase
33
+ raise "Invalid face value" unless INDEX.include?(@face_value)
34
+ @index = INDEX.index(@face_value)
35
+ @suit = str[-1].upcase
36
+ @card_str = str
37
+ end
38
+
39
+ def to_human_readable_format
40
+ "#{@value} of #{@suit}"
41
+ end
42
+
43
+ def to_s
44
+ "#{@face_value}#{@suit}"
45
+ end
46
+
47
+ def compute_value(char)
48
+ return 1 if char.upcase=='A'
49
+ return 10 if %w(J Q K).include?(char.upcase)
50
+ if char.to_i > 0 and char.to_i <= 10
51
+ return char.to_i
52
+ end
53
+ raise "Could not compute value of #{char}"
54
+ end
55
+ end
56
+
57
+ class Hand
58
+
59
+ attr :points
60
+ # key: fifteens
61
+ # cards: [ [1,2], [3,4] ] MUST BE Nested array
62
+ # points: 4
63
+
64
+ attr :cards
65
+
66
+ # Input is an array of strings from the command line
67
+ # I.E. 4H JD QD KH 3H
68
+ def initialize(cards_array)
69
+ @cards = [ ]
70
+ @points = { }
71
+ cards_array.each { |c| @cards << Card.new(c) }
72
+
73
+ find_fifteens
74
+ find_runs
75
+ find_pairs
76
+ find_flush
77
+ find_nobs
78
+ end
79
+
80
+ def starter_card
81
+ @cards.first
82
+ end
83
+
84
+ def players_cards
85
+ @cards[1...@cards.size]
86
+ end
87
+
88
+ def total_score
89
+ score = 0
90
+ @points.each do |k,v|
91
+ score += v[:point_value]
92
+ end
93
+ score
94
+ end
95
+
96
+ def print_score
97
+ # Your Hand
98
+ # 5H | 6D 3D JH
99
+ #
100
+ # Total Points: +17
101
+ # Fifteens (+8): (5 5 5), (5 J), (5 J), (5 K)
102
+ # Runs:
103
+
104
+ ap "#{starter_card} | #{players_cards.join(' ')}"
105
+ ap ""
106
+ ap "Total Points: #{total_score}"
107
+
108
+ print_points :fifteens
109
+ print_points :runs
110
+ print_points :pairs
111
+ print_points :flush
112
+ print_points :nobs
113
+ end
114
+
115
+ def print_points(key)
116
+ if @points[key].nil?
117
+ return false
118
+ end
119
+ cards = @points[key.to_sym][:cards] # should be array of array
120
+ str = cards.map { |c| "[" + c.join(' ') + "]" }.join ", "
121
+ title = key.to_s
122
+ ap "#{title} (+#{@points[key][:point_value]}): #{str}"
123
+ end
124
+
125
+ def print_cards(label, cards)
126
+ str = cards.map { |c| c.card_str }.join " "
127
+ ap "#{label}: #{str}"
128
+ end
129
+
130
+ def find_fifteens
131
+ found = nil
132
+ [2,3,4,5].each do |combo_size|
133
+ @cards.combination(combo_size).each do |card_combo|
134
+
135
+ # Check for 15s
136
+ card_count = 0
137
+ card_combo.each { |card| card_count += card.value }
138
+ if card_count == 15
139
+ found ||= [ ]
140
+ found << card_combo
141
+ end
142
+ end
143
+ end
144
+ if found!=nil
145
+ @points[:fifteens] = {cards: found, point_value: found.size*2}
146
+ end
147
+ end
148
+
149
+ def find_runs
150
+ found = { }
151
+ [5,4,3].each do |combo_size|
152
+ @cards.combination(combo_size).each do |combo|
153
+ combo = combo.sort { |x,y| x.index <=> y.index } # sort by index
154
+ next unless combo.map(&:face_value).uniq.size == combo.size # all cards must be unique
155
+ if (combo.last.index - combo.first.index) == (combo.size-1)
156
+ found[combo_size] ||= [ ]
157
+ found[combo_size] << combo
158
+ end
159
+ end
160
+ end
161
+ if found.has_key?(5)
162
+ @points[:runs] = {cards: found[5], point_value: 5}
163
+ return true
164
+ end
165
+ if found.has_key?(4)
166
+ cards = found[4]
167
+ @points[:runs] = {cards: cards, point_value: cards.first.size*cards.size}
168
+ return true
169
+ end
170
+ if found.has_key?(3)
171
+ cards = found[3]
172
+ @points[:runs] = {cards: cards, point_value: cards.first.size*cards.size}
173
+ return found[3]
174
+ end
175
+ return nil
176
+ end
177
+
178
+ def find_pairs
179
+ found = [ ]
180
+ @cards.combination(2).each do |pair|
181
+ if pair.first.face_value == pair.last.face_value
182
+ found << pair
183
+ end
184
+ end
185
+ if found.size==0
186
+ return nil
187
+ else
188
+ @points[:pairs] = {cards: found, point_value: found.size*2}
189
+ end
190
+ end
191
+
192
+ def find_flush
193
+ hand = @cards
194
+ if @cards.map(&:suit).uniq.size == 1
195
+ @points[:flush] = {cards: [@cards], point_value: 5}
196
+ return true
197
+ end
198
+ if players_cards.map(&:suit).uniq.size == 1
199
+ @points[:flush] = {cards: [ players_cards ], point_value: 4}
200
+ return true
201
+ end
202
+ return nil
203
+ end
204
+
205
+ def find_heels
206
+ # Awarded when the starter card is a Jack
207
+ if starter_card.face_value=='J'
208
+ @points[:heels] = {cards: [[starter_card]], point_value: 2}
209
+ end
210
+ end
211
+
212
+ def find_nobs
213
+ # One for nobs
214
+ jacks = players_cards.keep_if {|c| c.face_value=='J'}
215
+ if jacks.map(&:suit).include?(starter_card.suit)
216
+ @points[:nobs] = {cards: [ jacks.keep_if { |j| starter_card.suit==j.suit } ], point_value: 1}
217
+ end
218
+ return nil
219
+ end
220
+
221
+ end
222
+
223
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cribbage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - davycro
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: awesome_print
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - davycro1@gmail.com
72
+ executables:
73
+ - cribbage
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - cribbage.gemspec
85
+ - exe/cribbage
86
+ - lib/cribbage.rb
87
+ - lib/cribbage/cli.rb
88
+ - lib/cribbage/version.rb
89
+ homepage:
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.5.1
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: score a cribbage hand from the command line
113
+ test_files: []