game_shuffle_cards 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/Gemfile +10 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +123 -0
  6. data/Rakefile +2 -0
  7. data/doc/GameShuffleCards/Card.html +622 -0
  8. data/doc/GameShuffleCards/Game.html +1094 -0
  9. data/doc/GameShuffleCards/NotEnoughCardsPerPlayersError.html +123 -0
  10. data/doc/GameShuffleCards/NotEnoughPlayersError.html +123 -0
  11. data/doc/GameShuffleCards/ToManyCardsPerPlayerError.html +123 -0
  12. data/doc/GameShuffleCards/ToManyPlayersError.html +123 -0
  13. data/doc/GameShuffleCards/TooManyCardsDemandedError.html +123 -0
  14. data/doc/GameShuffleCards/ValidateGame.html +438 -0
  15. data/doc/GameShuffleCards.html +129 -0
  16. data/doc/_index.html +198 -0
  17. data/doc/class_list.html +58 -0
  18. data/doc/css/common.css +1 -0
  19. data/doc/css/full_list.css +57 -0
  20. data/doc/css/style.css +339 -0
  21. data/doc/file.README.html +196 -0
  22. data/doc/file_list.html +60 -0
  23. data/doc/frames.html +26 -0
  24. data/doc/index.html +196 -0
  25. data/doc/js/app.js +219 -0
  26. data/doc/js/full_list.js +181 -0
  27. data/doc/js/jquery.js +4 -0
  28. data/doc/method_list.html +159 -0
  29. data/doc/top-level-namespace.html +131 -0
  30. data/features/deck_of_cards.feature +58 -0
  31. data/features/initial_settings.feature +76 -0
  32. data/features/step_definitions/deck_of_cards_step.rb +21 -0
  33. data/features/step_definitions/initial_settings_step.rb +44 -0
  34. data/features/step_definitions/step.rb +2 -0
  35. data/game_shuffle_cards.gemspec +24 -0
  36. data/lib/game_shuffle_cards/card.rb +26 -0
  37. data/lib/game_shuffle_cards/exceptions.rb +9 -0
  38. data/lib/game_shuffle_cards/game.rb +84 -0
  39. data/lib/game_shuffle_cards/validate_game.rb +43 -0
  40. data/lib/game_shuffle_cards/version.rb +5 -0
  41. data/lib/game_shuffle_cards.rb +2 -0
  42. metadata +119 -0
