-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobj_10.rb
executable file
·76 lines (66 loc) · 1.16 KB
/
robj_10.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
75
76
#!/usr/bin/env ruby
require "./ruler"
#-----------------------------------------------------------------------------
R("")
module CardData
@@suits = %w{h d c d}
@@ranks = %w{2 3 4 5}
end
class Card
attr_accessor :suit, :rank
def initialize(s, r)
self.suit = s
self.rank = r
end
def inspect
"CARD(suit: #{self.suit} rank: #{self.rank})"
end
end
# The fact that Deck inherits after Array makes it get an access to Array's
# methods.
class Deck < Array
include CardData
attr_reader :shuffled
attr_writer :testing
def initialize
puts "-- Deck --"
@@suits.each do |s|
@@ranks.each do |r|
puts "=== s=#{s} r=#{r}"
push Card.new(s, r)
end
end
@shuffled = false
end
def testing_start
@testing = 1
end
def shuffle!
size.downto(1) do |n|
pos = rand(n)
if @testing then
pos = 1
end
push delete_at pos
end
@shuffled = true
end
def deal(n)
if size - n < 0
puts "Not enough cards to deal #{n}"
else
if not shuffled
puts "Warning: dealing from unshuffled deck"
end
slice!(0, n)
end
end
end
d = Deck.new
d.testing_start
hand = d.deal(5)
p hand
d.shuffle!
hand = d.deal(5)
p hand
hand = d.deal(100)