deck_of_cards_handler 0.1.2 → 0.1.3
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 +4 -4
- data/lib/deck_of_cards_handler/card/card.rb +29 -0
- data/lib/deck_of_cards_handler/packet/packet.rb +24 -0
- data/lib/deck_of_cards_handler/version.rb +1 -1
- data/sorbet/config +1 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 429d1307ee681bdbe06c04a5b4232a106d2ad4a0f07cbe684eaab13254e9f402
|
4
|
+
data.tar.gz: 4d89bba0e091028380bb6481dacf2f48a97b3df1147a67a0f8c3bfbc67a3997b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 971705372f4d56fc9d63d44f0d0c7b1a999209ff6f197084f8e951a58689ba45448c62ef3608eca4f97784dc9b6a4c8474aca0ce58de02e5811a3d9c654d47f4
|
7
|
+
data.tar.gz: bbe3e25a0fe903c1e687455225d64aeacd033d726d910f0516a7de7b83d626aa65e9d328234f654326a86f74c90a7f479c043aabe31a696f0259eaa99c400d7f
|
@@ -45,6 +45,35 @@ class Card
|
|
45
45
|
"#{value} of #{suit}"
|
46
46
|
end
|
47
47
|
|
48
|
+
sig { params(other: Card).returns(T::Boolean) }
|
49
|
+
def ==(other)
|
50
|
+
other.suit == suit && other.value == value
|
51
|
+
end
|
52
|
+
|
53
|
+
sig { returns(T::Array[Integer]) }
|
54
|
+
def rank
|
55
|
+
case value
|
56
|
+
when "A" then [1, 14]
|
57
|
+
when "J" then [11]
|
58
|
+
when "Q" then [12]
|
59
|
+
when "K" then [13]
|
60
|
+
else [value.to_i]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
sig { params(other: Card).returns(Integer) }
|
65
|
+
def <=>(other)
|
66
|
+
# compare by all possible ranks (Ace flexible)
|
67
|
+
my_ranks = rank
|
68
|
+
other_ranks = other.rank
|
69
|
+
|
70
|
+
# check if any ranks overlap (Ace == 1 or 14)
|
71
|
+
return 0 if (my_ranks & other_ranks).any?
|
72
|
+
|
73
|
+
# otherwise compare by highest possible
|
74
|
+
T.must(my_ranks.max) <=> T.must(other_ranks.max)
|
75
|
+
end
|
76
|
+
|
48
77
|
class << self
|
49
78
|
extend T::Sig
|
50
79
|
sig { returns(T::Array[String]) }
|
@@ -52,6 +52,30 @@ class Packet
|
|
52
52
|
packet
|
53
53
|
end
|
54
54
|
|
55
|
+
sig { params(string: String).returns(Packet) }
|
56
|
+
def build_from_string(string:) # rubocop:disable Metrics
|
57
|
+
content = string.split(",")
|
58
|
+
cards = []
|
59
|
+
cards_set = Set.new
|
60
|
+
|
61
|
+
content.each do |line|
|
62
|
+
value, suit = line.chomp.split(":")
|
63
|
+
raise StandardError unless value && suit
|
64
|
+
|
65
|
+
card = Card.new(suit:, value:)
|
66
|
+
card_string = card.to_s
|
67
|
+
raise StandardError, "Duplicate card. (#{card_string})" if cards_set.include?(card_string)
|
68
|
+
|
69
|
+
cards_set << card_string
|
70
|
+
cards << card
|
71
|
+
end
|
72
|
+
|
73
|
+
packet = Packet.new(cards:)
|
74
|
+
set_cards_positions(packet:)
|
75
|
+
|
76
|
+
packet
|
77
|
+
end
|
78
|
+
|
55
79
|
private
|
56
80
|
|
57
81
|
sig { params(packet: Packet).void }
|
data/sorbet/config
CHANGED