@@ -0,0 +1,76 @@
1
+ Feature: Initials Setting
2
+ Checking the valid settings
3
+ Scenario Outline: Checking Maximun Cards per Player
4
+ Given one user has set a Game with <Players> to shuffle cards
5
+ When I check the game restrictions
6
+ Then It should has this <Maximun Cards per Player>
7
+ Examples:
8
+ | Players | Maximun Cards per Player |
9
+ | 1 | 52 |
10
+ | 2 | 26 |
11
+ | 3 | 17 |
12
+ | 4 | 13 |
13
+ | 5 | 10 |
14
+ | 6 | 8 |
15
+ | 7 | 7 |
16
+ | 8 | 6 |
17
+ | 9 | 5 |
18
+ | 10 | 5 |
19
+ | 11 | 4 |
20
+ | 12 | 4 |
21
+ | 13 | 4 |
22
+ | 14 | 3 |
23
+ | 15 | 3 |
24
+ | 16 | 3 |
25
+ | 17 | 3 |
26
+ | 18 | 2 |
27
+ | 19 | 2 |
28
+ | 20 | 2 |
29
+ | 21 | 2 |
30
+ | 22 | 2 |
31
+ | 23 | 2 |
32
+ | 24 | 2 |
33
+ | 25 | 2 |
34
+ | 26 | 2 |
35
+ | 27 | 1 |
36
+ | 28 | 1 |
37
+ | 29 | 1 |
38
+ | 30 | 1 |
39
+ | 31 | 1 |
40
+ | 32 | 1 |
41
+ | 33 | 1 |
42
+ | 34 | 1 |
43
+ | 35 | 1 |
44
+ | 36 | 1 |
45
+ | 37 | 1 |
46
+ | 38 | 1 |
47
+ | 39 | 1 |
48
+ | 40 | 1 |
49
+ | 41 | 1 |
50
+ | 42 | 1 |
51
+ | 43 | 1 |
52
+ | 44 | 1 |
53
+ | 45 | 1 |
54
+ | 46 | 1 |
55
+ | 47 | 1 |
56
+ | 48 | 1 |
57
+ | 49 | 1 |
58
+ | 50 | 1 |
59
+ | 51 | 1 |
60
+ | 52 | 1 |
61
+
62
+ Scenario Outline: Checking limits restrictions
63
+ When I start a game with this number of <Players>
64
+ And and this <Cards per Player>
65
+ Then I should rescue this <Error>
66
+
67
+ Examples:
68
+ | Players | Cards per Player | Error |
69
+ | 60 | 1 | ToManyPlayersError |
70
+ | 1 | 53 | ToManyCardsPerPlayerError |
71
+ | 0 | 2 | NotEnoughPlayersError |
72
+ | 2 | 0 | NotEnoughCardsPerPlayersError |
73
+ | 4 | 16 | TooManyCardsDemandedError |
74
+ | ff | 16 | TypeError |
75
+ | 8 | hh55p | TypeError |
76
+ | fesef1 | hh55p | TypeError |
@@ -0,0 +1,21 @@
1
+ Given(/^this deak of cards defined to check$/) do |table|
2
+ @deck= table.raw
3
+ end
4
+
5
+ When(/^I list the cards$/) do
6
+ @game = Game.new
7
+ @card_list =[]
8
+
9
+ @game.cards_collection.keys.each do |key|
10
+ @card_list.push [key]
11
+ end
12
+
13
+ end
14
+ Then(/^all should be included$/) do
15
+ expect(@card_list).to match_array(@deck)
16
+
17
+ end
18
+
19
+ Then(/^they should be a total of (\d+)$/) do |total_cards|
20
+ expect(@game.cards_collection.count).to eq(total_cards.to_i)
21
+ end
@@ -0,0 +1,44 @@
1
+ Given(/^one user has set a Game with (\d+) to shuffle cards$/) do |players|
2
+ # create a new game with the given playeres
3
+ @game = Game.new(players.to_i,1)
4
+ end
5
+
6
+ When(/^I check the game restrictions$/) do
7
+ @maximun_cards_per_player = @game.maximun_cards_per_player
8
+ end
9
+
10
+ Then(/^It should has this (\d+)$/) do |maximun_cards_per_player|
11
+ expect(@maximun_cards_per_player).to eq(maximun_cards_per_player.to_i)
12
+ end
13
+
14
+ When(/^I start a game with this number of (\w+)$/) do |players|
15
+ @players=players
16
+ end
17
+
18
+ And(/^and this (\w+)$/) do |cards_per_player|
19
+ @cards_per_player = cards_per_player
20
+ end
21
+
22
+
23
+ Then(/^I should rescue this ToManyPlayersError$/) do
24
+ expect{@game = Game.new(@players,@cards_per_player)}.to raise_error(ToManyPlayersError)
25
+ end
26
+
27
+ Then(/^I should rescue this ToManyCardsPerPlayerError$/) do
28
+ expect{@game = Game.new(@players,@cards_per_player)}.to raise_error(ToManyCardsPerPlayerError)
29
+ end
30
+
31
+ Then(/^I should rescue this NotEnoughPlayersError$/) do
32
+ expect{@game = Game.new(@players,@cards_per_player)}.to raise_error(NotEnoughPlayersError)
33
+ end
34
+
35
+ Then(/^I should rescue this NotEnoughCardsPerPlayersError$/) do
36
+ expect{@game = Game.new(@players,@cards_per_player)}.to raise_error(NotEnoughCardsPerPlayersError)
37
+ end
38
+
39
+ Then(/^I should rescue this TooManyCardsDemandedError$/) do
40
+ expect{@game = Game.new(@players,@cards_per_player)}.to raise_error(TooManyCardsDemandedError)
41
+ end
42
+ Then(/^I should rescue this TypeError$/) do
43
+ expect{@game = Game.new(@players,@cards_per_player)}.to raise_error(TypeError)
44
+ end
@@ -0,0 +1,2 @@
1
+
2
+ require 'game_shuffle_cards'
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'game_shuffle_cards/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "game_shuffle_cards"
8
+ spec.version = GameShuffleCards::VERSION
9
+ spec.authors = ["Diego"]
10
+ spec.email = ["diego@guiasrails.es"]
11
+ spec.summary = "A Game to shuffle a deck of cards and presents the result"
12
+ spec.description = "With the class Game you can handle a deck of cards and setting the players and the cards per player. And shuffle the deak of cards."
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.has_rdoc = 'yard'
24
+ end
@@ -0,0 +1,26 @@
1
+
2
+ module GameShuffleCards
3
+
4
+ # @author Diego Hernán Piccinini Lagos
5
+ class Card
6
+
7
+ # Create a new card
8
+ # @param card_value [String]
9
+ # @param suit [String]
10
+ def initialize(card_value,suit)
11
+ @id= card_value + ' of ' + suit
12
+ @card_value=card_value
13
+ @suit=suit
14
+ end
15
+ attr_reader :id, :card_value, :suit, :image
16
+
17
+ # Get a image name file
18
+ # @param id [String]
19
+ # @return a file name [String]
20
+ class << self
21
+ def image(id)
22
+ id.tr(' ','_') + '.png'
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,9 @@
1
+
2
+
3
+ module GameShuffleCards
4
+ class ToManyPlayersError < RangeError; end
5
+ class NotEnoughPlayersError < RangeError; end
6
+ class ToManyCardsPerPlayerError < RangeError; end
7
+ class NotEnoughCardsPerPlayersError < RangeError; end
8
+ class TooManyCardsDemandedError < RangeError; end
9
+ end
@@ -0,0 +1,84 @@
1
+
2
+
3
+ module GameShuffleCards
4
+ # This class is the main class to shuffle a deck of cards
5
+ # @author Diego Piccinini Lagos
6
+ class Game
7
+ # the minimun cards per player
8
+ MINIMUN_CARDS = 1
9
+
10
+ # the minimun players in a game
11
+ MINIMUN_PLAYERS= 1
12
+
13
+ # the default value of players
14
+ PLAYERS = 2
15
+
16
+ # the defaul value of cards per player
17
+ CARDS_PER_PLAYER = 3
18
+
19
+ # The suit collection of the deck of cards
20
+ SUITS = ['spades', 'hearts', 'diamonds', 'clubs']
21
+
22
+ # The cards values collection
23
+ CARDS_VALUES = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']
24
+
25
+ # Count total cards in a deck
26
+ TOTAL_CARDS = SUITS.count * CARDS_VALUES.count
27
+
28
+ # Maximun players allowed in a Game
29
+ MAXIMUN_PLAYERS = TOTAL_CARDS # the maximun players in a game
30
+
31
+ # Initialize the game players and cards per player you want
32
+ # @param players [String|Integer] the number of players in the game
33
+ # @param cards_per_player [String|Integer] the number of cards that will recieve each player
34
+ def initialize(players = PLAYERS, cards_per_player=CARDS_PER_PLAYER)
35
+ # Validate the input params and parse values
36
+ @players, @cards_per_player = ValidateGame.parse_and_validate(players,cards_per_player)
37
+
38
+ # The card collection, all the cards in the deck
39
+ @cards_collection = {}
40
+
41
+ CARDS_VALUES.each do |card_value|
42
+ SUITS.each do |suit|
43
+ # Create one card and add it to cards_collections
44
+ card = Card.new(card_value,suit)
45
+ @cards_collection[card.id]=card
46
+ end
47
+ # add the cards keys to the cards availables
48
+ @cards_availables= @cards_collection.keys
49
+ end
50
+ # Initialize the maximun possible of cards per player,
51
+ # given a card collection and a number of players
52
+ @maximun_cards_per_player = Game.get_maximun_cards_per_player(@players)
53
+
54
+ end
55
+ attr_reader :players, :cards_per_player, :cards_collection, :maximun_cards_per_player
56
+ attr_accessor :cards_availables
57
+
58
+ # Shuffle the deck of cards
59
+ # @return a group of players [Hash] each with their cards
60
+ def results
61
+ players={}
62
+ @players.times do |player|
63
+ players[player]=get_cards
64
+ end
65
+ return players
66
+ end
67
+
68
+ # Get cards of the cards still availables in the deck
69
+ # @return group of cards [Array] an array of cards_per_player
70
+ def get_cards
71
+ cards=@cards_availables.sample(@cards_per_player)
72
+ @cards_availables-= cards
73
+ cards
74
+ end
75
+
76
+ # Get the maximun of cards per player,
77
+ # given a card collection and a number of players
78
+ # @return maximun cards per player [Integer]
79
+ def self.get_maximun_cards_per_player(players)
80
+ players = ValidateGame.parse_and_validate_players(players)
81
+ TOTAL_CARDS / players
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,43 @@
1
+
2
+ module GameShuffleCards
3
+ # @author Diego Hernán Piccinini Lagos
4
+ # validation to secure the inputs of the Game
5
+ class ValidateGame
6
+ class << self
7
+ # Given two values String or Integer, checks whether they are valid in a context Game
8
+ # then return the integers values or raise an exception
9
+ # @param players [String|Integer]
10
+ # @param cards_per_player [String|Integer]
11
+ # @return players and cards per player [Array] with integers values of each one
12
+ def parse_and_validate(players, cards_per_player)
13
+ begin
14
+ players = Integer(players)
15
+ cards_per_player= Integer(cards_per_player)
16
+
17
+ rescue
18
+ raise TypeError
19
+ end
20
+ raise GameShuffleCards::ToManyPlayersError if players > GameShuffleCards::Game::MAXIMUN_PLAYERS
21
+ raise GameShuffleCards::NotEnoughPlayersError if players < GameShuffleCards::Game::MINIMUN_PLAYERS
22
+ raise GameShuffleCards::ToManyCardsPerPlayerError if cards_per_player > GameShuffleCards::Game::TOTAL_CARDS
23
+ raise GameShuffleCards::NotEnoughCardsPerPlayersError if cards_per_player < GameShuffleCards::Game::MINIMUN_CARDS
24
+ raise GameShuffleCards::TooManyCardsDemandedError if (players * cards_per_player) > GameShuffleCards::Game::TOTAL_CARDS
25
+ [players,cards_per_player]
26
+ end
27
+
28
+ # Validates whether the amount and type of players are right. Otherwise raise an exception
29
+ # @param players [String|Integer]
30
+ # @return number of players [Integer]
31
+ def parse_and_validate_players(players)
32
+ begin
33
+ players = Integer(players)
34
+ rescue
35
+ raise TypeError
36
+ end
37
+ raise GameShuffleCards::ToManyPlayersError if players > GameShuffleCards::Game::MAXIMUN_PLAYERS
38
+ raise GameShuffleCards::NotEnoughPlayersError if players < GameShuffleCards::Game::MINIMUN_PLAYERS
39
+ players
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ Dir[File.join(File.dirname(__FILE__),'*.rb')].each {|file| require file }
2
+
3
+ module GameShuffleCards
4
+ VERSION = "1.0.4"
5
+ end
@@ -0,0 +1,2 @@
1
+ require "game_shuffle_cards/version"
2
+ include GameShuffleCards
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: game_shuffle_cards
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Diego
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: With the class Game you can handle a deck of cards and setting the players
42
+ and the cards per player. And shuffle the deak of cards.
43
+ email:
44
+ - diego@guiasrails.es
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - doc/GameShuffleCards.html
55
+ - doc/GameShuffleCards/Card.html
56
+ - doc/GameShuffleCards/Game.html
57
+ - doc/GameShuffleCards/NotEnoughCardsPerPlayersError.html
58
+ - doc/GameShuffleCards/NotEnoughPlayersError.html
59
+ - doc/GameShuffleCards/ToManyCardsPerPlayerError.html
60
+ - doc/GameShuffleCards/ToManyPlayersError.html
61
+ - doc/GameShuffleCards/TooManyCardsDemandedError.html
62
+ - doc/GameShuffleCards/ValidateGame.html
63
+ - doc/_index.html
64
+ - doc/class_list.html
65
+ - doc/css/common.css
66
+ - doc/css/full_list.css
67
+ - doc/css/style.css
68
+ - doc/file.README.html
69
+ - doc/file_list.html
70
+ - doc/frames.html
71
+ - doc/index.html
72
+ - doc/js/app.js
73
+ - doc/js/full_list.js
74
+ - doc/js/jquery.js
75
+ - doc/method_list.html
76
+ - doc/top-level-namespace.html
77
+ - features/deck_of_cards.feature
78
+ - features/initial_settings.feature
79
+ - features/step_definitions/deck_of_cards_step.rb
80
+ - features/step_definitions/initial_settings_step.rb
81
+ - features/step_definitions/step.rb
82
+ - game_shuffle_cards.gemspec
83
+ - lib/game_shuffle_cards.rb
84
+ - lib/game_shuffle_cards/card.rb
85
+ - lib/game_shuffle_cards/exceptions.rb
86
+ - lib/game_shuffle_cards/game.rb
87
+ - lib/game_shuffle_cards/validate_game.rb
88
+ - lib/game_shuffle_cards/version.rb
89
+ homepage: ''
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.4.5
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: A Game to shuffle a deck of cards and presents the result
113
+ test_files:
114
+ - features/deck_of_cards.feature
115
+ - features/initial_settings.feature
116
+ - features/step_definitions/deck_of_cards_step.rb
117
+ - features/step_definitions/initial_settings_step.rb
118
+ - features/step_definitions/step.rb
119
+ has_rdoc: yard