rubycards 0.0.1 → 0.0.2

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/README.md CHANGED
@@ -25,6 +25,7 @@ Or install it yourself as:
25
25
  Here's a trivial example of declaring a new deck, shuffling, and drawing 5 cards into a hand:
26
26
 
27
27
  require 'rubycards'
28
+ include RubyCards
28
29
 
29
30
  hand = Hand.new
30
31
  deck = Deck.new
@@ -1,131 +1,133 @@
1
1
  # encoding: utf-8
2
2
  require 'colored'
3
3
 
4
- class Card
4
+ module RubyCards
5
+ class Card
5
6
 
6
- include Comparable
7
+ include Comparable
7
8
 
8
- # constants for glyphs
9
- CLUB = '♣'
10
- DIAMOND = '♦'
11
- HEART = '♥'
12
- SPADE = '♠'
9
+ # constants for glyphs
10
+ CLUB = '♣'
11
+ DIAMOND = '♦'
12
+ HEART = '♥'
13
+ SPADE = '♠'
13
14
 
14
- def initialize(rank = 'Ace', suit = 'Spades')
15
- @rank = rank_to_i(rank)
16
- @suit = suit_to_i(suit)
17
- end
18
-
19
- # returns the rank of the card
20
- # optional short parameter will limit face cards to one character
21
- def rank(short = false)
22
- if (2..10) === @rank
23
- @rank.to_s
24
- else
25
- h = { 11 => 'Jack', 12 => 'Queen', 13 => 'King', 14 => 'Ace' }
26
- h[@rank] && short ? h[@rank][0] : h[@rank]
15
+ def initialize(rank = 'Ace', suit = 'Spades')
16
+ @rank = rank_to_i(rank)
17
+ @suit = suit_to_i(suit)
27
18
  end
28
- end
29
19
 
30
- # returns the suit of a card
31
- # optional glyph parameter displays a colored unicode character
32
- def suit(glyph = false)
33
- case @suit
34
- when 1
35
- glyph ? CLUB.black.bold : 'Clubs'
36
- when 2
37
- glyph ? DIAMOND.red : 'Diamonds'
38
- when 3
39
- glyph ? HEART.red : 'Hearts'
40
- when 4
41
- glyph ? SPADE.black.bold : 'Spades'
20
+ # returns the rank of the card
21
+ # optional short parameter will limit face cards to one character
22
+ def rank(short = false)
23
+ if (2..10) === @rank
24
+ @rank.to_s
25
+ else
26
+ h = { 11 => 'Jack', 12 => 'Queen', 13 => 'King', 14 => 'Ace' }
27
+ h[@rank] && short ? h[@rank][0] : h[@rank]
28
+ end
42
29
  end
43
- end
44
30
 
45
- # returns the short rank, followed by suit icon
46
- def short
47
- "#{rank(true)}#{suit(true)}"
48
- end
31
+ # returns the suit of a card
32
+ # optional glyph parameter displays a colored unicode character
33
+ def suit(glyph = false)
34
+ case @suit
35
+ when 1
36
+ glyph ? CLUB.black.bold : 'Clubs'
37
+ when 2
38
+ glyph ? DIAMOND.red : 'Diamonds'
39
+ when 3
40
+ glyph ? HEART.red : 'Hearts'
41
+ when 4
42
+ glyph ? SPADE.black.bold : 'Spades'
43
+ end
44
+ end
49
45
 
50
- # comparator
51
- def <=>(c)
52
- self.to_i <=> c.to_i
53
- end
46
+ # returns the short rank, followed by suit icon
47
+ def short
48
+ "#{rank(true)}#{suit(true)}"
49
+ end
54
50
 
55
- # returns the numerical representation of the rank
56
- def to_i
57
- @rank
58
- end
51
+ # comparator
52
+ def <=>(c)
53
+ self.to_i <=> c.to_i
54
+ end
59
55
 
60
- # draws a card picture
61
- def to_s
62
- # A simple template with X's as placeholders
63
- # YY represents the placement of the card's rank
64
- template = <<-TPL.gsub(/^\s+/,'')
65
- ╭───────╮
66
- | X X X |
67
- | X X X |
68
- | X YYX |
69
- | X X X |
70
- ╰───────╯
71
- TPL
72
-
73
- # the patterns represent the configuration of glyphys
74
- # read from left to right, top to bottom
75
- # X means place a glyph, _ means clear the space
76
- case @rank
77
- when 2; pattern = '_X_______X_'
78
- when 3; pattern = '_X__X____X_'
79
- when 4; pattern = 'X_X_____X_X'
80
- when 5; pattern = 'X_X_X___X_X'
81
- when 6; pattern = 'X_XX_X__X_X'
82
- when 7; pattern = 'X_X_X_XXX_X'
83
- when 8; pattern = 'X_XX_XXXX_X'
84
- when 9; pattern = 'X_XXXXXXX_X'
85
- when 10; pattern = 'XXXX_XXXXXX'
86
- when 11..14;
87
- pattern = 'X_________X'
56
+ # returns the numerical representation of the rank
57
+ def to_i
58
+ @rank
88
59
  end
