rubycards 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.rvmrc ADDED
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p385@rubycards"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.18.6 (stable)" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ else
29
+ # If the environment file has not yet been created, use the RVM CLI to select.
30
+ rvm --create "$environment_id" || {
31
+ echo "Failed to create RVM environment '${environment_id}'."
32
+ return 1
33
+ }
34
+ fi
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rubycards.gemspec
4
+ gemspec
5
+ gem 'colored'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jordan Scales
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,46 @@
1
+ # Rubycards
2
+
3
+ Rubycards is a library to emulate playing cards (cards, hands, decks). As an added bonus, you can display the cards as tiny pictures. I'm mainly doing this as an exercise to learn better Ruby organization, as well as documentation and testing. More importantly it's just fun.
4
+
5
+ Feel free to peek around my code and point out anything I'm bad at.
6
+
7
+ ![rubycards](http://jordanscales.com/rubycards.png)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'rubycards'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rubycards
22
+
23
+ ## Example
24
+
25
+ Here's a trivial example of declaring a new deck, shuffling, and drawing 5 cards into a hand:
26
+
27
+ require 'rubycards'
28
+
29
+ hand = Hand.new
30
+ deck = Deck.new
31
+
32
+ deck.shuffle!
33
+
34
+ hand.draw(deck, 5)
35
+
36
+ puts hand
37
+
38
+ Which produces the image at the top of this README.
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,17 @@
1
+ class String
2
+
3
+ # Places a string to the right of the current string
4
+ #
5
+ # Example:
6
+ # ab ef abef
7
+ # cd .next( gh ) => cdgh
8
+ def next(str)
9
+ # zip the two strings, split by line breaks
10
+ zipped = self.split("\n").zip(str.split("\n")).map
11
+
12
+ # map the zipped strings, by joining each pair and ending
13
+ # with a new line, then joining the whole thing together
14
+ zipped.map { |e| "#{e.join}\n" }.join
15
+ end
16
+
17
+ end
data/lib/rubycards.rb ADDED
@@ -0,0 +1,6 @@
1
+ $: << File.dirname(__FILE__)
2
+
3
+ require 'extensions/string'
4
+ require 'rubycards/card'
5
+ require 'rubycards/deck'
6
+ require 'rubycards/hand'
@@ -0,0 +1,131 @@
1
+ # encoding: utf-8
2
+ require 'colored'
3
+
4
+ class Card
5
+
6
+ include Comparable
7
+
8
+ # constants for glyphs
9
+ CLUB = '♣'
10
+ DIAMOND = '♦'
11
+ HEART = '♥'
12
+ SPADE = '♠'
13
+
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]
27
+ end
28
+ end
29
+
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'
42
+ end
43
+ end
44
+
45
+ # returns the short rank, followed by suit icon
46
+ def short
47
+ "#{rank(true)}#{suit(true)}"
48
+ end
49
+
50
+ # comparator
51
+ def <=>(c)
52
+ self.to_i <=> c.to_i
53
+ end
54
+
55
+ # returns the numerical representation of the rank
56
+ def to_i
57
+ @rank
58
+ end
59
+
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'
88
+ end
89
+
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/, " ")
97
+ end
98
+ end
99
+
100
+ # place the card rank (left-padded)
101
+ template.sub(/YY/, rank(true).ljust(2))
102
+ end
103
+
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
116
+ end
117
+ end
118
+
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
128
+ end
129
+ end
130
+
131
+ end
@@ -0,0 +1,45 @@
1
+ class Deck
2
+
3
+ include Enumerable
4
+
5
+ attr_reader :cards
6
+
7
+ RANKS = [*2..10, 'Jack', 'Queen', 'King', 'Ace']
8
+ SUITS = %w{ Clubs Diamonds Hearts Spades }
9
+
10
+ # initialize a deck of cards using the RANKS and SUITS constants
11
+ def initialize
12
+ @cards = []
13
+
14
+ RANKS.product(SUITS).each do |rank, suit|
15
+ @cards << Card.new(rank, suit)
16
+ end
17
+ end
18
+
19
+ # shuffle the deck
20
+ def shuffle!
21
+ @cards.shuffle!
22
+ end
23
+
24
+ # draw a card from the deck
25
+ def draw
26
+ @cards.shift
27
+ end
28
+
29
+ # determines if the deck is empty
30
+ def empty?
31
+ @cards.empty?
32
+ end
33
+
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
41
+ end
42
+ end
43
+ end
44
+
45
+ end
@@ -0,0 +1,57 @@
1
+ class Hand
2
+
3
+ include Enumerable
4
+
5
+ attr_reader :cards
6
+
7
+ # initialize with an array of cards
8
+ def initialize(cards = [])
9
+ @cards = []
10
+ cards.each do |card|
11
+ self << card
12
+ end
13
+ end
14
+
15
+ # add a card if it is valid
16
+ def add(card)
17
+ @cards << card if card.instance_of? Card
18
+ end
19
+
20
+ # alias array push (<<) to add method
21
+ alias_method :<<, :add
22
+
23
+ # indexing
24
+ def [](n)
25
+ @cards[n]
26
+ end
27
+
28
+ # sorting
29
+ def sort!
30
+ @cards.sort!
31
+ end
32
+
33
+ # draw n cards from a deck
34
+ def draw(deck, n = 1)
35
+ n.times do
36
+ @cards << deck.draw unless deck.empty?
37
+ end
38
+ end
39
+
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
47
+ end
48
+ end
49
+ end
50
+
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
56
+
57
+ end
@@ -0,0 +1,3 @@
1
+ module Rubycards
2
+ VERSION = "0.0.1"
3
+ end
data/rubycards.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rubycards/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rubycards"
8
+ gem.version = Rubycards::VERSION
9
+ gem.authors = ["Jordan Scales", "Joe Letizia"]
10
+ gem.email = ["scalesjordan@gmail.com", "joe.letizia@gmail.com"]
11
+ gem.description = "RubyCards"
12
+ gem.summary = "A 52 card gem"
13
+ gem.homepage = "https://github.com/prezjordan/rubycards"
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
+
20
+ gem.add_dependency('colored', '>= 1.2')
21
+ end
data/spec/card_spec.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'rubycards'
2
+
3
+ describe Card do
4
+ describe '#initialize' do
5
+ context 'no params' do
6
+ it 'should return the ace of spades' do
7
+ subject.suit.should == 'Spades'
8
+ subject.rank.should == 'Ace'
9
+ end
10
+ end
11
+
12
+ context 'params' do
13
+ it 'should return the card specified in params' do
14
+ new_card = Card.new('Queen', 'Clubs')
15
+ new_card.rank.should == 'Queen'
16
+ new_card.suit.should == 'Clubs'
17
+
18
+ new_card = Card.new(3, 'Spades')
19
+ new_card.rank.should == 3.to_s
20
+ new_card.suit.should == 'Spades'
21
+ end
22
+ end
23
+ end
24
+
25
+ describe '#comparator' do
26
+ let(:king){Card.new('King','Clubs')}
27
+ let(:queen){Card.new('Queen','Clubs')}
28
+ let(:jack){Card.new('Jack','Clubs')}
29
+ let(:ace){Card.new('Ace','Clubs')}
30
+ let(:c2){Card.new(2,'Diamonds')}
31
+ let(:c2_heart){Card.new(2,'Hearts')}
32
+ let(:c4){Card.new(4,'Spades')}
33
+
34
+ it 'should reflect the correct rank when compared' do
35
+ king.should > queen
36
+ king.should > jack
37
+ king.should == king
38
+ king.should < ace
39
+
40
+ jack.should_not > queen
41
+ jack.should_not > ace
42
+ jack.should < queen
43
+
44
+ ace.should_not < queen
45
+ ace.should > c4
46
+
47
+ c2.should < c4
48
+ c2_heart.should == c2
49
+
50
+ end
51
+ end
52
+
53
+ describe '#rank' do
54
+ context 'face cards' do
55
+ let(:king){Card.new('King','Clubs')}
56
+
57
+ it 'should return a long rank' do
58
+ king.rank.should == 'King'
59
+ end
60
+ it 'should return a short rank' do
61
+ king.rank(true).should == 'K'
62
+ end
63
+ end
64
+
65
+ context 'numeric cards' do
66
+ let(:num){Card.new(10,'Diamonds')}
67
+
68
+ it 'should have the same long and short rank' do
69
+ num.rank.should == num.rank(true)
70
+ end
71
+ end
72
+ end
73
+ end
data/spec/factory.rb ADDED
@@ -0,0 +1,10 @@
1
+ FactoryGirl.define do
2
+ factory :card do
3
+ end
4
+
5
+ factory :deck do
6
+ end
7
+
8
+ factory :hand do
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubycards
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jordan Scales
9
+ - Joe Letizia
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-04-21 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: colored
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '1.2'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '1.2'
31
+ description: RubyCards
32
+ email:
33
+ - scalesjordan@gmail.com
34
+ - joe.letizia@gmail.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - .gitignore
40
+ - .rspec
41
+ - .rvmrc
42
+ - Gemfile
43
+ - LICENSE.txt
44
+ - README.md
45
+ - Rakefile
46
+ - lib/extensions/string.rb
47
+ - lib/rubycards.rb
48
+ - lib/rubycards/card.rb
49
+ - lib/rubycards/deck.rb
50
+ - lib/rubycards/hand.rb
51
+ - lib/rubycards/version.rb
52
+ - rubycards.gemspec
53
+ - spec/card_spec.rb
54
+ - spec/factory.rb
55
+ - spec/spec_helper.rb
56
+ homepage: https://github.com/prezjordan/rubycards
57
+ licenses: []
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.23
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: A 52 card gem
80
+ test_files:
81
+ - spec/card_spec.rb
82
+ - spec/factory.rb
83
+ - spec/spec_helper.rb
84
+ has_rdoc: