-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbj.rb
More file actions
222 lines (186 loc) · 4.13 KB
/
Copy pathbj.rb
File metadata and controls
222 lines (186 loc) · 4.13 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
# STDOUT is not put immediately without a code below
STDOUT.sync = true
class HandOfCards
def initialize
@@deck = Array.new
@hand = Array.new
end
def put_deck
print @@deck
end
attr_reader :deck, :hand
def refresh_hand
@hand = Array.new
end
def draw_a_card
card = @@deck.sample
@@deck.delete_at(@@deck.find_index(card))
@hand.push(card)
end
def evaluate_hand
total = [0,0]
@hand.each{|card|
if card > 10 then
total.map! {|value| value += 10 }
elsif card == 1 then
total[0] += card
total[1] += (total[1] + 11) <= BJ ? 11 : card
else
total.map! {|value| value += card }
end
}
if total[0] > BJ then
return 'Bust'
elsif total.find_index(BJ) != nil then
return [BJ]
elsif total[0] == total[1] or total[1] > BJ then
return [total[0]]
else
return total
end
end
def show_hand(hand=@hand)
print '['
hand.map {|card|
case card
when 1 then
print 'A, '
when 11 then
print 'J, '
when 12 then
print 'Q, '
when 13 then
print 'K, '
else
print card.to_s + ', '
end
}
print ']'
end
end
class Dealer < HandOfCards
def deck
@@deck
end
def initializer_deck(deck_number)
card_list = [*(1..13)]
@@deck = card_list * 4 * deck_number
end
def show_first_hand
show_hand([@hand[0], '?'])
end
end
class Player < HandOfCards
attr_accessor :bet
def win
return @bet
end
def win_bj
return (@bet * 1.5).floor
end
end
BJ = 21
HIT = 'h'
STAND = 's'
deck_number = 6
tip = 100
puts '----------'
puts 'BLACK JACK'
puts '----------'
player = Player.new()
dealer = Dealer.new()
dealer.initializer_deck(deck_number)
loop do
if tip < 1 then
puts 'Bankrupt!'
break
elsif dealer.deck.size < 100 then
dealer.initializer_deck(deck_number)
end
puts
puts 'Next Game [Enter]'
gets
puts 'Your tip: ' + tip.to_s
puts 'How much do you want to bet?'
while (input = gets.to_i) < 1 or input > tip
puts 'Please input again.'
end
player.refresh_hand
dealer.refresh_hand
player.bet = input
player.draw_a_card
dealer.draw_a_card
player.draw_a_card
dealer.draw_a_card
print 'dealer hand: '
if dealer.evaluate_hand.max == BJ then
puts dealer.show_hand
puts 'Dealer Black Jack!'
else
puts dealer.show_first_hand
end
print 'player hand: '
puts player.show_hand
puts 'Player Black Jack!' if player.evaluate_hand.max == BJ
if dealer.evaluate_hand.max == BJ && player.evaluate_hand.max == BJ then
puts 'Draw'
next
elsif dealer.evaluate_hand.max == BJ then
puts 'Dealer wins...'
tip -= player.bet
next
elsif player.evaluate_hand.max == BJ then
puts 'Player wins!'
tip += player.win_bj
next
end
puts 'player total is ' + player.evaluate_hand.to_s
puts
loop do
puts 'Hit[h] or Stand[s]?'
input = gets.chomp
if input == HIT then
player.draw_a_card
print 'dealer hand: '
puts dealer.show_first_hand
print 'player hand: '
puts player.show_hand
puts 'player total is ' + player.evaluate_hand.to_s
puts
if player.evaluate_hand == 'Bust' then
puts 'Player Bust!'
puts 'Dealer wins...'
tip -= player.bet
break
end
break if player.evaluate_hand.max == BJ
next
end
break if input == STAND
puts 'Please select again.'
end
next if player.evaluate_hand == 'Bust'
loop do
print 'dealer hand: '
puts dealer.show_hand
if dealer.evaluate_hand == 'Bust' then
puts 'Dealer Bust!'
puts 'Player wins!'
tip += player.win
break
elsif dealer.evaluate_hand.max > 16 then
puts 'dealer total is ' + dealer.evaluate_hand.max.to_s
if player.evaluate_hand.max > dealer.evaluate_hand.max then
puts 'Player wins!'
tip += player.win
elsif player.evaluate_hand.max < dealer.evaluate_hand.max then
puts 'Dealer wins...'
tip -= player.bet
else
puts 'Draw'
end
break
end
dealer.draw_a_card
end
end