-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.rb
74 lines (60 loc) · 1.15 KB
/
card.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Card
include Comparable
attr_reader :number, :power
attr_reader :suit
def initialize(number, suit)
@number = number
@suit = suit
@power = ([2, 1] + (3..13).to_a.reverse).reverse
end
def <=>(other)
@power.index(@number) - @power.index(other.number)
end
end
class Deck
attr_reader :cards
SUITS = %w(heart diamond spade crub).freeze
def initialize(*players)
@cards = SUITS.map do |suit|
(1..13).map { |num| Card.new(num, suit) } end.flatten
@players = players
end
def shuffle
@cards.shuffle
end
def distribute
loop do
@players.each do |player|
return if @cards.empty?
player.draw(pick!)
end
end
end
def pick!
@cards.pop
end
def first_player
@players.find do |player|
player.cards.find {|c| c.suit == 'diamond' && c.number == 3 }
end
end
end
class Player
attr_accessor :cards
def initialize
@cards = []
end
def draw(card)
@cards << card
end
end
class CardSet
attr_reader :cards
include Comparable
def initialize(*cards)
@cards = cards
end
def <=>(other)
@cards.first <=> other.cards.first
end
end