blackjack

Ruby code posted by Joe Jackson
created at 24 Apr 02:11

Edit | Back
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
# Interactive command line blackjack game


def calculate_total(cards) 
  # [['3', 'H'], ['Q', 'S'], ... ]
  localarray = cards.map{|e| e[0] }  
# Go through cards array and pull out the card values and put into 
# an array called localarray

# Change pictures in cards to a number
# For each element in the local array
# if the card is an ace, give it a value of eleven
# if not an ace, test for picture. Changing pic to integer results in
# a zero.  If not zero then card is not a picture, the number becomes 
# the value
#
  total = 0
  localarray.each do |value|
    if value == "A"
      total += 11
    elsif value.to_i == 0 # J, Q, K
      total += 10
    else
      total += value.to_i
    end
  end

  #correct for Aces
  # Function to adjust a person’s hand to remain below 21
  #  because an ace can be a one or eleven.
  # How: Subtract 10 from the hand for every ace the person
  #   has until the total value is below 21
  #
  localarray.select{|e| e == "A"}.count.times do
    total -= 10 if total > 21
  end

  total
end


  


# Start Game

puts "Welcome to Blackjack!"
puts " "
game_on=0
p_bank = 500
p_bet = 0
while game_on == 0 || p_bank > 0
  puts "==========Beginning new game==================="
  puts "How much do you want to bet? enter zero to quit"
  puts "You have #{p_bank} dollars"

  p_bet=gets.chomp.to_i
  
  if p_bet == 0
    puts "You chose to bet nothing, it looks like you do not want to play, goodbye"
    game_on=1
    exit
  elsif 
    p_bet > p_bank
    puts "You do not have enough to bet that much"
    break
  else
    p_bank=p_bank-p_bet
  end

  suits = ['H', 'D', 'S', 'C']
  cards = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']

  deck = cards.product(suits)
  deck.shuffle!
  deck
  # Deal Cards

  p_cards = []
  dealercards = []



  p_cards << deck.pop
  dealercards << deck.pop
  p_cards << deck.pop
  dealercards << deck.pop

  dealertotal = calculate_total(dealercards)
  p_cardsum = calculate_total(p_cards)

  # Show Cards

  puts ""
  puts "Dealer has: #{dealercards[0]} and #{dealercards[1]}, for a total of #{dealertotal}"
  puts "The Dealer is showing #{dealercards[1]}"
  puts "You have: #{p_cards[0]} and #{p_cards[1]}, for a total of: #{p_cardsum}"
  puts ""
  puts "You bet #{p_bet} and you have #{p_bank} total dollars" 


  # Player turn
  if p_cardsum == 21
    puts "Congratulations, you hit blackjack! You win!"
    busted=1
    p_bet=p_bet+(p_bet*2)
    p_bank=p_bank+p_bet
    #exit
  end
  doubled=1
  first_time_thru=0 # 0 is yes, first time
  while p_cardsum < 21 && doubled != 0
    puts "What would you like to do? 1) hit 2) stay 0) Double"
    busted=0
    hit_or_stay = gets.chomp
    doubled=hit_or_stay.to_i

    if !['0','1', '2'].include?(hit_or_stay)
      puts "Error: you must enter 0, 1 or 2"
      next
    end


    if doubled == 0
      hit_or_stay = "1"
      if first_time_thru != 0 
        
        puts "You can only double once per hand"
   #     exit
   #     break
        elsif
          first_time_thru = 1
        puts "You have #{p_bank} in the bank"
        puts "you had bet #{p_bet}"
        if p_bet > p_bank 
          puts "Sorry, you do not have enough to double"
  #        doubled=1
           break
        else 
          p_bank=p_bank-p_bet
          p_bet=p_bet*2
          doubled=0
          puts "Your bet is now #{p_bet} with #{p_bank} in the bank"
        end
       # break
      end
    end

    if hit_or_stay == "2"
      puts "You chose to stay."
      puts "Good luck sucker"
      puts "****"
      break
    end

    #hit
    new_card = deck.pop
    puts "Dealing card to player: #{new_card}"
    p_cards << new_card
    p_cardsum = calculate_total(p_cards)
    puts "Your total is now: #{p_cardsum}"

    if p_cardsum == 21
      puts "Congratulations, you hit blackjack! You win!"
      p_bet=p_bet*2
      p_bank=p_bank+p_bet
      puts "You won #{p_bet}"
      puts "You now have #{p_bank}"
      busted=1
    #  exit
      break
    elsif p_cardsum > 21
      puts "Sorry, it looks like you busted!"
      busted=1
      puts ""
      puts "You lost #{p_bet}"
      puts "You now have #{p_bank}"
      break
#      exit
    end
  end

  # Dealer’s turn

  if dealertotal == 21 && busted == 0
    puts "Sorry, dealer hit blackjack. You lose."
    puts "You lost #{p_bet}"
  #  p_bank=p_bank-p_bet
    puts "You now have #{p_bank}"
    busted=1
  #  break
   # exit
  end

  while dealertotal < 17 && busted == 0
    #hit=
    new_card = deck.pop
    puts "Dealing new card for dealer: #{new_card}"
    dealercards << new_card
    dealertotal = calculate_total(dealercards)
    puts "Dealer total is now: #{dealertotal}"

    if dealertotal == 21
      puts "Sorry, dealer hit blackjack. You lose."
      puts "You lost #{p_bet}"
      puts "You now have #{p_bank}"
      busted=1
      break
 #     exit
    elsif dealertotal > 21
      puts "Congratulations, dealer busted! You win!"
      puts "You won #{p_bet}"
      p_bank=p_bank+p_bet+p_bet
      puts "You now have #{p_bank}"
      busted=1
      break
#      exit

    end
  end

  # Compare hands

  puts "Dealer's cards: "
  dealercards.each do |card|
    puts "=> #{card}"
  end
  puts ""

  puts "Your cards:"
  p_cards.each do |card|
    puts "=> #{card}"
  end
  puts ""

  if (dealertotal <= 21 || p_cardsum <= 21) && busted == 0
    if dealertotal > p_cardsum
      puts "Sorry, dealer wins."
    #  p_bank=p_bank-p_bet
      puts "You lost #{p_bet}"
      puts "You now have #{p_bank}"
    elsif dealertotal < p_cardsum
      puts "Congratulations, you win!"
      puts "You won #{p_bet}"
      p_bank=p_bank+p_bet+p_bet
      puts "You now have #{p_bank}"
    else
      puts "It's a tie!"
      puts "You lucky dog, you got away this time"
      p_bank=p_bank+p_bet
      puts "You still have #{p_bank}"
    end
  end
busted=0
  if p_bank < 0 
    game_on=1
  end

#  exit
end
puts "Thank you for playing"
puts "You leave us with a bank of #{p_bank}"
6.09 KB in 14 ms with coderay