deck 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/deck.rb +12 -0
- data/lib/deck/card.rb +72 -0
- data/lib/deck/deck.rb +56 -0
- data/lib/deck/game.rb +29 -0
- data/lib/deck/hand.rb +9 -0
- data/lib/deck/version.rb +3 -0
- data/test/card_test.rb +53 -0
- data/test/deck_test.rb +42 -0
- data/test/game_test.rb +21 -0
- data/test/hand_test.rb +15 -0
- data/test/helper.rb +2 -0
- metadata +58 -0
data/lib/deck.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
|
2
|
+
$: << File.dirname(__FILE__)
|
3
|
+
|
4
|
+
require "deck/version"
|
5
|
+
require 'deck/card'
|
6
|
+
require 'deck/deck'
|
7
|
+
require 'deck/hand'
|
8
|
+
require 'deck/game'
|
9
|
+
class Deck
|
10
|
+
SUITS = %w(Spades Hearts Diamonds Clubs)
|
11
|
+
RANKS = %w(Ace 2 3 4 5 6 7 8 9 10 Jack Queen King)
|
12
|
+
end
|
data/lib/deck/card.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
class Card
|
4
|
+
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
attr_reader :suit, :rank
|
8
|
+
|
9
|
+
def initialize(rank = 'Joker', suit = 'None')
|
10
|
+
@suit = suit
|
11
|
+
@rank = rank
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
def rank
|
16
|
+
case @rank.to_s.upcase[0..1]
|
17
|
+
when 'JA' "Jack"
|
18
|
+
when 'QU' "Queen"
|
19
|
+
when 'KI' "King"
|
20
|
+
when 'AC' "Ace"
|
21
|
+
when 'JO' "Joker"
|
22
|
+
else @rank.to_s
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
def suit
|
28
|
+
@suit.capitalize
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_s
|
32
|
+
"#{rank} of #{suit}"
|
33
|
+
end
|
34
|
+
|
35
|
+
def ==(other)
|
36
|
+
to_i == other.to_i && suit == other.suit
|
37
|
+
end
|
38
|
+
|
39
|
+
def <=>(other)
|
40
|
+
to_i<=>other.to_i
|
41
|
+
end
|
42
|
+
|
43
|
+
def <(other)
|
44
|
+
to_i<other.to_i
|
45
|
+
end
|
46
|
+
|
47
|
+
def >(other)
|
48
|
+
to_i>other.to_i
|
49
|
+
end
|
50
|
+
|
51
|
+
def each
|
52
|
+
yield
|
53
|
+
end
|
54
|
+
|
55
|
+
def to_i
|
56
|
+
case @rank.to_s.upcase[0..1]
|
57
|
+
when 'JA'
|
58
|
+
11
|
59
|
+
when 'QU'
|
60
|
+
12
|
61
|
+
when 'KI'
|
62
|
+
13
|
63
|
+
when 'AC'
|
64
|
+
14
|
65
|
+
when 'JO'
|
66
|
+
15
|
67
|
+
else @rank.to_i
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
data/lib/deck/deck.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
class Deck
|
2
|
+
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
attr_reader :cards
|
6
|
+
|
7
|
+
#create a new deck of cards
|
8
|
+
def initialize(options = {})
|
9
|
+
defaults = {
|
10
|
+
:jokers => false
|
11
|
+
}
|
12
|
+
options = defaults.merge(options)
|
13
|
+
|
14
|
+
@cards = []
|
15
|
+
Deck::SUITS.each{|s|
|
16
|
+
Deck::RANKS.each{|r|
|
17
|
+
self << Card.new(r,s)
|
18
|
+
}
|
19
|
+
}
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
if options[:jokers]
|
24
|
+
self << Card.new
|
25
|
+
self << Card.new
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
def shuffle!
|
31
|
+
100.times { @cards.push @cards.delete_at(rand(size-1)) }
|
32
|
+
end
|
33
|
+
|
34
|
+
def each
|
35
|
+
@cards.each { |card| yield card }
|
36
|
+
end
|
37
|
+
|
38
|
+
def <<(card)
|
39
|
+
@cards << card
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
def size
|
44
|
+
@cards.size
|
45
|
+
end
|
46
|
+
|
47
|
+
alias :length :size
|
48
|
+
|
49
|
+
def draw
|
50
|
+
cards.shift
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
end
|
56
|
+
|
data/lib/deck/game.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
|
4
|
+
class Game
|
5
|
+
|
6
|
+
attr_reader :deck, :hands, :number_of_hands, :number_of_cards
|
7
|
+
|
8
|
+
|
9
|
+
def initialize(hands = 3, cards = 3)
|
10
|
+
@deck = Deck.new
|
11
|
+
@number_of_hands = hands
|
12
|
+
@number_of_cards = cards
|
13
|
+
1..@number_of_hands.to_i do |n|
|
14
|
+
@hands << Hand.new
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
def deal(shuffle = true)
|
21
|
+
deck.shuffle! if shuffle
|
22
|
+
1..@hands.size do |n|
|
23
|
+
1..@number_of_cards.to_i do |m|
|
24
|
+
@hands[m] << deck.draw
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/lib/deck/hand.rb
ADDED
data/lib/deck/version.rb
ADDED
data/test/card_test.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
|
4
|
+
class CardTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_card_object_initializes
|
7
|
+
assert_instance_of Card, Card.new("Ace","Spades")
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_should_initialize_with_number
|
11
|
+
assert_instance_of Card, Card.new(2,"Spades")
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_should_have_correct_rank_word
|
15
|
+
card = Card.new("Ace","Spades")
|
16
|
+
assert_equal "Ace", card.rank
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_should_have_correct_rank_number
|
20
|
+
card = Card.new(2,"Spades")
|
21
|
+
assert_equal "2", card.rank
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_should_have_correct_suit
|
25
|
+
card = Card.new(8,"Hearts")
|
26
|
+
assert_equal "Hearts", card.suit
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_should_have_correct_suit_non_case
|
30
|
+
card = Card.new(8,"hEaRts")
|
31
|
+
assert_equal "Hearts", card.suit
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_persists_ok
|
35
|
+
card = Card.new("Jack","Clubs")
|
36
|
+
card_2 = Card.new("King","Hearts")
|
37
|
+
assert_equal "Jack", card.rank
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_should_get_good_to_s
|
41
|
+
card = Card.new(10,"Clubs")
|
42
|
+
assert_equal "10 of Clubs", card.to_s
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_should_compare_ok
|
46
|
+
card = Card.new("Queen","Clubs")
|
47
|
+
card2 = Card.new("Jack", "Clubs")
|
48
|
+
assert card2 < card
|
49
|
+
assert card > card2
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
end
|
data/test/deck_test.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
|
4
|
+
class DeckTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_deck_object_initializes
|
7
|
+
assert_instance_of Deck, Deck.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_deck_size_is_correct
|
11
|
+
assert_equal 52, Deck.new.size
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_deck_containes_card_objects
|
15
|
+
assert_instance_of Card, Deck.new.first
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_can_create_deck_with_jokers
|
19
|
+
assert_equal 54, Deck.new(:jokers=>true).size
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_draw_first_is_ace_of_spades
|
23
|
+
deck = Deck.new
|
24
|
+
card = deck.draw
|
25
|
+
assert_equal "Ace of Spades", card.to_s
|
26
|
+
assert_equal 51, deck.size
|
27
|
+
end
|
28
|
+
|
29
|
+
#not ideal
|
30
|
+
def test_draw_first_after_shuffle_different
|
31
|
+
deck = Deck.new
|
32
|
+
deck_2 = Deck.new
|
33
|
+
deck.shuffle!
|
34
|
+
deck_2.shuffle!
|
35
|
+
assert_not_equal deck_2.draw, deck.draw
|
36
|
+
assert_equal 51, deck.size
|
37
|
+
assert_equal 51, deck_2.size
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
end
|
data/test/game_test.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class GameTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_initializes_ok
|
6
|
+
assert_instance_of Game, Game.new
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
def deals_ok
|
11
|
+
game = Game.new(10,5)
|
12
|
+
assert_equal 10, game.hands.size
|
13
|
+
|
14
|
+
(1..10).each do |n|
|
15
|
+
assert_equal 5, game.hands[n].size
|
16
|
+
end
|
17
|
+
|
18
|
+
assert_equal 2, game.deck.size
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/test/hand_test.rb
ADDED
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: deck
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.4
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- paul
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-08-04 00:00:00.000000000 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
description: represent playing cards, hands, decks and games
|
16
|
+
email:
|
17
|
+
- paul@dryule.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- lib/deck/version.rb
|
23
|
+
- lib/deck/hand.rb
|
24
|
+
- lib/deck/card.rb
|
25
|
+
- lib/deck/deck.rb
|
26
|
+
- lib/deck/game.rb
|
27
|
+
- lib/deck.rb
|
28
|
+
- test/hand_test.rb
|
29
|
+
- test/helper.rb
|
30
|
+
- test/deck_test.rb
|
31
|
+
- test/card_test.rb
|
32
|
+
- test/game_test.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: ''
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.6.2
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: a human readable class for a deck of cards
|
58
|
+
test_files: []
|