-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.rb
More file actions
264 lines (230 loc) · 6.28 KB
/
classes.rb
File metadata and controls
264 lines (230 loc) · 6.28 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
require 'timeout'
require 'colorize'
class Gameplay
attr_reader :referencedeck
attr_accessor :playerhand, :computerhand, :suits
def initialize
@exit_game = false
@gameover = false
@referencedeck = []
@playerhand = []
@computerhand = []
@gameplaydeck = []
@suits = %w[ Hearts Diamonds Clubs Spades ]
@snap = false
@logicstate = 0
end
def generate_refdeck
@suits.each do |x|
13.times {|y|
case y
when 0
@referencedeck << Card.new(x, "Ace")
when 1..9
@referencedeck << Card.new(x, (y + 1).to_s)
when 10
@referencedeck << Card.new(x, "Jack")
when 11
@referencedeck << Card.new(x, "Queen")
when 12
@referencedeck << Card.new(x, "King")
end
}
end
end
def gamebegin
puts `clear`
puts "Welcome to Coen Drexler's mental and existential snap!"
sleep(0.5)
puts "-----"
sleep(0.5)
while @exit_game == false
puts "Whenever you're ready to play, enter 1, and to exit enter E"
input = gets.strip.chomp
case input
when '1' || 1
puts "You can either play by suit (Spades - Spades) pairs, or by index (Ace - Ace) pairs"
sleep(1)
puts "Enter 'S' for suit pairs, and 'I' or anything else for normal index pairs"
gets.strip.chomp == 'S' ? @logicstate = 1 : @logicstate = 0
puts (@logicstate == 1 ? "Okay, special suit pairs it is!" : "Normal index pairs it is!")
@playerpoints = 0
@computerpoints = 0
@pot = []
@gameplaydeck = @referencedeck.shuffle
self.cards_sort(@gameplaydeck)
while @gameover == false
sleep(1)
self.startsnap
end
when 'E' || 'e'
@exit_game = true
else
puts "Invalid input"
end
end
end
def cards_sort(x)
26.times do |a|
@playerhand << x.pop
end
26.times do |a|
@computerhand << x.pop
end
end
def startsnap
roundcomplete = false
while roundcomplete == false
puts "--------------------------------------
Okay, whenever you feel ready, enter 1
--------------------------------------"
puts "
Your cards: #{@playerhand.count}
Computer's cards: #{@computerhand.count}
"
if gets.chomp == '1'
puts `clear`
@roundcardcomputer = nil
@roundcarduser = nil
@roundcardcomputer = @computerhand.pop
@roundcarduser = @playerhand.pop
puts "Computer:"
self.drawcard(@roundcardcomputer)
puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
puts "You:"
self.drawcard(@roundcarduser)
puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
self.cardcomparison(@roundcardcomputer,@roundcarduser)
@snap == false ? @randvalue = 1.5 : @randvalue = rand((0.45)..(1))
begin
@userinput = Timeout::timeout(@randvalue) do
gets
end
rescue Timeout::Error
@userinput = false
end
if @userinput != false
if @snap == true
puts "User: SNAP"
puts "You won!"
@playerpoints += 10
@computerpoints -= 10
self.readpoints
sleep(0.5)
@pot.each do |a|
@computerhand << @pot.pop
end
@pot = []
elsif @snap == false
puts "They didn't match sorry!"
@playerpoints -= 10
@computerpoints += 10
if @pot.count > 0
@pot.each {|a|
@playerhand << a
}
@pot = []
end
@playerhand << @roundcardcomputer
@playerhand << @roundcarduser
@playerhand.shuffle!
self.readpoints
end
else
if @snap == true
@computerpoints += 10
@playerpoints -= 10
puts "Computer: SNAP"
@pot.each {|a|
@playerhand << a
}
@pot = []
@playerhand << @roundcardcomputer
@playerhand << @roundcarduser
self.readpoints
else
puts "This round was a tie!"
@pot << @roundcardcomputer
@pot << @roundcarduser
self.readpoints
end
end
if @playerhand == 0 || @computerhand == 0
@gameover = true
else
end
else
puts "Invalid input sorry, put 1 to play"
end
end
end
def drawcard(a)
drawsuit = nil
drawvalue = nil
case a.suit
when "Clubs"
uni = "\u2667"
drawsuit = "#{(uni.encode('utf-8')).colorize(:white)}"
when "Diamonds"
uni = "\u2666"
drawsuit = "#{(uni.encode('utf-8')).colorize(:red)}"
when "Spades"
uni = "\u2664"
drawsuit = "#{(uni.encode('utf-8')).colorize(:white)}"
when "Hearts"
uni = "\u2665"
drawsuit = "#{(uni.encode('utf-8')).colorize(:red)}"
end
case a.value
when "Ace"
drawvalue = "A "
when '10'
drawvalue = a.value.to_s
when "Jack"
drawvalue = "J "
when "Queen"
drawvalue = "Q "
when "King"
drawvalue = "K "
else
drawvalue = a.value.to_s + " "
end
puts "
______
|#{drawvalue} |
| |
| |
|#{drawsuit}____|
"
end
def readpoints
puts "User: #{@playerpoints.to_s} points!"
puts "Computer: #{@computerpoints.to_s} points!
"
end
def cardcomparison(a,b)
if @logicstate == 0
if a.value == b.value
@snap = true
else
@snap = false
end
elsif @logicstate == 1
if a.suit == b.suit
@snap = true
else
@snap = false
end
end
end
end
class Card
attr_reader :suit, :value
def initialize(suit,value)
@suit = suit
@value = value
end
end
game = Gameplay.new
game.generate_refdeck
game.gamebegin