Class: Packet

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Cuts, Deals, Shuffles
Defined in:
lib/deck_of_cards_handler/packet/packet.rb

Overview

This represents a packet of cards

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shuffles

faro_shuffle, #overhand_shuffle, riffle_shuffle

Methods included from Cuts

#cut, #cut_and_complete, #reassemble_left_to_right_on_bottom, #reassemble_left_to_right_on_top, #reassemble_right_to_left_on_bottom, #reassemble_right_to_left_on_top

Methods included from Deals

#bottom_deal, #deal_into_piles, #second_deal, #top_deal

Constructor Details

#initialize(cards: []) ⇒ Packet

Returns a new instance of Packet.



19
20
21
# File 'lib/deck_of_cards_handler/packet/packet.rb', line 19

def initialize(cards: [])
  @cards = T.let(cards, T::Array[Card])
end

Instance Attribute Details

#cardsObject

Returns the value of attribute cards.



16
17
18
# File 'lib/deck_of_cards_handler/packet/packet.rb', line 16

def cards
  @cards
end

Class Method Details

.build_from_string(string:) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/deck_of_cards_handler/packet/packet.rb', line 56

def build_from_string(string:)
  content = string.split(",").map(&:strip)
  cards = []
  cards_set = Set.new

  content.each do |line|
    value, suit = line.chomp.split(":")
    raise StandardError unless value && suit

    card = Card.new(suit:, value:)
    card_string = card.to_s
    raise StandardError, "Duplicate card. (#{card_string})" if cards_set.include?(card_string)

    cards_set << card_string
    cards << card
  end

  Packet.new(cards:)
end

.build_from_text_file(file_path:) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/deck_of_cards_handler/packet/packet.rb', line 32

def build_from_text_file(file_path:)
  file_content = File.read(file_path)
  cards = []
  cards_set = Set.new

  file_content.each_line do |line|
    value, suit = line.chomp.split(":")
    raise StandardError unless value && suit

    card = Card.new(suit:, value:)
    card_string = card.to_s
    raise StandardError, "Duplicate card. (#{card_string})" if cards_set.include?(card_string)

    cards_set << card_string
    cards << card
  end

  packet = Packet.new(cards:)
  packet.set_cards_positions

  packet
end

Instance Method Details

#cull(from:, to:) ⇒ Object

Raises:

  • (ArgumentError)


100
101
102
103
104
105
# File 'lib/deck_of_cards_handler/packet/packet.rb', line 100

def cull(from:, to:)
  raise ArgumentError, "`from` must be in range with 1..#{size}" if from <= 0 || from > size
  raise ArgumentError, "`to` must be in range with 1..#{size}" if to <= 0 || to > size

  cards.insert(to - 1, T.must(cards.delete_at(from - 1)))
end

#faro(other_packet:) ⇒ Object



78
79
80
# File 'lib/deck_of_cards_handler/packet/packet.rb', line 78

def faro(other_packet:)
  self.cards = Shuffles.faro_shuffle(top_half: self, bottom_half: other_packet)
end

#reverseObject



93
94
95
# File 'lib/deck_of_cards_handler/packet/packet.rb', line 93

def reverse
  self.cards = cards.reverse
end

#riffle_shuffle(other_packet:) ⇒ Object



83
84
85
# File 'lib/deck_of_cards_handler/packet/packet.rb', line 83

def riffle_shuffle(other_packet:)
  self.cards = Shuffles.riffle_shuffle(left_half: self, right_half: other_packet)
end

#set_cards_positionsObject



108
109
110
111
112
# File 'lib/deck_of_cards_handler/packet/packet.rb', line 108

def set_cards_positions
  cards.each_with_index do |card, index|
    card.position = index + 1
  end
end

#shuffleObject



88
89
90
# File 'lib/deck_of_cards_handler/packet/packet.rb', line 88

def shuffle
  self.cards = cards.shuffle
end

#sizeObject



24
25
26
# File 'lib/deck_of_cards_handler/packet/packet.rb', line 24

def size
  cards.size
end

#to_poker_handObject



134
135
136
# File 'lib/deck_of_cards_handler/packet/packet.rb', line 134

def to_poker_hand
  PokerHands::PokerHand.create(cards:)
end

#to_sObject



115
116
117
# File 'lib/deck_of_cards_handler/packet/packet.rb', line 115

def to_s
  cards.map(&:to_s)
end