card_dealer 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e4f9b59efbde62a455eb8cc518b88e10819824ec42723a97947093ebda55ca88
4
+ data.tar.gz: 8be49f812cbde111460d824e308110dd2dcc5bda31248ab8d41fe8cbe6bf963f
5
+ SHA512:
6
+ metadata.gz: 271397f599f5d7e05c5cb86795506e5c97afb853d77eb575fa1d8ab612bd8641c3f4d4eea63aed3e4493c9742f01be98d6f2778e8b19c6483552f9ae78d549c2
7
+ data.tar.gz: abbd762746b5b86b1ee16b983888072e396648fa7fd9632c02c9234d1f7be145c9a2b95ad949f2231a1fd289ed539482d6346bae24ef612db5912d3b0d10071d
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,27 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 3.1
7
+ DisplayCopNames: true
8
+ DisplayStyleGuide: true
9
+ ExtraDetails: true
10
+ NewCops: enable
11
+
12
+ Style/StringLiterals:
13
+ Enabled: true
14
+ EnforcedStyle: double_quotes
15
+
16
+ Style/StringLiteralsInInterpolation:
17
+ Enabled: true
18
+ EnforcedStyle: double_quotes
19
+
20
+ Style/Documentation:
21
+ Enabled: false
22
+
23
+ Layout/LineLength:
24
+ Max: 120
25
+
26
+ RSpec/NestedGroups:
27
+ Max: 4
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-03-26
4
+
5
+ Initial release!
6
+
7
+ ### Added
8
+
9
+ - Card class
10
+ - Deck class
11
+ - Ability to create a decks of various size, ranks and suits
12
+ - Ability to shuffle a deck
13
+ - Ability to reset a deck
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in card_dealer.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "faker", "~> 3.1"
11
+ gem "rspec", "~> 3.12"
12
+ gem "simplecov", require: false, group: :test
13
+
14
+ gem "rubocop", "~> 1.28"
15
+ gem "rubocop-rake", "~> 0.6"
16
+ gem "rubocop-rspec", "~> 2.19"
data/Gemfile.lock ADDED
@@ -0,0 +1,80 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ card_dealer (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ concurrent-ruby (1.2.2)
11
+ diff-lcs (1.5.0)
12
+ docile (1.4.0)
13
+ faker (3.1.1)
14
+ i18n (>= 1.8.11, < 2)
15
+ i18n (1.12.0)
16
+ concurrent-ruby (~> 1.0)
17
+ json (2.6.3)
18
+ parallel (1.22.1)
19
+ parser (3.2.1.1)
20
+ ast (~> 2.4.1)
21
+ rainbow (3.1.1)
22
+ rake (13.0.6)
23
+ regexp_parser (2.7.0)
24
+ rexml (3.2.5)
25
+ rspec (3.12.0)
26
+ rspec-core (~> 3.12.0)
27
+ rspec-expectations (~> 3.12.0)
28
+ rspec-mocks (~> 3.12.0)
29
+ rspec-core (3.12.1)
30
+ rspec-support (~> 3.12.0)
31
+ rspec-expectations (3.12.2)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.12.0)
34
+ rspec-mocks (3.12.4)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.12.0)
37
+ rspec-support (3.12.0)
38
+ rubocop (1.48.1)
39
+ json (~> 2.3)
40
+ parallel (~> 1.10)
41
+ parser (>= 3.2.0.0)
42
+ rainbow (>= 2.2.2, < 4.0)
43
+ regexp_parser (>= 1.8, < 3.0)
44
+ rexml (>= 3.2.5, < 4.0)
45
+ rubocop-ast (>= 1.26.0, < 2.0)
46
+ ruby-progressbar (~> 1.7)
47
+ unicode-display_width (>= 2.4.0, < 3.0)
48
+ rubocop-ast (1.28.0)
49
+ parser (>= 3.2.1.0)
50
+ rubocop-capybara (2.17.1)
51
+ rubocop (~> 1.41)
52
+ rubocop-rake (0.6.0)
53
+ rubocop (~> 1.0)
54
+ rubocop-rspec (2.19.0)
55
+ rubocop (~> 1.33)
56
+ rubocop-capybara (~> 2.17)
57
+ ruby-progressbar (1.13.0)
58
+ simplecov (0.22.0)
59
+ docile (~> 1.1)
60
+ simplecov-html (~> 0.11)
61
+ simplecov_json_formatter (~> 0.1)
62
+ simplecov-html (0.12.3)
63
+ simplecov_json_formatter (0.1.4)
64
+ unicode-display_width (2.4.2)
65
+
66
+ PLATFORMS
67
+ arm64-darwin-22
68
+
69
+ DEPENDENCIES
70
+ card_dealer!
71
+ faker (~> 3.1)
72
+ rake (~> 13.0)
73
+ rspec (~> 3.12)
74
+ rubocop (~> 1.28)
75
+ rubocop-rake (~> 0.6)
76
+ rubocop-rspec (~> 2.19)
77
+ simplecov
78
+
79
+ BUNDLED WITH
80
+ 2.4.9
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Leonid Svyatov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # CardDealer
2
+
3
+ This gem allows you to generate a deck of cards, shuffle them, and deal them to players.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ ## Usage
16
+
17
+ TBD
18
+
19
+ ## Development
20
+
21
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
22
+
23
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
24
+
25
+ ## Contributing
26
+
27
+ Bug reports and pull requests are welcome on GitHub at https://github.com/svyatov/card_dealer.
28
+
29
+ ## License
30
+
31
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CardDealer
4
+ class Card
5
+ CLUBS = "c"
6
+ DIAMONDS = "d"
7
+ HEARTS = "h"
8
+ SPADES = "s"
9
+
10
+ # T = 10, J = Jack, Q = Queen, K = King, A = Ace
11
+ RANKS = %w[2 3 4 5 6 7 8 9 T J Q K A].freeze
12
+ SUITS = [CLUBS, DIAMONDS, HEARTS, SPADES].freeze
13
+
14
+ # For faster lookup
15
+ RANKS_SET = RANKS.to_set.freeze
16
+ SUITS_SET = SUITS.to_set.freeze
17
+
18
+ attr_reader :rank, :suit
19
+
20
+ # Initializes a new card.
21
+ #
22
+ # @param rank [String] the card's rank (2-9, T, J, Q, K, A)
23
+ # @param suit [String] the card's suit (c, d, h, s)
24
+ def initialize(rank, suit)
25
+ validate_arguments rank, suit
26
+
27
+ @rank = rank
28
+ @suit = suit
29
+ end
30
+
31
+ # @return [String] the card's rank and suit
32
+ def to_s
33
+ "#{rank}#{suit}"
34
+ end
35
+
36
+ # @return [String] the card's object string representation
37
+ def inspect
38
+ %(#<CardDealer::Card "#{self}">)
39
+ end
40
+
41
+ private
42
+
43
+ def validate_arguments(rank, suit)
44
+ raise Error, "Invalid rank: #{rank}" unless RANKS_SET.include?(rank)
45
+ raise Error, "Invalid suit: #{suit}" unless SUITS_SET.include?(suit)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CardDealer
4
+ class Deck
5
+ attr_reader :cards, :seed
6
+
7
+ # Initializes a new deck.
8
+ #
9
+ # @param decks [Integer] the number of decks to use
10
+ # @param cards_per_suit [Integer] the number of cards per suit to use
11
+ # @param ranks [Array<String>, :highest, :lowest] the ranks to use
12
+ # @option :highest will use the highest cards_per_suit ranks (A, K, Q, J, T, ...)
13
+ # :lowest will use the lowest cards_per_suit ranks (2, 3, 4, 5, 6, ...)
14
+ # array will use the specified ranks (e.g. %w[2 4 6 8 T])
15
+ # @param suits [Array<String>, :all] the suits to use
16
+ # :all will use all suits (c, d, h, s)
17
+ # array will use the specified suits (e.g. %w[d h])
18
+ # @raise [Error] if ranks or suits are invalid
19
+ def initialize(decks: 1, cards_per_suit: 13, ranks: :highest, suits: :all)
20
+ @decks = decks
21
+ @ranks = build_ranks ranks, cards_per_suit
22
+ @suits = build_suits suits
23
+ reset
24
+ end
25
+
26
+ # Resets the deck to its original state.
27
+ #
28
+ # @return [Deck]
29
+ def reset
30
+ @cards = []
31
+
32
+ @decks.times do
33
+ @suits.each do |suit|
34
+ @ranks.each do |rank|
35
+ @cards << Card.new(rank, suit)
36
+ end
37
+ end
38
+ end
39
+
40
+ self
41
+ end
42
+
43
+ # Shuffles the deck using the Fisher-Yates algorithm.
44
+ #
45
+ # @param seed [Integer] the seed to use for shuffling
46
+ # @return [Deck]
47
+ def shuffle(seed = Random.new_seed)
48
+ @seed = seed
49
+ @cards.shuffle! random: Random.new(seed)
50
+ self
51
+ end
52
+
53
+ # @return [Array<String>] the array of cards in the deck as strings
54
+ def to_a
55
+ cards.map(&:to_s)
56
+ end
57
+
58
+ # @return [String] the cards in the deck as a string
59
+ def to_s
60
+ to_a.to_s
61
+ end
62
+
63
+ private
64
+
65
+ def build_suits(suits)
66
+ case suits
67
+ when :all
68
+ Card::SUITS
69
+ when Array
70
+ suits
71
+ else
72
+ raise Error, "Invalid suits: #{suits}"
73
+ end
74
+ end
75
+
76
+ def build_ranks(ranks, cards_per_suit)
77
+ case ranks
78
+ when :highest
79
+ Card::RANKS.last(cards_per_suit)
80
+ when :lowest
81
+ Card::RANKS.first(cards_per_suit)
82
+ when Array
83
+ ranks
84
+ else
85
+ raise Error, "Invalid ranks: #{ranks}"
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CardDealer
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ require_relative "card_dealer/version"
6
+ require_relative "card_dealer/card"
7
+ require_relative "card_dealer/deck"
8
+
9
+ module CardDealer
10
+ class Error < StandardError; end
11
+ end
@@ -0,0 +1,25 @@
1
+ module CardDealer
2
+ class Card
3
+ CLUBS: 'c'
4
+ DIAMONDS: 'd'
5
+ HEARTS: 'h'
6
+ SPADES: 's'
7
+
8
+ RANKS: Array[String]
9
+ SUITS: Array[String]
10
+
11
+ RANKS_SET: Set[String]
12
+ SUITS_SET: Set[String]
13
+
14
+ attr_reader rank: String
15
+ attr_reader suit: String
16
+
17
+ def initialize: (rank: String, suit: String) -> void
18
+
19
+ def to_s: -> String
20
+
21
+ private
22
+
23
+ def validate_arguments: (rank: String, suit: String) -> void
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ module CardDealer
2
+ class Deck
3
+ attr_reader cards: Array[Card]
4
+ attr_reader seed: Integer
5
+
6
+ @decks: Integer
7
+ @ranks: :highest | :lowest | Array[String]
8
+ @suits: :all | Array[String]
9
+
10
+ def initialize: (
11
+ ?decks: Integer,
12
+ ?cards_per_suit: Integer,
13
+ ?ranks: :highest | :lowest | Array[String],
14
+ ?suits: :all | Array[String]) -> void
15
+
16
+ def reset: -> self
17
+
18
+ def shuffle: (?seed: Integer) -> self
19
+
20
+ def to_a: -> Array[String]
21
+
22
+ def to_s: -> String
23
+
24
+ private
25
+
26
+ def build_suits: (suits: :all | Array[String]) -> Array[String]
27
+
28
+ def build_ranks: (ranks: :highest | :lowest | Array[String]) -> Array[String]
29
+ end
30
+ end
@@ -0,0 +1,4 @@
1
+ module CardDealer
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: card_dealer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Leonid Svyatov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-03-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Card dealer allows to generate a deck of cards, shuffle them, and deal
14
+ them to players.
15
+ email:
16
+ - leonid@svyatov.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rspec"
22
+ - ".rubocop.yml"
23
+ - CHANGELOG.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - lib/card_dealer.rb
30
+ - lib/card_dealer/card.rb
31
+ - lib/card_dealer/deck.rb
32
+ - lib/card_dealer/version.rb
33
+ - sig/card_dealer.rbs
34
+ - sig/card_dealer/card.rbs
35
+ - sig/card_dealer/deck.rbs
36
+ homepage: https://github.com/svyatov/card_dealer
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ homepage_uri: https://github.com/svyatov/card_dealer
41
+ source_code_uri: https://github.com/svyatov/card_dealer
42
+ changelog_uri: https://github.com/svyatov/card_dealer/blob/master/CHANGELOG.md
43
+ rubygems_mfa_required: 'true'
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 3.1.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.4.9
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: It deals with cards. It's a card dealer.
63
+ test_files: []