View difference between Paste ID: aKs9xtQx and BTEx46bT
SHOW: | | - or go back to the newest paste.
1
-- Init
2
print("Enter your bet here:")
3
term.redirect(peripheral.wrap("top"))
4-
term.setBackgroundColor(colors.black)
4+
5
term.setTextColor(colors.red)
6
term.clear()
7
function center(str)
8
  curX,curY = term.getCursorPos()
9
  maxX,maxY = term.getSize()
10
  maxX = maxX / 2
11
  maxX = maxX - (#str/2)
12
  term.setCursorPos(maxX,curY)
13
  print(str)
14
end
15
deckCount = false
16
cardCount = false
17
analysis = {}
18
19
-- Start Game
20
paintutils.drawFilledBox(1,7,51,11,colors.lightGray)
21
term.setCursorPos(1,8)
22
term.setTextColor(colors.red)
23
center("Blackjack")
24
term.setCursorPos(1,10)
25
term.setTextColor(colors.yellow)
26
center("Deluxe")
27
sleep(3)
28
faceCount = 0
29
for i=1,19 do
30
  paintutils.drawFilledBox(1,i,51,i,colors.green)
31
  sleep(0)
32
end
33
function shuffle()
34
faceCount = 0
35
term.setBackgroundColor(colors.green)
36
term.clear()
37
term.setCursorPos(1,9)
38
term.setTextColor(colors.red)
39
center("Shuffling deck...")
40
cards = {
41
"A", "A", "A", "A",
42
"J", "J", "J", "J",
43
"K", "K", "K", "K",
44
"Q", "Q", "Q", "Q",
45
"2", "2", "2", "2",
46
"3", "3", "3", "3",
47
"4", "4", "4", "4",
48
"5", "5", "5", "5",
49
"6", "6", "6", "6",
50
"7", "7", "7", "7",
51
"8", "8", "8", "8",
52
"9", "9", "9", "9",
53
"10", "10", "10", "10",
54
}
55
deck = {}
56
for i,v in pairs(cards) do
57
  repeat
58
    pos = math.random(1,52)
59
  until deck[pos] == nil
60
  deck[pos] = v
61
  sleep(0)
62
end
63
end
64
shuffle()
65
playerHand = {}
66
dealerHand = {}
67
cash = 1000
68
function countCard(sCard)
69
  if sCard == "A" or sCard == "J" or sCard == "K" or sCard == "Q" or sCard == "10" then
70
    faceCount = faceCount - 1
71
  elseif sCard == "2" or sCard == "3" or sCard == "4" or sCard == "5" or sCard == "6" then
72
    faceCount = faceCount + 1
73
  end
74
end
75
function dealSelf(hide)
76
  dealerHand[#dealerHand+1] = deck[#deck]
77
  if not hide then
78
    countCard(deck[#deck])
79
  end
80
  deck[#deck] = nil
81
end
82
function dealPlayer()
83
  playerHand[#playerHand+1] = deck[#deck]
84
  countCard(deck[#deck])
85
  deck[#deck] = nil
86
end
87
function drawCard(card,x,y) do
88
  term.setBackgroundColor(colors.red)
89-
  term.setTextColor(colors.black)
89+
90
  if card == "flipped" then
91
    term.setBackgroundColor(colors.red)
92
    term.setTextColor(colors.green)
93
    term.setCursorPos(x,y)
94
    write("+-+")
95
    term.setCursorPos(x,y+1)
96
    write("|*|")
97
    term.setCursorPos(x,y+2)
98
    write("+-+")
99
  elseif card == "10" then
100
    term.setCursorPos(x,y)
101
    write("10 ")
102
    term.setCursorPos(x,y+1)
103
    write("   ")
104
    term.setCursorPos(x,y+2)
105
    write(" 10")
106
  else
107
    term.setCursorPos(x,y)
108
    write(card.."  ")
109
    term.setCursorPos(x,y+1)
110
    write("   ")
111
    term.setCursorPos(x,y+2)
112
    write("  "..card)
113
  end
114
  end
115
end
116
function getHandValue(tHand)
117
  nValue = 0
118
  nAces = 0
119
  for i,v in pairs(tHand) do
120
    if v == "A" then
121
      nAces = nAces + 1
122
      nValue = nValue + 11
123
    elseif v == "2" then
124
      nValue = nValue + 2
125
    elseif v == "3" then
126
      nValue = nValue + 3
127
    elseif v == "4" then
128
      nValue = nValue + 4
129
    elseif v == "5" then
130
      nValue = nValue + 5
131
    elseif v == "6" then
132
      nValue = nValue + 6
133
    elseif v == "7" then
134
      nValue = nValue + 7
135
    elseif v == "8" then
136
      nValue = nValue + 8
137
    elseif v == "9" then
138
      nValue = nValue + 9
139
    elseif v == "10" then
140
      nValue = nValue + 10
141
    elseif v == "J" then
142
      nValue = nValue + 10
143
    elseif v == "K" then
144
      nValue = nValue + 10
145
    elseif v == "Q" then
146
      nValue = nValue + 10
147
    end
148
  end
149
  repeat
150
    if nValue > 21 and nAces > 0 then
151
      nAces = nAces - 1
152
      nValue = nValue - 10
153
    end
154
  until nAces <= 0 or nValue <= 21
155
  if nAces > 0 then
156
    soft = true
157
  else
158
    soft = false
159
  end
160
  return nValue,soft
161
end
162
dealerShowing = false
163
buttons = false
164
function redraw()
165-
  term.setBackgroundColor(colors.black)
165+
166
  term.clear()
167
  term.setTextColor(colors.red)
168
  spacing = 25-(#dealerHand*2)
169
  for i,v in pairs(dealerHand) do
170
    if dealerShowing or i == 1 then
171
      drawCard(v,spacing,3)
172
    else
173
      drawCard("flipped",spacing,3)
174
    end
175
    spacing = spacing + 4
176
  end
177
  spacing = 25-(#playerHand*2)
178
  for i,v in pairs(playerHand) do
179
    if i == #playerHand and doubled and not dealerShowing then
180
      drawCard("flipped",spacing,13)
181
    else
182
      drawCard(v,spacing,13)
183
    end
184
    spacing = spacing + 4
185
  end
186
  if buttons then
187
    term.setCursorPos(10,17)
188
    term.setBackgroundColor(colors.lightGray)
189
    term.setTextColor(colors.gray)
190
    write(" Stand ")
191
    term.setCursorPos(18,17)
192
    if doubled then
193
      term.setBackgroundColor(colors.gray)
194
      term.setTextColor(colors.lightGray)
195
    else
196
      term.setBackgroundColor(colors.lightGray)
197
      term.setTextColor(colors.gray)
198
    end
199
    write(" Hit ")
200
    term.setCursorPos(24,17)
201
    if cash >= (bet*2) and not doubled then
202
      term.setBackgroundColor(colors.lightGray)
203
      term.setTextColor(colors.gray)
204
    else
205
      term.setBackgroundColor(colors.gray)
206
      term.setTextColor(colors.lightGray)
207
    end
208
    write(" Double ")
209
    term.setCursorPos(33,17)
210
    if #playerHand == 2 and playerHand[1] == playerHand[2] then
211
      term.setBackgroundColor(colors.lightGray)
212
      term.setTextColor(colors.gray)
213
    else   
214
      term.setBackgroundColor(colors.gray)
215
      term.setTextColor(colors.lightGray)
216
    end
217
    write(" Split ")
218
  end
219
  term.setCursorPos(2,16)
220
  term.setBackgroundColor(colors.green)
221
  term.setTextColor(colors.red)
222
  write("Cash:")
223
  term.setCursorPos(2,17)
224
  term.setTextColor(colors.lime)
225
  write("$"..tostring(cash))
226
  term.setCursorPos(45,16)
227
  term.setTextColor(colors.red)
228
  if deckCount then
229
    write("Deck:")
230
    term.setCursorPos(46,17)
231
    term.setTextColor(colors.lightGray)
232
    write(tostring(#deck))
233
  end
234
  if cardCount then
235
    write("Count:")
236
    term.setCursorPos(46,17)
237-
    term.setTextColor(colors.white)
237+
    term.setTextColor(colors.red)
238
    write(tostring(faceCount))
239
  end
240
  if #playerHand > 0 then
241
    term.setCursorPos(1,11)
242
    term.setBackgroundColor(colors.green)
243
    term.setTextColor(colors.gray)
244
    if dealerShowing or not doubled then
245
      center(tostring(getHandValue(playerHand)))
246
    else
247
      center("?")
248
    end
249
    term.setCursorPos(1,7)
250
    if dealerShowing then
251
      center(tostring(getHandValue(dealerHand)))
252
    else
253
      center("?")
254
    end
255
  end
256
end
257
function msg(str)
258
  paintutils.drawFilledBox(1,8,51,10,colors.gray)
259
  term.setCursorPos(1,9)
260-
  term.setTextColor(colors.white)
260+
261
  center(str)
262
end
263
function winAnim()
264
  dollars = {}
265
  for i=1,51 do
266
    dollars[i] = math.random(-5,0)
267
  end
268
  term.setTextColor(colors.yellow)
269-
  term.setBackgroundColor(colors.black)
269+
270
  for i=1,40 do
271
    for x,v in pairs(dollars) do
272
      if v >= 1 and v <= 51 then
273
        term.setCursorPos(x,v)
274
        write(" ")
275
      end
276
      dollars[x] = dollars[x]+1
277
      if (v+1) >= 1 and (v+1) <= 51 then
278
        term.setCursorPos(x,v+1)
279
        write("$")
280
      end
281
    end
282
    sleep(0.1)
283
  end
284
  for i=1,19 do
285
    paintutils.drawLine(1,i,51,i,colors.green)
286
    sleep(0)
287
  end
288
end
289
function log(str)
290
  f = fs.open("/log","a")
291
  f.writeLine(str)
292
  f.close()
293
end
294
function playHand()
295
  if cash == 0 then
296
    optA = true
297
    optB = true
298
    optC = true
299
    while true do
300-
    term.setBackgroundColor(colors.black)
300+
301
    term.clear()
302-
    paintutils.drawImage(paintutils.loadImage("/blackjack/dollar"),1,1)
302+
    paintutils.drawImage(paintutils.loadImage("/Blackjack/dollar"),1,1)
303-
    term.setBackgroundColor(colors.black)
303+
304-
    term.setTextColor(colors.white)
304+
    term.setTextColor(colors.red)
305
    term.setCursorPos(1,11)
306
    center("You're ruined!")
307
    term.setCursorPos(1,18)
308
    term.setBackgroundColor(colors.lightGray)
309
    term.setTextColor(colors.gray)
310
    if optA then
311
      term.setBackgroundColor(colors.lightGray)
312
      term.setTextColor(colors.gray)
313
    else
314
      term.setBackgroundColor(colors.gray)
315
      term.setTextColor(colors.lightGray)
316
    end
317
    center(" Move in with mom ")
318
    term.setCursorPos(1,16)
319
    if optB then
320
      term.setBackgroundColor(colors.lightGray)
321
      term.setTextColor(colors.gray)
322
    else
323
      term.setBackgroundColor(colors.gray)
324
      term.setTextColor(colors.lightGray)
325
    end
326
    center(" Resort to crime  ")
327
    term.setCursorPos(1,14)
328
    if optC then
329
      term.setBackgroundColor(colors.lightGray)
330
      term.setTextColor(colors.gray)
331
    else
332
      term.setBackgroundColor(colors.gray)
333
      term.setTextColor(colors.lightGray)
334
    end
335
    center("  Pray for money  ")
336
    if true then
337
      e,c,x,y = os.pullEvent('mouse_click')
338
      if y == 18 and optA then
339
        optA = false
340
        if true then
341
          msg("She bails you out!")
342
          sleep(3)
343
          cash = 500
344
          break
345
        end
346
      elseif y == 16 and optB then
347
        optB = false
348
        if math.random(1,4) == 4 then
349
          msg("You start a drug operation!")
350
          sleep(3)
351
          cash = 15000
352
          break
353
        else
354
          msg("You'll get caught!")
355
          sleep(3)
356
        end
357
      elseif y == 14 and optC then
358
        optC = false
359
        if math.random(1,2) == 2 then
360
          msg("Your wish is granted!")
361
          sleep(3)
362
          cash = 1000
363
          break
364
        else
365
          msg("You live in sin!")
366
          sleep(3)
367
        end
368
      end
369
    end
370
    end
371
  end
372
  bet = 0
373
  buttons = false
374-
  blackJack = false
374+
  Blackjack = false
375
  playerBust = false
376
  dealerBust = false
377
  dealerShowing = false
378
  playerHand = {}
379
  dealerHand = {}
380
  if #deck < 12 then
381
    shuffle()
382
  end
383
  doubled = false
384
  redraw()
385
  if not splitCard then
386
    repeat
387
      paintutils.drawFilledBox(10,17,39,17,colors.green)
388
      term.setCursorPos(18,17)
389-
      term.setTextColor(colors.white)
389+
      term.setTextColor(colors.red)
390
      write("Bet: ")
391
      bet = tonumber(read())
392
      if not bet then
393
        bet = cash+1
394
      end
395
    until bet <= cash
396
  end
397
  if not splitCard then
398
    dealPlayer()
399
    redraw()
400
    sleep(0.5)
401
    dealSelf()
402
    redraw()
403
    sleep(0.5)
404
    dealPlayer()
405
    redraw()
406
    sleep(0.5)
407
    dealSelf(true)
408
    redraw()
409
  else
410
    playerHand[#playerHand+1] = splitCard
411
    bet = splitBet
412
    splitCard = nil
413
    splitBet = nil
414
    dealSelf()
415
    redraw()
416
    sleep(0.5)
417
    dealPlayer()
418
    redraw()
419
    sleep(0.5)
420
    dealSelf(true)
421
    redraw()
422
    sleep(0.5)
423
    msg("Playing Split Hand")
424
    sleep(3)
425
  end
426
  continue = true
427
  if getHandValue(playerHand) == 21 then
428
    continue = false
429-
    blackJack = true
429+
    Blackjack = true
430
  end
431
  while continue do
432
    buttons = true
433
    redraw()
434
    if getHandValue(playerHand) > 21 then
435
      playerBust = true
436
      continue = false
437
      break
438
    end
439
    e,c,x,y = os.pullEvent("monitor_touch")
440
    if y == 17 then
441
      if x >= 10 and x <= 16 then
442
        -- Stand
443
        break
444
      end
445
      if x >= 18 and x <= 22 then
446
        -- Hit
447
        if not doubled then
448
          dealPlayer()
449
        end
450
      end
451
      if x >= 24 and x <= 31 then
452
        -- Double
453
        if cash >= (bet*2) and not doubled then
454
          doubled = true
455
          dealPlayer()
456
          bet = bet*2
457
        end
458
      end
459
      if x >= 33 and x <= 39 then
460
        -- Split
461
        if playerHand[1] == playerHand[2] then
462
          if #playerHand == 2 then
463
            splitBet = bet
464
            splitCard = playerHand[2]
465
            playerHand[2] = nil
466
            dealPlayer()
467
          end
468
        end
469
      end
470
    end
471
  end
472
  buttons = false
473
  dealerShowing = true
474
  countCard(dealerHand[2])
475
  if continue then
476
    for i=1,8 do
477
      redraw()
478
      value,soft = getHandValue(dealerHand)
479
      if value < 17 then
480
        dealSelf()
481
        sleep(0.5)
482
      elseif value == 17 and soft then
483
        dealSelf()
484
        sleep(0.5)
485
      end
486
    end
487
  end
488
  redraw()
489-
  if blackJack then
489+
  if Blackjack then
490
    cash = cash + (bet*1.5)
491
    msg("Blackjack!")
492-
    analysis[#analysis+1] = "blackjack"
492+
    analysis[#analysis+1] = "Blackjack"
493
    sleep(2)
494
    winAnim()
495
    return
496
  end
497
  if playerBust then
498
    cash = cash - bet
499
    msg("You Bust!")
500
    analysis[#analysis+1] = "bust"
501
    sleep(3)
502
    return
503
  end
504
  if getHandValue(dealerHand) > 21 then
505
    cash = cash + bet
506
    msg("Dealer Busts!")
507
    analysis[#analysis+1] = "dealerbust"
508
    sleep(2)
509
    winAnim()
510
    return
511
  end
512
  if getHandValue(dealerHand) > getHandValue(playerHand) then
513
    cash = cash - bet
514
    msg("You Lose!")
515
    analysis[#analysis+1] = "lose"
516
    sleep(3)
517
    return
518
  end
519
  if getHandValue(dealerHand) == getHandValue(playerHand) then
520
    msg("You Push!")
521
    analysis[#analysis+1] = "push"
522
    sleep(3)
523
    return
524
  end
525
  if getHandValue(playerHand) > getHandValue(dealerHand) then
526
    cash = cash + bet
527
    msg("You Win!")
528
    analysis[#analysis+1] = "win"
529
    sleep(2)
530
    winAnim()
531
    return
532
  end
533
end
534
535
while true do
536
  playHand()
537
  if #deck < 12 then
538
  term.setBackgroundColor(colors.gray)
539
  term.clear()
540-
  term.setTextColor(colors.white)
540+
541
  cBlackjack = 0
542
  cDealerbust = 0
543
  cBust = 0
544
  cLose = 0
545
  cPush = 0
546
  cWin = 0
547
  for i,v in pairs(analysis) do
548-
    if v == "blackjack" then
548+
    if v == "Blackjack" then
549
      cBlackjack = cBlackjack + 1
550
    elseif v == "dealerbust" then
551
      cDealerbust = cDealerbust + 1
552
    elseif v == "bust" then
553
      cBust = cBust + 1
554
    elseif v == "lose" then
555
      cLose = cLose + 1
556
    elseif v == "push" then
557
      cPush = cPush + 1
558
    elseif v == "win" then
559
      cWin = cWin + 1
560
    end
561
  end
562
  gameCount = cBlackjack + cDealerbust + cBust + cLose + cPush + cWin
563
  startPoint = 2
564
  colorz = {
565
    [6] = colors.purple,
566
    [4] = colors.lime,
567
    [1] = colors.red,
568
    [2] = colors.orange,
569
    [5] = colors.green,
570
    [3] = colors.yellow,
571
  }
572
  output = {
573
    [1] = cBust,
574
    [2] = cLose,
575
    [3] = cPush,
576
    [4] = cWin,
577
    [5] = cDealerbust,
578
    [6] = cBlackjack,
579
  }
580
  for i,v in pairs(output) do
581
    paintutils.drawLine(startPoint,4,(startPoint+((v/gameCount)*49)-1),4,colorz[i])
582
    startPoint = startPoint + ((v/gameCount)*49)
583
  end
584
  paintutils.drawPixel(1,4,colors.gray)
585
  paintutils.drawPixel(51,4,colors.gray)
586
  analysis = {}
587
  term.setBackgroundColor(colors.gray)
588-
  term.setTextColor(colors.white)
588+
589
  term.setCursorPos(1,2)
590
  center("Game Analysis")
591
  term.setCursorPos(4,6)
592
  term.setBackgroundColor(colors.gray)
593
  term.setTextColor(colors.red)
594
  write("Bust  ")
595
  term.setTextColor(colors.orange)
596
  write("Lose  ")
597
  term.setTextColor(colors.yellow)
598
  write("Push  ")
599
  term.setTextColor(colors.lime)
600
  write("Win  ")
601
  term.setTextColor(colors.green)
602
  write("Dealer Bust  ")
603
  term.setTextColor(colors.purple)
604
  write("Blackjack")
605
  term.setTextColor(colors.gray)
606
  term.setBackgroundColor(colors.lightGray)
607
  term.setCursorPos(42,18)
608
  write(" Close ")
609
  repeat
610
    e,c,x,y = os.pullEvent("mouse_click")
611
  until x >= 42 and x <= 50 and y == 18
612
  end
613
end