ruby_playing_cards 0.0.1
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 +7 -0
- data/.travis.yml +3 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +9 -0
- data/lib/ruby_playing_cards/card.rb +19 -0
- data/lib/ruby_playing_cards/dealer.rb +41 -0
- data/lib/ruby_playing_cards/deck.rb +37 -0
- data/lib/ruby_playing_cards/hand.rb +27 -0
- data/lib/ruby_playing_cards/version.rb +3 -0
- data/lib/ruby_playing_cards.rb +5 -0
- data/ruby_playing_cards.gemspec +19 -0
- data/spec/card_spec.rb +36 -0
- data/spec/dealer_spec.rb +65 -0
- data/spec/deck_spec.rb +13 -0
- data/spec/spec_helper.rb +4 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e1bf2e0a631db30fd8497e9be093449ff9c17e7c
|
4
|
+
data.tar.gz: e0ec451f9ead125e7a1be66fb1f7d70f5ab9f3f8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c1073abf1c671845068b5e2c249340a06a358bda91c6550ce2a2687d1db89804bd4472f9c7cff32cbbf0374d8716eea725502f88766ec937d24ab5dbfcf75f91
|
7
|
+
data.tar.gz: c3568f1dc59f6d621cd4b39e1b2628c1418df60cc18b1491ad4e9d0e9623ea2210bd9ea49116088cd29b914f2e46d5fc03c460344909181a3f206a7b710121e7
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2017 Justin McKay
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Ruby Playing Cards
|
2
|
+
|
3
|
+
[](https://travis-ci.org/jcmckay/ruby_playing_cards) [](https://coveralls.io/github/jcmckay/ruby_playing_cards?branch=master)
|
4
|
+
|
5
|
+
## Instalation
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
```ruby
|
8
|
+
gem 'ruby_playing_cards'
|
9
|
+
```
|
10
|
+
And then execute:
|
11
|
+
```ruby
|
12
|
+
$ bundle
|
13
|
+
```
|
14
|
+
Or install it yourself as:
|
15
|
+
```ruby
|
16
|
+
$ gem install ruby_playing_cards
|
17
|
+
```
|
18
|
+
|
19
|
+
## Example
|
20
|
+
Here is a simple example of building and dealing a deck of cards.
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
require 'ruby_playing_cards'
|
24
|
+
|
25
|
+
include RubyPlayingCards
|
26
|
+
deck = Deck.new
|
27
|
+
|
28
|
+
Dealer.shuffle deck
|
29
|
+
|
30
|
+
hands = Dealer.deal deck, hand_size: 5, player_count: 3
|
31
|
+
|
32
|
+
hands.each { |hand| puts hand.to_s }
|
33
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module RubyPlayingCards
|
2
|
+
|
3
|
+
class Card
|
4
|
+
|
5
|
+
FACE_CARDS = { 1 => :A, 13 => :K, 12 => :Q, 11 => :J }
|
6
|
+
attr_accessor :rank, :suit
|
7
|
+
|
8
|
+
def initialize(rank, suit)
|
9
|
+
@rank = FACE_CARDS[rank] || rank
|
10
|
+
@suit = suit
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
"#{@suit}#{@rank}"
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module RubyPlayingCards
|
2
|
+
class Dealer
|
3
|
+
|
4
|
+
class << self
|
5
|
+
|
6
|
+
def cut!(deck, index=26)
|
7
|
+
top = deck.cards.shift(index)
|
8
|
+
deck.cards = deck.cards + top
|
9
|
+
end
|
10
|
+
|
11
|
+
def deal(deck, hand_size: 1, player_count: 1)
|
12
|
+
hands = []
|
13
|
+
card_total = hand_size * player_count
|
14
|
+
|
15
|
+
player_count.times do |player_index|
|
16
|
+
hand = build_hand(deck, player_index, card_total, player_count)
|
17
|
+
hands << hand
|
18
|
+
end
|
19
|
+
|
20
|
+
deck.cards.shift(card_total)
|
21
|
+
hands
|
22
|
+
end
|
23
|
+
|
24
|
+
def draw(deck, card_count=1)
|
25
|
+
deck.cards.shift card_count
|
26
|
+
end
|
27
|
+
|
28
|
+
def shuffle(deck)
|
29
|
+
deck.cards.shuffle!
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
def build_hand(deck, player_index, card_count, player_count)
|
34
|
+
cards = []
|
35
|
+
(player_index+1).step(card_count,player_count) { |deck_index| cards << deck.cards[deck_index-1]}
|
36
|
+
Hand.new(cards, "player#{player_index+1}")
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module RubyPlayingCards
|
2
|
+
class Deck
|
3
|
+
SUITS = [
|
4
|
+
{ name: :spades, icon: :♠},
|
5
|
+
{ name: :hearts, icon: :♥},
|
6
|
+
{ name: :diamonds, icon: :♦},
|
7
|
+
{ name: :clubs, icon: :♣}
|
8
|
+
]
|
9
|
+
attr_accessor :cards
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@cards = []
|
13
|
+
build_deck
|
14
|
+
end
|
15
|
+
|
16
|
+
def size
|
17
|
+
@cards.size
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_s
|
21
|
+
@cards.map do |card|
|
22
|
+
"#{card.suit}#{card.rank}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def build_deck
|
29
|
+
SUITS.each do |suit|
|
30
|
+
1.upto(13) do |rank|
|
31
|
+
@cards << Card.new(rank, suit[:icon])
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module RubyPlayingCards
|
2
|
+
class Hand
|
3
|
+
|
4
|
+
attr_accessor :cards, :player
|
5
|
+
|
6
|
+
def initialize(cards, player)
|
7
|
+
@cards = cards
|
8
|
+
@player = player
|
9
|
+
end
|
10
|
+
|
11
|
+
def add_cards(cards)
|
12
|
+
@cards << cards
|
13
|
+
end
|
14
|
+
|
15
|
+
def size
|
16
|
+
@cards.size
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_s
|
20
|
+
puts "Player #{@player}: "
|
21
|
+
@cards.map do |card|
|
22
|
+
card.to_s
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'ruby_playing_cards/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "ruby_playing_cards"
|
7
|
+
spec.version = RubyPlayingCards::VERSION
|
8
|
+
spec.authors = ["Justin McKay"]
|
9
|
+
spec.email = ["justinmckay16@gmail.com"]
|
10
|
+
spec.description = %q{Basic playing cards}
|
11
|
+
spec.summary = %q{Basic playing cards for playing with cards}
|
12
|
+
spec.homepage = "https://github.com/jcmckay/ruby_playing_cards"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
end
|
data/spec/card_spec.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe RubyPlayingCards::Card do
|
4
|
+
|
5
|
+
describe 'when creating cards' do
|
6
|
+
it 'should show card' do
|
7
|
+
for val in 2...10 do
|
8
|
+
card = RubyPlayingCards::Card.new(val, 'suit')
|
9
|
+
expect(card.to_s).to eq("suit#{val}")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'when building face cards' do
|
15
|
+
it 'should build an ace' do
|
16
|
+
card = RubyPlayingCards::Card.new(1, 'suit')
|
17
|
+
expect(card.rank).to eq(:A)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should build a jack' do
|
21
|
+
card = RubyPlayingCards::Card.new(11, 'suit')
|
22
|
+
expect(card.rank).to eq(:J)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should build a queen' do
|
26
|
+
card = RubyPlayingCards::Card.new(12, 'suit')
|
27
|
+
expect(card.rank).to eq(:Q)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should build a king' do
|
31
|
+
card = RubyPlayingCards::Card.new(13, 'suit')
|
32
|
+
expect(card.rank).to eq(:K)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/spec/dealer_spec.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe RubyPlayingCards::Dealer do
|
4
|
+
|
5
|
+
let(:deck) { RubyPlayingCards::Deck.new }
|
6
|
+
let(:dealer) {RubyPlayingCards::Dealer }
|
7
|
+
|
8
|
+
context 'cutting the deck' do
|
9
|
+
|
10
|
+
it 'should cut the deck in half' do
|
11
|
+
top_card = deck.cards.first.to_s
|
12
|
+
cards = dealer.cut! deck
|
13
|
+
|
14
|
+
expect(cards[26].to_s).to eq(top_card)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should cut the deck where specified' do
|
18
|
+
top_card = deck.cards.first.to_s
|
19
|
+
CUT_INDEX = 5
|
20
|
+
cards = dealer.cut!(deck, CUT_INDEX)
|
21
|
+
expect(cards[52-CUT_INDEX].to_s).to eq(top_card)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'dealing' do
|
27
|
+
|
28
|
+
it 'should deal a hand to each player' do
|
29
|
+
HAND_SIZE = 3
|
30
|
+
PLAYER_COUNT = 7
|
31
|
+
|
32
|
+
hands = dealer.deal deck, hand_size: HAND_SIZE, player_count: PLAYER_COUNT
|
33
|
+
|
34
|
+
expect(hands.size).to eq(PLAYER_COUNT)
|
35
|
+
hands.each { |h| expect(h.size).to eq(HAND_SIZE)}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'drawing a card' do
|
40
|
+
|
41
|
+
it 'should remove a single card' do
|
42
|
+
top_card = dealer.draw(deck)
|
43
|
+
expect(top_card).to be_truthy
|
44
|
+
expect(top_card.size).to eq(1)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'drawing multiple cards' do
|
49
|
+
it 'should remove n number of cards' do
|
50
|
+
cards = dealer.draw(deck, 5)
|
51
|
+
expect(cards).to be_truthy
|
52
|
+
expect(cards.size).to eq(5)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'shuffling' do
|
57
|
+
it 'should shuffle the cards' do
|
58
|
+
unshuffled_cards = deck.cards.dup
|
59
|
+
shuffled_cards = dealer.shuffle deck
|
60
|
+
|
61
|
+
expect(shuffled_cards).not_to eq(unshuffled_cards)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
data/spec/deck_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby_playing_cards
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Justin McKay
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Basic playing cards
|
14
|
+
email:
|
15
|
+
- justinmckay16@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".travis.yml"
|
21
|
+
- Gemfile
|
22
|
+
- LICENSE.txt
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- lib/ruby_playing_cards.rb
|
26
|
+
- lib/ruby_playing_cards/card.rb
|
27
|
+
- lib/ruby_playing_cards/dealer.rb
|
28
|
+
- lib/ruby_playing_cards/deck.rb
|
29
|
+
- lib/ruby_playing_cards/hand.rb
|
30
|
+
- lib/ruby_playing_cards/version.rb
|
31
|
+
- ruby_playing_cards.gemspec
|
32
|
+
- spec/card_spec.rb
|
33
|
+
- spec/dealer_spec.rb
|
34
|
+
- spec/deck_spec.rb
|
35
|
+
- spec/spec_helper.rb
|
36
|
+
homepage: https://github.com/jcmckay/ruby_playing_cards
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
metadata: {}
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 2.6.10
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Basic playing cards for playing with cards
|
60
|
+
test_files:
|
61
|
+
- spec/card_spec.rb
|
62
|
+
- spec/dealer_spec.rb
|
63
|
+
- spec/deck_spec.rb
|
64
|
+
- spec/spec_helper.rb
|