-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack.rb
111 lines (99 loc) · 2.78 KB
/
blackjack.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
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
puts "***********************************"
puts "Welcome to The Iron Yard Blackjack!"
puts "***********************************"
puts ""
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'deck'
puts "I'm going to be the dealer, and my name is Aaron."
puts "What's your name?"
name = gets.chomp
puts "Nice to meet you, #{name}! Good luck, you are going to need it."
puts
deck = Deck.new
deck.shuffle
dealer_hand = []
player_hand = []
2.times do
dealer_hand << deck.draw
player_hand << deck.draw
end
dealer_total = dealer_hand.inject(0) { |sum, card| sum += card.value}
player_total = player_hand.inject(0) { |sum, card| sum += card.value}
if player_total == 21
puts player_hand, dealer_hand
puts "Congratualations, #{name}! You just hit Blackjack."
exit
elsif dealer_total == 21
puts "You lost! Dealer hit Blackjack."
exit
else
puts "One of the dealer's card is facedown. The other card is a #{dealer_hand[0]}."
puts "Your cards are: "
puts player_hand
puts "Your total sum is #{player_hand.inject(0) { |sum, card| sum += card.value } }."
puts "Do you want to hit or stay? Please type 'H' for 'Hit' or any other key for stay."
hit_or_stay = gets.chomp
if hit_or_stay.upcase == "H"
player_hand << deck.draw
puts player_hand
player_total = player_hand.inject(0) { |sum, card| sum += card.value}
puts player_total
if player_total > 21
puts
puts "You lost."
exit
else
puts puts "Do you want to hit or stay?"
hit_or_stay = gets.chomp
if hit_or_stay.upcase == "H"
player_hand << deck.draw
puts player_hand
player_total = player_hand.inject(0) { |sum, card| sum += card.value}
puts player_total
if player_total > 21
puts player_hand, dealer_hand
puts "You went bust so you lost."
exit
end
else
end
end
else
puts dealer_hand
end
while dealer_total < 16 do
dealer_hand << deck.draw
if dealer_total > 21
puts player_hand, dealer_hand
puts "#{name}, you won! Dealer went bust."
else
puts
end
end
if player_total > dealer_total
puts "Your cards were:"
puts player_hand
puts "Dealer's cardz were:"
puts dealer_hand
if player_total < 22
puts "You won #{name}."
else
puts "You went bust. You lost #{name}."
exit
end
elsif dealer_total > player_total
puts "Your cards were:"
puts player_hand
puts "Dealer's cards were:"
puts dealer_hand
puts "You lost #{name}."
exit
else
puts "Your cards were:"
puts player_hand
puts "Dealer's cards were:"
puts dealer_hand
puts "Can you believe it #{name}, it's a tie? Sorry, the house wins all ties."
exit
end
end