04-15-2026, 07:24 AM
==================
BLACKJACK
- ASCII Edition
==================
BLACKJACK
- ASCII Edition
==================
Code:
' ==================
' BLACKJACK
' - ASCII Edition
' ==================
randomize time()
visible values=[], deckRank=[], deckSuit=[], deckIdx = 0
visible pRank=[], pSuit=[], pCount=0 'player
visible cRank=[], cSuit=[], cCount=0 'computer
visible buf = fill(" ",6,100) 'buffer
values = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
TitleScreen()
' --------------------
' MAIN LOOP
' --------------------
do
system("cls")
pCount = 0;cCount = 0
InitDeck()
ShuffleDeck()
' Deal initial 2 cards each, 1 card is hidden
Deal("p"); Deal("c")
Deal("p"); Deal("c")
PlayerTurn()
' Computer plays only if player hasn't busted
if CalcScore("p") <= 21 then
ComputerTurn()
system("cls")
endif
ShowHands(0)
DetermineWinner()
pln chr(7)'beep sound
wln "Play again? [1/0]: "
wln "(1) Yes"
wln "(0) No"
ans=rln()
if ans <> 1 then break
loop
' ----------------------
' FUNCTIONS
' ----------------------
function InitDeck()
idx = 0
for s = 3 to 6 'suits ASCII code
for v = 0 to 12 'values
deckRank[idx] = v
deckSuit[idx] = s
idx = idx + 1
next
next
endfunc
function ShuffleDeck()
for i = 51 to 1 step -1
j = rnd(0, i)
tmpR = deckRank[i]
tmpS = deckSuit[i]
deckRank[i] = deckRank[j]
deckSuit[i] = deckSuit[j]
deckRank[j] = tmpR
deckSuit[j] = tmpS
next
deckIdx = 0
endfunc
function Deal(who)
if who = "p" then 'player
pRank[pCount] = deckRank[deckIdx]
pSuit[pCount] = deckSuit[deckIdx]
pCount = pCount + 1
else 'computer
cRank[cCount] = deckRank[deckIdx]
cSuit[cCount] = deckSuit[deckIdx]
cCount = cCount + 1
endif
deckIdx = deckIdx + 1
endfunc
function CalcScore(who)
score = 0
aces = 0
cnt = 0
if who = "p" then
cnt = pCount
else
cnt = cCount
endif
for i = 0 to cnt - 1
r = 0
if who = "p" then
r = pRank[i]
else
r = cRank[i]
endif
if r = 0 then
aces = aces + 1
score = score + 11
elseif r >= 10 then
score = score + 10
else
score = score + (r + 1)
endif
next
' Adjust Aces from 11 to 1 if busting
while score > 21 and aces > 0
score = score - 10
aces = aces - 1
wend
return score
endfunc
function DrawCardToBuf(v, suit, offset)
w = 5
h = 3
' Top border
buf[0][offset] = chr(218)
for i = 1 to w
buf[0][offset+i] = chr(196)
next
buf[0][offset+w+1] = chr(191)
' Value row
buf[1][offset] = chr(179)
for i = 1 to w
buf[1][offset+i] = chr(32)
next
if v = "10" then
buf[1][offset+1] = "1"
buf[1][offset+2] = "0"
else
buf[1][offset+1] = v
endif
buf[1][offset+w+1] = chr(179)
' Middle row
buf[2][offset] = chr(179)
for i = 1 to w
buf[2][offset+i] = chr(32)
next
buf[2][offset+w+1] = chr(179)
' Suit row
buf[h][offset] = chr(179)
for i = 1 to w
buf[h][offset+i] = chr(32)
next
if v=chr(32) then
buf[h][offset+w] = chr(32)
else
buf[h][offset+w] = chr(suit)
endif
buf[h][offset+w+1] = chr(179)
' Bottom border
buf[h+1][offset] = chr(192)
for i = 1 to w
buf[h+1][offset+i] = chr(196)
next
buf[h+1][offset+w+1] = chr(217)
endfunc
function ShowHands(hideComp)
' Clear buffer
for r = 0 to 4
for c = 0 to 70
buf[r][c] = " "
next
next
' Computer
for i = 0 to cCount - 1
if i = 0 and hideComp = 1 then
DrawCardToBuf(chr(32), 3, i * 10) 'hidden with chr(32)
else
DrawCardToBuf(values[cRank[i]], cSuit[i], i * 10)
endif
next
pln "COMPUTER"
for r = 0 to 4
for c = 0 to 70
write buf[r][c]
next
pln
next
' Clear buffer again for you
for r = 0 to 4
for c = 0 to 70
buf[r][c] = " "
next
next
' You
for i = 0 to pCount - 1
DrawCardToBuf(values[pRank[i]], pSuit[i], i * 10)
next
pln;pln
pln "YOU"
for r = 0 to 4
for c = 0 to 70
write buf[r][c]
next
pln
next
endfunc
function PlayerTurn()
do
ShowHands(1)
pScore = CalcScore("p")
if pScore >= 21 then
system("cls")
break
endif
pln
pln "Instruction"
pln "(1) Hit "
pln "(2) Stand "
write "Choose number [1/2] : "; choice = rln()
select choice
case "1";Deal("p")
case "2";system("cls");break
endsel
system("cls")
loop
endfunc
function ComputerTurn()
do
ShowHands(0)
cScore = CalcScore("c")
if cScore < 17 then
Deal("c")
else
break
endif
loop
endfunc
function DetermineWinner()
pScore = CalcScore("p")
cScore = CalcScore("c")
pln;pln
if pScore > 21 then
pln "RESULT: YOU LOSE! (Bust)"
elseif cScore > 21 then
pln "RESULT: YOU WIN! (Computer busts)"
elseif pScore > cScore then
pln "RESULT: YOU WIN!"
elseif cScore > pScore then
pln "RESULT: COMPUTER WINS!"
else
pln "RESULT: PUSH! (Tie)"
endif
endfunc
function TitleScreen()
pln; pln
pln " _ _ _ _ _ "
pln "| | | | | | (_) | | "
pln "| |__ | | __ _ ___| | ___ __ _ ___| | __"
pln "| '_ \| |/ _` |/ __| |/ / |/ _` |/ __| |/ /"
pln "| |_) | | (_| | (__| <| | (_| | (__| < "
pln "|_.__/|_|\__,_|\___|_|\_\ |\__,_|\___|_|\_\"
pln " _/ | "
pln " |__/ "
pln
pln "Aim to get a hand total closer to 21"
pln "without exceeding 21"
pln "Player receive 2 face up cards,"
pln "while computer shows 1 card up and 1 down"
pln "-Hit = take more cards"
pln "-Stand = keep current hand"
pln chr(7) 'beep sound
system("pause")
endfunc