89
60
 
90
- pattern.each_char do |c|
91
- # replace X's with glyphs
92
- if c == 'X'
93
- template.sub!(/X/, "#{suit(true)}")
94
- # replace _'s with whitespace
95
- else
96
- template.sub!(/X/, " ")
61
+ # draws a card picture
62
+ def to_s
63
+ # A simple template with X's as placeholders
64
+ # YY represents the placement of the card's rank
65
+ template = <<-TPL.gsub(/^\s+/,'')
66
+ ╭───────╮
67
+ | X X X |
68
+ | X X X |
69
+ | X YYX |
70
+ | X X X |
71
+ ╰───────╯
72
+ TPL
73
+
74
+ # the patterns represent the configuration of glyphys
75
+ # read from left to right, top to bottom
76
+ # X means place a glyph, _ means clear the space
77
+ case @rank
78
+ when 2; pattern = '_X_______X_'
79
+ when 3; pattern = '_X__X____X_'
80
+ when 4; pattern = 'X_X_____X_X'
81
+ when 5; pattern = 'X_X_X___X_X'
82
+ when 6; pattern = 'X_XX_X__X_X'
83
+ when 7; pattern = 'X_X_X_XXX_X'
84
+ when 8; pattern = 'X_XX_XXXX_X'
85
+ when 9; pattern = 'X_XXXXXXX_X'
86
+ when 10; pattern = 'XXXX_XXXXXX'
87
+ when 11..14;
88
+ pattern = 'X_________X'
97
89
  end
98
- end
99
90
 
100
- # place the card rank (left-padded)
101
- template.sub(/YY/, rank(true).ljust(2))
102
- end
91
+ pattern.each_char do |c|
92
+ # replace X's with glyphs
93
+ if c == 'X'
94
+ template.sub!(/X/, "#{suit(true)}")
95
+ # replace _'s with whitespace
96
+ else
97
+ template.sub!(/X/, " ")
98
+ end
99
+ end
103
100
 
104
- private
105
-
106
- # converts the string representation of a rank to an integer
107
- def rank_to_i(rank)
108
- case rank.to_s
109
- when /^(a|ace)/i; 14
110
- when /^(k|king)/i; 13
111
- when /^(q|queen)/i; 12
112
- when /^(j|jack)/i; 11
113
- when '10'; 10
114
- when '2'..'9'; rank
115
- else 0
101
+ # place the card rank (left-padded)
102
+ template.sub(/YY/, rank(true).ljust(2))
116
103
  end
117
- end
118
104
 
119
- # converts the string representation of a suit to an integer
120
- # suits are ordered alphabetically
121
- def suit_to_i(suit)
122
- case suit
123
- when /^club/i; 1
124
- when /^diamond/i; 2
125
- when /^heart/i; 3
126
- when /^spade/i; 4
127
- else 0
105
+ private
106
+
107
+ # converts the string representation of a rank to an integer
108
+ def rank_to_i(rank)
109
+ case rank.to_s
110
+ when /^(a|ace)/i; 14
111
+ when /^(k|king)/i; 13
112
+ when /^(q|queen)/i; 12
113
+ when /^(j|jack)/i; 11
114
+ when '10'; 10
115
+ when '2'..'9'; rank
116
+ else 0
117
+ end
118
+ end
119
+
120
+ # converts the string representation of a suit to an integer
121
+ # suits are ordered alphabetically
122
+ def suit_to_i(suit)
123
+ case suit
124
+ when /^club/i; 1
125
+ when /^diamond/i; 2
126
+ when /^heart/i; 3
127
+ when /^spade/i; 4
128
+ else 0
129
+ end
128
130
  end
129
- end
130
131
 
132
+ end
131
133
  end
@@ -1,45 +1,47 @@
1
- class Deck
1
+ module RubyCards
2
+ class Deck
2
3
 
3
- include Enumerable
4
+ include Enumerable
4
5
 
5
- attr_reader :cards
6
+ attr_reader :cards
6
7
 
7
- RANKS = [*2..10, 'Jack', 'Queen', 'King', 'Ace']
8
- SUITS = %w{ Clubs Diamonds Hearts Spades }
8
+ RANKS = [*2..10, 'Jack', 'Queen', 'King', 'Ace']
9
+ SUITS = %w{ Clubs Diamonds Hearts Spades }
9
10
 
10
- # initialize a deck of cards using the RANKS and SUITS constants
11
- def initialize
12
- @cards = []
11
+ # initialize a deck of cards using the RANKS and SUITS constants
12
+ def initialize
13
+ @cards = []
13
14
 
