-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackjack.rb
executable file
·142 lines (130 loc) · 3.88 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
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
#!/usr/bin/env ruby
# YOUR CODE HERE
require_relative "deck"
require_relative "card"
require_relative "hand"
require "pry"
class Blackjack
attr_reader :deck, :player_hand, :house_hand
def initialize
@deck = Deck.new
@player_hand = Hand.new
@house_hand = Hand.new
end
def player_hand_display
string = ""
player_hand.cards.each do |card|
string << card.value
string << card.suit
string << " "
end
string
end
def house_hand_display
string = ""
house_hand.cards.each do |card|
string << card.value
string << card.suit
string << " "
end
string
end
def end_game
print "#{player_hand_display}, totaling #{player_hand.sum_value} and the house has #{house_hand_display}, totaling #{house_hand.sum_value}"
end
def new_game
deck = @deck
player_hand = @player_hand
house_hand = @house_hand
player_hand.cards << deck.deal
house_hand.cards << deck.deal
player_hand.cards << deck.deal
house_hand.cards << deck.deal
puts "\nWelcome to Blackjack!"
puts "Player was dealt #{player_hand_display}"
puts "Your hand total is #{player_hand.sum_value}, would you like to hit or stand?"
decision = gets.chomp.downcase
until decision == "hit" || decision == "stand"
puts "Please enter either hit or stand"
decision = gets.chomp.downcase
end
if decision == "hit"
hit
elsif decision == "stand"
if player_hand.sum_value > house_hand.sum_value && house_hand.sum_value > 16
end_game
print "\n YOU WIN!\n"
elsif player_hand.sum_value < house_hand.sum_value && house_hand.sum_value > 16
end_game
print "\n HOUSE WINS!\n"
elsif player_hand.sum_value == house_hand.sum_value && house_hand.sum_value > 16
end_game
print "\n PUSH!\n"
else
stand
end
end
puts "\nWould you like to play again? (Yes/No)"
new_round = gets.chomp.downcase
until new_round == "yes" || new_round == "no"
puts "Please say yes or no"
new_round = gets.chomp.downcase
end
if new_round == "yes"
player_hand.cards = []
house_hand.cards = []
if @deck.stack.count > 4
new_game
else
puts "Sorry, end of the deck!"
end
else
puts "Come back anytime!"
end
end
def hit
player_hand.cards << deck.deal
if player_hand.sum_value > 21
print "You have #{player_hand_display}. Your total is #{player_hand.sum_value}\n YOU BUST!"
else
puts "You have #{player_hand_display}. Your hand total is now #{player_hand.sum_value}, would you like to hit or stand?"
decision = gets.chomp.downcase
if decision == "hit"
hit
else
puts "Player score: " + player_hand.sum_value.to_s
if player_hand.sum_value > house_hand.sum_value && house_hand.sum_value > 16
end_game
print "\n YOU WIN!\n"
elsif player_hand.sum_value <= house_hand.sum_value && house_hand.sum_value > 16
end_game
print "\n HOUSE WINS!\n"
else
stand
end
end
end
end
def stand
print "\nYou have #{(player_hand_display)} and the house has #{house_hand_display}, house will hit \n\n"
print "Press enter for next card flip \n"
gets.chomp
house_hand.cards << deck.deal
if player_hand.sum_value < house_hand.sum_value && house_hand.sum_value <= 21
end_game
print "\n HOUSE WINS!\n"
elsif house_hand.sum_value > 21
end_game
print "\n HOUSE BUSTS\n YOU WIN!\n"
elsif 16 < house_hand.sum_value && house_hand.sum_value <= 21 && player_hand.sum_value > house_hand.sum_value
end_game
print "\n YOU WIN!\n"
elsif 16 < house_hand.sum_value && house_hand.sum_value <= 21 && player_hand.sum_value == house_hand.sum_value
end_game
print "\n PUSH!\n"
else
stand
end
end
end
Blackjack.new.new_game