deck-of-cards 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -0
- data/README.md +55 -0
- data/Rakefile +5 -0
- data/deck-of-cards.gemspec +20 -0
- data/lib/deck-of-cards.rb +28 -0
- data/lib/deck-of-cards/card.rb +17 -0
- data/lib/deck-of-cards/compare_cards.rb +22 -0
- data/lib/deck-of-cards/version.rb +3 -0
- data/test/test_deck.rb +61 -0
- metadata +56 -0
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# Deck of Cards
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
`gem install deck-of-cards`
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
require 'deck-of-cards'
|
11
|
+
|
12
|
+
deck = DeckOfCards.new
|
13
|
+
# => #<Deck:0x007fa6d4187e78
|
14
|
+
# @cards=
|
15
|
+
# [#<2 of Hearts>,
|
16
|
+
# #<3 of Hearts>,
|
17
|
+
# #<4 of Hearts>,
|
18
|
+
# #<5 of Hearts>,
|
19
|
+
# '...']>
|
20
|
+
|
21
|
+
deck.shuffle
|
22
|
+
# => [#<Ace of Clubs>,
|
23
|
+
# #<9 of Spades>,
|
24
|
+
# #<10 of Spades>,
|
25
|
+
# #<Queen of Clubs>,
|
26
|
+
|
27
|
+
deck.split
|
28
|
+
# => => [#<7 of Hearts>,
|
29
|
+
# #<4 of Diamonds>,
|
30
|
+
# #<King of Diamonds>,
|
31
|
+
# #<4 of Clubs>,
|
32
|
+
# #<6 of Clubs>,
|
33
|
+
|
34
|
+
my_card = deck.draw
|
35
|
+
# => #<7 of Hearts>
|
36
|
+
|
37
|
+
your_card = deck.draw
|
38
|
+
# => #<#<King of Diamonds>
|
39
|
+
|
40
|
+
my_card > your_card
|
41
|
+
# => false
|
42
|
+
|
43
|
+
my_card <= your_card
|
44
|
+
# => true
|
45
|
+
```
|
46
|
+
|
47
|
+
## MIT License
|
48
|
+
|
49
|
+
Copyright (c) 2012 Shannon Skipper
|
50
|
+
|
51
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
52
|
+
|
53
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
54
|
+
|
55
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "deck-of-cards/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "deck-of-cards"
|
7
|
+
s.version = DeckOfCards::VERSION
|
8
|
+
s.authors = ["shannon skipper"]
|
9
|
+
s.email = ["shannonskipper@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/Havenwood/deck-of-cards"
|
11
|
+
s.summary = %q{a minimalist 'deck of cards' gem}
|
12
|
+
s.description = %q{A minimalist 'deck of cards' Ruby gem.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "deck-of-cards"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'deck-of-cards/compare_cards'
|
2
|
+
require_relative 'deck-of-cards/card'
|
3
|
+
require_relative 'deck-of-cards/version'
|
4
|
+
|
5
|
+
class DeckOfCards
|
6
|
+
attr_reader :cards
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@cards = []
|
10
|
+
suits = %w[Hearts Spades Diamonds Clubs]
|
11
|
+
ranks = %w[2 3 4 5 6 7 8 9 10 Jack Queen King Ace]
|
12
|
+
suits.product(ranks) { |suit, rank| @cards << Card.new(rank, suit) }
|
13
|
+
end
|
14
|
+
|
15
|
+
def shuffle
|
16
|
+
@cards.shuffle!
|
17
|
+
end
|
18
|
+
|
19
|
+
def cut
|
20
|
+
@cards.rotate! @cards.count.div 2
|
21
|
+
end
|
22
|
+
|
23
|
+
alias split cut
|
24
|
+
|
25
|
+
def draw
|
26
|
+
@cards.shift
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module CompareCards
|
2
|
+
include Comparable
|
3
|
+
|
4
|
+
def value
|
5
|
+
case @rank
|
6
|
+
when 'Ace'
|
7
|
+
14
|
8
|
+
when 'King'
|
9
|
+
13
|
10
|
+
when 'Queen'
|
11
|
+
12
|
12
|
+
when 'Jack'
|
13
|
+
11
|
14
|
+
else
|
15
|
+
@rank.to_i
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def <=> other_card
|
20
|
+
self.value <=> other_card.value
|
21
|
+
end
|
22
|
+
end
|
data/test/test_deck.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'minitest/pride'
|
5
|
+
require 'set'
|
6
|
+
require_relative '../lib/deck-of-cards'
|
7
|
+
|
8
|
+
describe DeckOfCards do
|
9
|
+
before do
|
10
|
+
@deck = DeckOfCards.new
|
11
|
+
@correct_cards = []
|
12
|
+
suits = ["Hearts", "Spades", "Diamonds", "Clubs"]
|
13
|
+
ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King", "Ace"]
|
14
|
+
suits.each { |suit| ranks.each { |rank| @correct_cards << Card.new(rank, suit) } }
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "when a deck is created" do
|
18
|
+
it "must have 52 cards" do
|
19
|
+
@deck.cards.count.must_equal 52
|
20
|
+
end
|
21
|
+
|
22
|
+
it "must have the correct cards" do
|
23
|
+
@deck.cards.map(&:to_s).to_set.must_equal @correct_cards.map(&:to_s).to_set
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "when a deck is shuffled" do
|
28
|
+
before do
|
29
|
+
@deck.shuffle
|
30
|
+
end
|
31
|
+
|
32
|
+
it "wont be in the same order" do
|
33
|
+
@deck.cards.map(&:to_s).wont_equal @correct_cards
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "when a deck is cut" do
|
38
|
+
before do
|
39
|
+
@deck.cut
|
40
|
+
end
|
41
|
+
|
42
|
+
it "must be cut in half" do
|
43
|
+
@deck.cards.first.to_s.must_equal @correct_cards[26].to_s
|
44
|
+
@deck.cards.last.to_s.must_equal @correct_cards[25].to_s
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "when a card is drawn" do
|
49
|
+
before do
|
50
|
+
@card = @deck.draw
|
51
|
+
end
|
52
|
+
|
53
|
+
it "must be a real card" do
|
54
|
+
@correct_cards.map(&:to_s).must_include @card.to_s
|
55
|
+
end
|
56
|
+
|
57
|
+
it "must reduce the deck size by one" do
|
58
|
+
@deck.cards.size.must_equal 51
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: deck-of-cards
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- shannon skipper
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-19 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A minimalist 'deck of cards' Ruby gem.
|
15
|
+
email:
|
16
|
+
- shannonskipper@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- Gemfile
|
22
|
+
- README.md
|
23
|
+
- Rakefile
|
24
|
+
- deck-of-cards.gemspec
|
25
|
+
- lib/deck-of-cards.rb
|
26
|
+
- lib/deck-of-cards/card.rb
|
27
|
+
- lib/deck-of-cards/compare_cards.rb
|
28
|
+
- lib/deck-of-cards/version.rb
|
29
|
+
- test/test_deck.rb
|
30
|
+
homepage: https://github.com/Havenwood/deck-of-cards
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project: deck-of-cards
|
50
|
+
rubygems_version: 1.8.24
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: a minimalist 'deck of cards' gem
|
54
|
+
test_files:
|
55
|
+
- test/test_deck.rb
|
56
|
+
has_rdoc:
|