-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcard.rb
More file actions
95 lines (66 loc) · 1.84 KB
/
card.rb
File metadata and controls
95 lines (66 loc) · 1.84 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
class Card
#attr_reader :rank, :suit #allow people to get / read the value of the rank and the suit exposes the val outside the class
#attr_writer :rank, :suit # people to change the value
attr_accessor :rank, :suit # allows to get /change values merges both
def initialize(rank, suit)
@rank = rank #instance variable @var / saves the rank
@suit = suit #settting up instance vars to be reffered elsewhere
end
#def rank
#@rank #exposes rank and lets you use it /if soomeone asks for rank here it is
#end
#def rank=(rank) #method or function
# @rank = rank
# end
#encapsulation - hiding the innerworkings as much as possible distrust assume the worst
def output_card #method or function
puts "#{self.rank} of #{self.suit}" #self. accesses teh object itself whats the thing in context what si the thing were dealing with / the specific instance
end
def self.random_card #class method / self. method lets you run a method without an instance
Card.new(rand(10), :spades) # not commonly used REFACTORED -move code around
end
end
class Deck
def initialize
@cards = []
@rank = [:ace, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, :Jack, :Queen, :King]
@suit = [:s
pades, :Hearts, :Diamonds, :Clubs]
@rank.each do |rank|
@suit.each do |suit|
@cards<<Card.new(rank,suit)
end
end
end
def shuffle
@cards.shuffle!
end
def output
@cards.each do |card|
card.output_card
end
end
def deal
@cards.shift
end
end
deck = Deck.new
deck.shuffle
deck.deal
deck.output
#card = Card.random_card #triggers initialize
#card.rank = 9 #changes rnk value
#card.output_card
class Card
def output_hello
puts "Hello"
end
end
class Deck
def create_card_and_output
card = Card.new
card.output_hello
end
end
deck = Deck.new
deck.create_card_and_output