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.
- data/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/playing_cards/card.rb +76 -0
- data/lib/playing_cards/card_constant.rb +11 -0
- data/lib/playing_cards/deck.rb +51 -0
- data/lib/playing_cards/version.rb +3 -0
- data/lib/playing_cards.rb +5 -0
- data/playing_cards.gemspec +19 -0
- data/spec/card_spec.rb +81 -0
- data/spec/deck_spec.rb +123 -0
- data/spec/spec_helper.rb +1 -0
- metadata +62 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 hollanddd
|
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,29 @@
|
|
1
|
+
# PlayingCards
|
2
|
+
|
3
|
+
Playing Cards. Not much else to see here.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'playing_cards'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install playing_cards
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
deck_of_cards = Deck.standard
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,76 @@
|
|
1
|
+
class Card
|
2
|
+
attr_accessor :rank, :suit
|
3
|
+
|
4
|
+
def initialize rank, suit
|
5
|
+
@rank = rank.to_s.capitalize
|
6
|
+
@suit = suit.downcase.sub(/s$/,'').capitalize
|
7
|
+
end
|
8
|
+
|
9
|
+
def == another_card
|
10
|
+
rank == another_card.rank && suit == another_card.suit
|
11
|
+
end
|
12
|
+
|
13
|
+
def > another_card
|
14
|
+
numberify > another_card.numberify
|
15
|
+
end
|
16
|
+
|
17
|
+
def < another_card
|
18
|
+
numberify < another_card.numberify
|
19
|
+
end
|
20
|
+
|
21
|
+
def equals? another_card
|
22
|
+
numberify == another_card.numberify
|
23
|
+
end
|
24
|
+
|
25
|
+
def numberify
|
26
|
+
convert = {
|
27
|
+
'Two' => 2,
|
28
|
+
'Three' => 3,
|
29
|
+
'Four' => 4,
|
30
|
+
'Five' => 5,
|
31
|
+
'Six' => 6,
|
32
|
+
'Seven' => 7,
|
33
|
+
'Eight' => 8,
|
34
|
+
'Nine' => 9,
|
35
|
+
'Ten' => 10,
|
36
|
+
'Jack' => 11,
|
37
|
+
'Queen' => 12,
|
38
|
+
'King' => 13,
|
39
|
+
'Ace' => 14,
|
40
|
+
'2' => 2,
|
41
|
+
'3' => 3,
|
42
|
+
'4' => 4,
|
43
|
+
'5' => 5,
|
44
|
+
'6' => 6,
|
45
|
+
'7' => 7,
|
46
|
+
'8' => 8,
|
47
|
+
'9' => 9,
|
48
|
+
'10' => 10
|
49
|
+
}
|
50
|
+
convert[rank]
|
51
|
+
end
|
52
|
+
|
53
|
+
def name
|
54
|
+
"#{ rank } of #{ suit }s"
|
55
|
+
end
|
56
|
+
|
57
|
+
def inspect
|
58
|
+
"<Card: \"#{ name }\">"
|
59
|
+
end
|
60
|
+
|
61
|
+
alias to_s name
|
62
|
+
|
63
|
+
def self.parse name
|
64
|
+
name = name.to_s.sub(/^the()?/i, '').strip
|
65
|
+
|
66
|
+
if name =~ /^(\w+) of (\w+)$/i
|
67
|
+
Card.new $1, $2
|
68
|
+
elsif name =~ /^(\w+)of(\w+)$/i
|
69
|
+
Card.new $1, $2
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class << self
|
74
|
+
alias [] parse
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
def Deck *cards
|
2
|
+
Deck[*cards]
|
3
|
+
end
|
4
|
+
|
5
|
+
class Deck < Array
|
6
|
+
|
7
|
+
module ArrayExtensions
|
8
|
+
def to_deck
|
9
|
+
Deck.new self
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.standard
|
14
|
+
deck = Deck.new
|
15
|
+
%w{club diamond heart spade}.each do |suit|
|
16
|
+
%w{2 3 4 5 6 7 8 9 10 jack queen king ace}.each do |rank|
|
17
|
+
deck << CardWeb.new(rank, suit)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
deck
|
21
|
+
end
|
22
|
+
|
23
|
+
def deal_from_top num = 1
|
24
|
+
_draw num, :shift
|
25
|
+
end
|
26
|
+
|
27
|
+
alias draw deal_from_top
|
28
|
+
alias burn deal_from_top
|
29
|
+
|
30
|
+
def deal_from_bottom num = 1
|
31
|
+
_draw num, :pop
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def _draw num, operation
|
37
|
+
if num.is_a? Card
|
38
|
+
delete num
|
39
|
+
else
|
40
|
+
if num == 1
|
41
|
+
send(operation)
|
42
|
+
else
|
43
|
+
cards = []
|
44
|
+
num.times { cards << send(operation) }
|
45
|
+
cards
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
Array.send :include, Deck::ArrayExtensions
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'playing_cards/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "playing_cards"
|
8
|
+
gem.version = PlayingCards::VERSION
|
9
|
+
gem.authors = ["hollanddd"]
|
10
|
+
gem.email = ["me@darrenholland.com"]
|
11
|
+
gem.description = %q{Standard deck of 52. No Jokers}
|
12
|
+
gem.summary = %q{Standard deck of 52. No Jokers}
|
13
|
+
gem.homepage = "https://github.com/hollanddd/playing_cards"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
data/spec/card_spec.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe Card do
|
4
|
+
|
5
|
+
it 'has a rank and a suit' do
|
6
|
+
card = Card[TheAceOfHearts]
|
7
|
+
card.rank.should eq 'Ace'
|
8
|
+
card.suit.should eq 'Heart'
|
9
|
+
Card.new(8, 'heart').rank.should eq '8'
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'can initailize cards with a plural suit' do
|
13
|
+
card = Card.new 3, 'Hearts'
|
14
|
+
card.suit.should eq 'Heart'
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'can return a card given a name' do
|
18
|
+
card = Card.parse('3 of hearts')
|
19
|
+
card.rank.should eq '3'
|
20
|
+
card.suit.should eq 'Heart'
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'aliases [] to parse' do
|
24
|
+
card = Card['3 of hearts']
|
25
|
+
card.rank.should eq '3'
|
26
|
+
card.suit.should eq 'Heart'
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'can parse a card name with or without "the"' do
|
30
|
+
two_hearts = Card['2 of Hearts']
|
31
|
+
two_hearts.rank.should eq '2'
|
32
|
+
|
33
|
+
three_hearts = Card['the3ofHearts']
|
34
|
+
three_hearts.rank.should eq '3'
|
35
|
+
|
36
|
+
four_hearts = Card['the 4 of hearts']
|
37
|
+
four_hearts.rank.should eq '4'
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'can parse itself regardless of the casing of the suit' do
|
41
|
+
card = Card.new 2, 'spades'
|
42
|
+
card.rank.should eq '2'
|
43
|
+
card.suit.should eq 'Spade'
|
44
|
+
|
45
|
+
another_card = Card.new 3, 'hEaRtS'
|
46
|
+
another_card.rank.should eq '3'
|
47
|
+
another_card.suit.should eq 'Heart'
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'can parse itself from a constant' do
|
51
|
+
card = The2ofHearts
|
52
|
+
card.rank.should eq '2'
|
53
|
+
card.suit.should eq 'Heart'
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'can compare cards' do
|
57
|
+
card = TheAceOfHearts
|
58
|
+
the_other_card = Card.new('ace', 'hearts')
|
59
|
+
card.object_id.should_not eq the_other_card.object_id
|
60
|
+
card.should eq the_other_card
|
61
|
+
|
62
|
+
the_other_card = TheKingOfHearts
|
63
|
+
card > the_other_card
|
64
|
+
card < the_other_card
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'describes itself' do
|
68
|
+
|
69
|
+
it 'with a name' do
|
70
|
+
The3OfHearts.name.should eq '3 of Hearts'
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'with .to_s alias' do
|
74
|
+
The3OfHearts.to_s.should eq '3 of Hearts'
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'by inspecting itself' do
|
78
|
+
The3OfHearts.inspect.should eq '<Card: "3 of Hearts">'
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/spec/deck_spec.rb
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe Deck do
|
4
|
+
|
5
|
+
it 'can get an empty deck' do
|
6
|
+
deck = Deck.new
|
7
|
+
deck.length.should be 0
|
8
|
+
deck.should be_empty
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'can get a standard deck of 52 cards' do
|
12
|
+
deck = Deck.standard
|
13
|
+
deck.length.should be 52
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'can compare decks' do
|
17
|
+
deck = Deck.new
|
18
|
+
the_other_deck = Deck.new
|
19
|
+
deck.should == the_other_deck
|
20
|
+
|
21
|
+
deck << The2OfHearts
|
22
|
+
deck.equal?(the_other_deck).should eq false
|
23
|
+
|
24
|
+
the_other_deck << The2OfHearts
|
25
|
+
deck.equal?(the_other_deck).should eq false
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'can shuffle a deck' do
|
29
|
+
deck = Deck.standard
|
30
|
+
shuffled_deck = deck.shuffle
|
31
|
+
|
32
|
+
deck.each do |card|
|
33
|
+
shuffled_deck.should include(card)
|
34
|
+
end
|
35
|
+
|
36
|
+
shuffled_deck.should_not == deck
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'can shuffle! a deck' do
|
40
|
+
deck = Deck.standard
|
41
|
+
deck.shuffle!
|
42
|
+
|
43
|
+
deck.should_not == Deck.standard
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'can draw a card' do
|
47
|
+
deck = Deck.standard
|
48
|
+
deck.length.should be 52
|
49
|
+
|
50
|
+
card = deck.draw
|
51
|
+
card.should eq The2OfClubs
|
52
|
+
deck.length.should be 51
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'can convert cards to_deck' do
|
56
|
+
deck = Deck.standard
|
57
|
+
hand = deck.draw(7).to_deck
|
58
|
+
hand.class.should be Deck
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'can deal' do
|
62
|
+
|
63
|
+
it 'cards from the top' do
|
64
|
+
deck = Deck.standard
|
65
|
+
deck.length.should be 52
|
66
|
+
|
67
|
+
deck.deal_from_top.should eq The2OfClubs
|
68
|
+
deck.length.should be 51
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'cards from the bottom' do
|
72
|
+
deck = Deck.standard
|
73
|
+
deck.deal_from_bottom.should eq TheAceOfSpades
|
74
|
+
deck.length.should be 51
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'a given number of cards' do
|
78
|
+
deck = Deck.standard
|
79
|
+
cards = deck.draw 2
|
80
|
+
|
81
|
+
deck.length.should be 50
|
82
|
+
cards.length.should be 2
|
83
|
+
|
84
|
+
cards[0].should eq The2OfClubs
|
85
|
+
cards[1].should eq The3OfClubs
|
86
|
+
|
87
|
+
cards = deck.deal_from_bottom 2
|
88
|
+
|
89
|
+
deck.length.should be 48
|
90
|
+
cards.length.should be 2
|
91
|
+
|
92
|
+
cards[0].should eq TheAceOfSpades
|
93
|
+
cards[1].should eq TheKingOfSpades
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'a card by name' do
|
97
|
+
deck = Deck.standard
|
98
|
+
|
99
|
+
card = deck.draw The3OfClubs
|
100
|
+
deck.length.should be 51
|
101
|
+
deck[0].should eq The2OfClubs
|
102
|
+
deck[1].should eq The4OfClubs
|
103
|
+
|
104
|
+
deck.draw(The3OfClubs).should be_nil
|
105
|
+
|
106
|
+
card = deck.deal_from_bottom TheKingOfSpades
|
107
|
+
card.should eq TheKingOfSpades
|
108
|
+
deck.length.should be 50
|
109
|
+
deck[49].should eq TheAceOfSpades
|
110
|
+
deck[48].should eq TheQueenOfSpades
|
111
|
+
|
112
|
+
deck.deal_from_bottom(TheKingOfSpades).should be_nil
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'a burn card' do
|
116
|
+
deck = Deck.standard
|
117
|
+
deck.length.should be 52
|
118
|
+
deck.burn
|
119
|
+
deck.length.should be 51
|
120
|
+
deck[0].should eq The3OfClubs
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../lib/playing_cards'
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: playing_cards
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- hollanddd
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-20 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Standard deck of 52. No Jokers
|
15
|
+
email:
|
16
|
+
- me@darrenholland.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/playing_cards.rb
|
27
|
+
- lib/playing_cards/card.rb
|
28
|
+
- lib/playing_cards/card_constant.rb
|
29
|
+
- lib/playing_cards/deck.rb
|
30
|
+
- lib/playing_cards/version.rb
|
31
|
+
- playing_cards.gemspec
|
32
|
+
- spec/card_spec.rb
|
33
|
+
- spec/deck_spec.rb
|
34
|
+
- spec/spec_helper.rb
|
35
|
+
homepage: https://github.com/hollanddd/playing_cards
|
36
|
+
licenses: []
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.8.24
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: Standard deck of 52. No Jokers
|
59
|
+
test_files:
|
60
|
+
- spec/card_spec.rb
|
61
|
+
- spec/deck_spec.rb
|
62
|
+
- spec/spec_helper.rb
|