deck_of_cards_handler 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: 1b2047d98e01703a7121033f7f3fe003222b97d72e7e1d2bfee7c53e2ad2b8f5
4
+ data.tar.gz: '0786c32ac1a61c75a1f37c5e89b1bb52f70e1e578c8c471bf7c49b648ba14a20'
5
+ SHA512:
6
+ metadata.gz: b07650c2422846f715bd3e5ba4d63a02e984cc08067a328544dff5da67c016dd0c3aef9fa99ee657e2fe06b036bd030ddab0aadae5f70bdf11aafa0bbbff5203
7
+ data.tar.gz: 699e114cd1c825d53104a7611a546d9c963b43bfbc9acb2e1fef9c6cba9cb5950da6753018958d6164824c2dcc56b9674961856c1b4eec4704efd3d9934a2b91
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-09-26
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Simon Bernard
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,38 @@
1
+ # DeckOfCards
2
+
3
+ This is a gem that simulates the handling of a deck of cards.
4
+ It provides all the moves one could do such as shuffling, cutting, dealing, culling, etc.
5
+
6
+ ## Installation
7
+
8
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
9
+
10
+ Install the gem and add to the application's Gemfile by executing:
11
+
12
+ ```bash
13
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+ ```
15
+
16
+ If bundler is not being used to manage dependencies, install the gem by executing:
17
+
18
+ ```bash
19
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ TODO: Write usage instructions here
25
+
26
+ ## Development
27
+
28
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
29
+
30
+ 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).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/simonbernard2/deck_of_cards>.
35
+
36
+ ## License
37
+
38
+ 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 "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
@@ -0,0 +1,14 @@
1
+ A:H
2
+ 2:H
3
+ 3:H
4
+ 4:H
5
+ 5:H
6
+ 6:H
7
+ 7:H
8
+ 8:H
9
+ 9:H
10
+ 10:H
11
+ 10:H
12
+ 10:H
13
+ 10:H
14
+ J:H
@@ -0,0 +1,52 @@
1
+ 4:C
2
+ 2:H
3
+ 7:D
4
+ 3:C
5
+ 4:H
6
+ 6:D
7
+ A:S
8
+ 5:H
9
+ 9:S
10
+ 2:S
11
+ Q:H
12
+ 3:D
13
+ Q:C
14
+ 8:H
15
+ 6:S
16
+ 5:S
17
+ 9:H
18
+ K:C
19
+ 2:D
20
+ J:H
21
+ 3:S
22
+ 8:S
23
+ 6:H
24
+ 10:C
25
+ 5:D
26
+ K:D
27
+ 2:C
28
+ 3:H
29
+ 8:D
30
+ 5:C
31
+ K:S
32
+ J:D
33
+ 8:C
34
+ 10:S
35
+ K:H
36
+ J:C
37
+ 7:S
38
+ 10:H
39
+ A:D
40
+ 4:S
41
+ 7:H
42
+ 4:D
43
+ A:C
44
+ 9:C
45
+ J:S
46
+ Q:D
47
+ 7:C
48
+ Q:S
49
+ 10:D
50
+ 6:C
51
+ A:H
52
+ 9:D
@@ -0,0 +1,70 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ # This represents a Playing Card.
5
+ # It has the following properties: a suit, a value and a position.
6
+ class Card
7
+ extend T::Sig
8
+
9
+ sig { returns(String) }
10
+ attr_accessor :suit
11
+
12
+ sig { returns(String) }
13
+ attr_accessor :value
14
+
15
+ sig { returns(T.nilable(Integer)) }
16
+ attr_accessor :position
17
+
18
+ sig { params(suit: String, value: String, position: T.nilable(Integer)).void }
19
+ def initialize(suit:, value:, position: nil)
20
+ raise ArgumentError, "Invalid suit" unless Card.suits.include?(suit)
21
+ raise ArgumentError, "Invalid value" unless Card.values.include?(value)
22
+
23
+ @suit = T.let(suit, String)
24
+ @value = T.let(value, String)
25
+ @position = T.let(position, T.nilable(Integer))
26
+ end
27
+
28
+ sig { returns(String) }
29
+ def color
30
+ red? ? "red" : "black"
31
+ end
32
+
33
+ sig { returns(T::Boolean) }
34
+ def red?
35
+ Card.red_suits.include?(suit)
36
+ end
37
+
38
+ sig { returns(T::Boolean) }
39
+ def black?
40
+ Card.black_suits.include?(suit)
41
+ end
42
+
43
+ sig { returns(String) }
44
+ def to_s
45
+ "#{value} of #{suit}"
46
+ end
47
+
48
+ class << self
49
+ extend T::Sig
50
+ sig { returns(T::Array[String]) }
51
+ def suits
52
+ %w[C H S D].freeze
53
+ end
54
+
55
+ sig { returns(T::Array[String]) }
56
+ def red_suits
57
+ %w[H D].freeze
58
+ end
59
+
60
+ sig { returns(T::Array[String]) }
61
+ def black_suits
62
+ %w[S C].freeze
63
+ end
64
+
65
+ sig { returns(T::Array[String]) }
66
+ def values
67
+ %w[A 2 3 4 5 6 7 8 9 10 J Q K].freeze
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "card/card"
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ # Includes cutting methods, either splits or assembles packets of cards
5
+ module Cuts
6
+ extend T::Helpers
7
+ extend T::Sig
8
+ requires_ancestor { Packet }
9
+
10
+ sig { params(number: Integer).returns(Packet) }
11
+ def cut(number:)
12
+ raise ArgumentError if invalid_number_to_cut_to?(number)
13
+
14
+ cut_cards = cards.slice!(0...number)
15
+ Packet.new(cards: T.must(cut_cards))
16
+ end
17
+
18
+ sig { params(number: Integer).void }
19
+ def cut_and_complete(number:)
20
+ top_half = cut(number:)
21
+ self.cards = [cards, top_half.cards].flatten
22
+ end
23
+
24
+ private
25
+
26
+ sig { params(number: Integer).returns(T::Boolean) }
27
+ def invalid_number_to_cut_to?(number)
28
+ return true if number.negative?
29
+ return true if number.zero?
30
+ return true if number >= cards.size
31
+
32
+ false
33
+ end
34
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ # Includes dealing methods, that either returns a single Card or Packet(s) of cards
5
+ module Deals
6
+ extend T::Helpers
7
+ extend T::Sig
8
+
9
+ requires_ancestor { Packet }
10
+ sig { returns(Card) }
11
+ def top_deal
12
+ raise StandardError if size.zero?
13
+
14
+ T.must(cards.slice!(0))
15
+ end
16
+
17
+ sig { returns(Card) }
18
+ def second_deal
19
+ raise StandardError if size < 2
20
+
21
+ T.must(cards.slice!(1))
22
+ end
23
+
24
+ sig { returns(Card) }
25
+ def bottom_deal
26
+ raise StandardError if size.zero?
27
+
28
+ T.must(cards.pop)
29
+ end
30
+
31
+ sig { params(number_of_piles: Integer, number_of_cards: Integer).returns(T::Array[Packet]) }
32
+ def deal_into_piles(number_of_piles:, number_of_cards:)
33
+ raise StandardError, "Not enough cards" if (number_of_cards * number_of_piles) > size
34
+ raise StandardError, "Invalid number_of_cards" unless number_of_cards.positive?
35
+
36
+ piles = Array.new(number_of_piles) { [] }
37
+ number_of_cards.times do
38
+ piles.each { |pile| pile << top_deal }
39
+ end
40
+
41
+ piles.map { Packet.new(cards: _1) }
42
+ end
43
+ end
@@ -0,0 +1,89 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ # This represents a packet of cards
5
+ class Packet
6
+ extend T::Sig
7
+ require "deck_of_cards/packet/shuffles"
8
+ require "deck_of_cards/packet/deals"
9
+ require "deck_of_cards/packet/cuts"
10
+ include Deals
11
+ include Cuts
12
+
13
+ sig { returns(T::Array[Card]) }
14
+ attr_accessor :cards
15
+
16
+ sig { params(cards: T::Array[Card]).void }
17
+ def initialize(cards:)
18
+ raise ArgumentError if cards.empty?
19
+
20
+ @cards = T.let(cards, T::Array[Card])
21
+ end
22
+
23
+ sig { returns(Integer) }
24
+ def size
25
+ cards.size
26
+ end
27
+
28
+ class << self
29
+ extend T::Sig
30
+
31
+ sig { params(file_path: String).returns(Packet) }
32
+ def build_from_text_file(file_path:) # rubocop:disable Metrics
33
+ file_content = File.read(file_path)
34
+ cards = []
35
+ cards_set = Set.new
36
+
37
+ file_content.each_line do |line|
38
+ value, suit = line.chomp.split(":")
39
+ raise StandardError unless value && suit
40
+
41
+ card = Card.new(suit:, value:)
42
+ card_string = card.to_s
43
+ raise StandardError, "Duplicate card. (#{card_string})" if cards_set.include?(card_string)
44
+
45
+ cards_set << card_string
46
+ cards << card
47
+ end
48
+
49
+ packet = Packet.new(cards:)
50
+ set_cards_positions(packet:)
51
+
52
+ packet
53
+ end
54
+
55
+ private
56
+
57
+ sig { params(packet: Packet).void }
58
+ def set_cards_positions(packet:)
59
+ packet.cards.each_with_index do |card, index|
60
+ card.position = index + 1
61
+ end
62
+ end
63
+ end
64
+
65
+ sig { params(other_packet: Packet).void }
66
+ def faro(other_packet:)
67
+ self.cards = Shuffles.faro_shuffle(top_half: self, bottom_half: other_packet)
68
+ end
69
+
70
+ sig { params(other_packet: Packet).void }
71
+ def riffle_shuffle(other_packet:)
72
+ self.cards = Shuffles.riffle_shuffle(left_half: self, right_half: other_packet)
73
+ end
74
+
75
+ sig { void }
76
+ def shuffle
77
+ self.cards = cards.shuffle
78
+ end
79
+
80
+ sig { void }
81
+ def reverse
82
+ self.cards = cards.reverse
83
+ end
84
+
85
+ sig { returns(T::Array[String]) }
86
+ def to_s
87
+ cards.map(&:to_s)
88
+ end
89
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+ # typed: strict
3
+
4
+ module Shuffles
5
+ class << self
6
+ extend T::Sig
7
+ sig { params(left_half: Packet, right_half: Packet).returns(T::Array[Card]) }
8
+ def riffle_shuffle(left_half:, right_half:) # rubocop:disable Metrics/AbcSize
9
+ shuffled_cards = []
10
+ until left_half.cards.empty? && right_half.cards.empty?
11
+ if !left_half.cards.empty? && (right_half.cards.empty? || Kernel.rand < 0.5)
12
+ shuffled_cards.concat(left_half.cards.shift(Kernel.rand(1..3)))
13
+ else
14
+ shuffled_cards.concat(right_half.cards.shift(Kernel.rand(1..3)))
15
+ end
16
+ end
17
+ shuffled_cards
18
+ end
19
+
20
+ sig { params(top_half: Packet, bottom_half: Packet).returns(T::Array[Card]) }
21
+ def faro_shuffle(top_half:, bottom_half:)
22
+ top_cards = top_half.cards
23
+ bottom_cards = bottom_half.cards
24
+ remaining_cards = []
25
+
26
+ remaining_cards = top_cards[bottom_cards.size..top_cards.size] if bottom_cards.size < top_cards.size
27
+
28
+ zipped_cards = bottom_cards.zip(top_cards).flatten.compact
29
+ [zipped_cards, remaining_cards].flatten.compact
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "packet/packet"
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeckOfCards
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sorbet-runtime"
4
+ require_relative "deck_of_cards/version"
5
+ require_relative "deck_of_cards/card"
6
+ require_relative "deck_of_cards/packet"
@@ -0,0 +1,4 @@
1
+ module DeckOfCards
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deck_of_cards_handler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Simon Bernard
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: sorbet-runtime
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '0.6'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '0.6'
26
+ - !ruby/object:Gem::Dependency
27
+ name: sorbet
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '0.6'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.6'
40
+ - !ruby/object:Gem::Dependency
41
+ name: tapioca
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.17'
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 0.17.7
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '0.17'
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 0.17.7
60
+ description: |2
61
+ It provides all the moves one could do with a deck of cards.
62
+ Such as shuffling, cutting, dealing, culling, etc.
63
+ email:
64
+ - simonbernard@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - ".rubocop.yml"
70
+ - CHANGELOG.md
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - data/duplicate_cards.txt
75
+ - data/mnemonica.txt
76
+ - lib/deck_of_cards.rb
77
+ - lib/deck_of_cards/card.rb
78
+ - lib/deck_of_cards/card/card.rb
79
+ - lib/deck_of_cards/packet.rb
80
+ - lib/deck_of_cards/packet/cuts.rb
81
+ - lib/deck_of_cards/packet/deals.rb
82
+ - lib/deck_of_cards/packet/packet.rb
83
+ - lib/deck_of_cards/packet/shuffles.rb
84
+ - lib/deck_of_cards/version.rb
85
+ - sig/deck_of_cards.rbs
86
+ homepage: https://rubygems.org/gems/deck_of_cards_handler
87
+ licenses:
88
+ - MIT
89
+ metadata:
90
+ homepage_uri: https://rubygems.org/gems/deck_of_cards_handler
91
+ source_code_uri: https://github.com/simonbernard2/deck_of_cards
92
+ changelog_uri: https://github.com/simonbernard2/deck_of_cards/blob/main/CHANGELOG.md
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: 3.1.0
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.6.9
108
+ specification_version: 4
109
+ summary: A gem that simulates the handling of a deck of cards
110
+ test_files: []