14
- RANKS.product(SUITS).each do |rank, suit|
15
- @cards << Card.new(rank, suit)
15
+ RANKS.product(SUITS).each do |rank, suit|
16
+ @cards << Card.new(rank, suit)
17
+ end
16
18
  end
17
- end
18
19
 
19
- # shuffle the deck
20
- def shuffle!
21
- @cards.shuffle!
22
- end
20
+ # shuffle the deck
21
+ def shuffle!
22
+ @cards.shuffle!
23
+ end
23
24
 
24
- # draw a card from the deck
25
- def draw
26
- @cards.shift
27
- end
25
+ # draw a card from the deck
26
+ def draw
27
+ @cards.shift
28
+ end
28
29
 
29
- # determines if the deck is empty
30
- def empty?
31
- @cards.empty?
32
- end
30
+ # determines if the deck is empty
31
+ def empty?
32
+ @cards.empty?
33
+ end
33
34
 
34
- # Enumerable#each
35
- def each(&block)
36
- @cards.each do |card|
37
- if block_given?
38
- block.call card
39
- else
40
- yield card
35
+ # Enumerable#each
36
+ def each(&block)
37
+ @cards.each do |card|
38
+ if block_given?
39
+ block.call card
40
+ else
41
+ yield card
42
+ end
41
43
  end
42
44
  end
43
- end
44
45
 
46
+ end
45
47
  end
@@ -1,57 +1,59 @@
1
- class Hand
1
+ module RubyCards
2
+ class Hand
2
3
 
3
- include Enumerable
4
+ include Enumerable
4
5
 
5
- attr_reader :cards
6
+ attr_reader :cards
6
7
 
7
- # initialize with an array of cards
8
- def initialize(cards = [])
9
- @cards = []
10
- cards.each do |card|
11
- self << card
8
+ # initialize with an array of cards
9
+ def initialize(cards = [])
10
+ @cards = []
11
+ cards.each do |card|
12
+ self << card
13
+ end
12
14
  end
13
- end
14
15
 
15
- # add a card if it is valid
16
- def add(card)
17
- @cards << card if card.instance_of? Card
18
- end
16
+ # add a card if it is valid
17
+ def add(card)
18
+ @cards << card if card.instance_of? Card
19
+ end
19
20
 
20
- # alias array push (<<) to add method
21
- alias_method :<<, :add
21
+ # alias array push (<<) to add method
22
+ alias_method :<<, :add
22
23
 
23
- # indexing
24
- def [](n)
25
- @cards[n]
26
- end
24
+ # indexing
25
+ def [](n)
26
+ @cards[n]
27
+ end
27
28
 
28
- # sorting
29
- def sort!
30
- @cards.sort!
31
- end
29
+ # sorting
30
+ def sort!
31
+ @cards.sort!
32
+ end
32
33
 
33
- # draw n cards from a deck
34
- def draw(deck, n = 1)
35
- n.times do
36
- @cards << deck.draw unless deck.empty?
34
+ # draw n cards from a deck
35
+ def draw(deck, n = 1)
36
+ n.times do
37
+ @cards << deck.draw unless deck.empty?
38
+ end
37
39
  end
38
- end
39
40
 
40
- # Enumerable#each
41
- def each(&block)
42
- @cards.each do |card|
43
- if block_given?
44
- block.call card
45
- else
46
- yield card
41
+ # Enumerable#each
42
+ def each(&block)
43
+ @cards.each do |card|
44
+ if block_given?
45
+ block.call card
46
+ else
47
+ yield card
48
+ end
47
49
  end
48
50
  end
49
- end
50
51
 
51
- # displaying the card icons
52
- # using extensions/string.rb for String#next
53
- def to_s
54
- @cards.map(&:to_s).inject(:next)
55
- end
52
+ # displaying the card icons
53
+ # using extensions/string.rb for String#next
54
+ def to_s
55
+ @cards.map(&:to_s).inject(:next)
56
+ end
56
57
 
58
+ end
57
59
  end
@@ -1,3 +1,3 @@
1
- module Rubycards
2
- VERSION = "0.0.1"
1
+ module RubyCards
2
+ VERSION = "0.0.2"
3
3
  end
@@ -5,7 +5,7 @@ require 'rubycards/version'
5
5
 
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = "rubycards"
8
- gem.version = Rubycards::VERSION
8
+ gem.version = RubyCards::VERSION
9
9
  gem.authors = ["Jordan Scales", "Joe Letizia"]
10
10
  gem.email = ["scalesjordan@gmail.com", "joe.letizia@gmail.com"]
11
11
  gem.description = "RubyCards"
@@ -1,5 +1,7 @@
1
1
  require 'rubycards'
2
2
 
3
+ include RubyCards
4
+
3
5
  describe Card do
4
6
  describe '#initialize' do
5
7
  context 'no params' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubycards
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: