All pastes #767309 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-11-09 17:27 UTC
rendered paste body
var deck : array 1 .. 52 of int
var hand : array 1 .. 2, 1 .. 5 of int
var deckCounter, bank, totalBet : int
var no_cards : array 1 .. 2 of int
var bet, dealerTotal : int
var playerHit, dealerHit : boolean
const player := 1
const dealer := 2
const x := maxrow div 2
const y := maxcol div 2
const mid := maxrow div 2 + 1
const plength := 20
const gap := (maxcol - 3 * plength) div 4

proc displayMsg (msg : int)
    var dummy : string

    case msg of
        label 1 :
            locate (1, y - 13)
            put "Press <enter> to continue."
            locate (x + 1, y)
            put "You can't bet more than is in your bank!"
        label 2 :
            locate (1, y - 13)
            put "Press <enter> to play again."
            locate (x + 1, y)
            put "You win!"
        label 3 :
            locate (1, y - 13)
            put "Press <enter> to play again."
            locate (x + 1, y)
            put "The dealer wins!"
        label 4 :
            locate (1, y - 13)
            put "Press <enter> to play again."
            locate (x + 1, y)
            put "Draw."
    end case
    locate (x + 1, y - 1)
    get dummy : *
    locate (x + 1, y)
    put ""
    locate (1, y - 13)
    put "" : 26
end displayMsg

proc shuffle
    var count, nbr, face, suit : int
    var found : boolean

    for i : 1 .. 52
        deck (i) := 0
    end for

    count := 0
    loop
        randint (suit, 1, 4)
        randint (face, 1, 13)
        nbr := suit * 100 + face
        found := false
        for i : 1 .. count
            if deck (i) = nbr then
                found := true
            end if
        end for
        if not found then
            count := count + 1
            deck (count) := nbr
        end if
        exit when count = 52
    end loop
    deckCounter := 0
end shuffle

function deal : int
    deckCounter := deckCounter + 1
    result deck (deckCounter)
end deal

function countHand (who : int) : int
    var face, total, aces : int

    total := 0
    aces := 0
    for i : 1 .. 5
        face := hand (who, i) mod 100
        case face of
            label 1 :
                total := total + 11
                aces := aces + 1
            label 11, 12, 13 :
                total := total + 10
            label :
                total := total + face
        end case
    end for
    if total > 21 then
        total := total - aces * 10
    end if
    result total
end countHand

proc displayPrompts

    locate (mid - 1, gap)
    put "Bank     : " ..
    locate (mid - 1, 2 * gap + plength)
    put "Player Total: " ..
    locate (mid - 1, 3 * gap + 2 * plength)
    put "Bet : " ..
    locate (mid + 1, gap)
    put "Total Bet: " ..
    locate (mid + 1, 2 * gap + plength)
    put "Dealer Total: " ..
    locate (mid + 1, 3 * gap + 2 * plength)
    put "Hit?: " ..
end displayPrompts

proc displayBank (bnk : int)
    locate (mid - 1, gap + 11)
    put bnk : 4 ..
end displayBank

proc displayTotal (who : int)
    if who = player then
        locate (mid - 1, 2 * gap + plength + 14)
    else
        locate (mid + 1, 2 * gap + plength + 14)
    end if
    put countHand (who) : 2 ..
end displayTotal

proc drawCard (who, which, what : int, up : boolean)
    var x, y, pic, picId : int
    var file : string
    const cardHeight := maxy div 4
    const cardWidth := 4 * cardHeight div 7
    const gap := (maxx - 5 * cardWidth) div 6
    if who = player then
        y := maxy - cardHeight - gap
    else
        y := gap
    end if
    x := which * gap + (which - 1) * cardWidth
    if up then
        file := "cards/" + intstr (what) + ".bmp"
    else
        file := "cards/back.jpg"
    end if
    picId := Pic.FileNew (file)
    picId := Pic.Scale (picId, cardWidth, cardHeight)

    Pic.Draw (picId, x, y, picCopy)
    if up then
        drawbox (x, y, x + cardWidth, y + cardHeight, black)
    end if
    Pic.Free (picId)
end drawCard


function getBet (bnk : int) : int
    var bet : int
    loop
        locate (mid - 1, 3 * gap + 2 * plength + 6)
        put "" : 10 ..
        locate (mid - 1, 3 * gap + 2 * plength + 6)
        get bet
        exit when bet >= 0 and bet <= bnk
        displayMsg (1)
    end loop
    result bet
end getBet

function getHit : boolean
    var hit : string (1)
    loop
        locate (mid + 1, 3 * gap + 2 * plength + 6)
        put "" : 10 ..
        locate (mid + 1, 3 * gap + 2 * plength + 6)
        get hit : *
        exit when index ("YyNn", hit) > 0
    end loop
    if index ("Yy", hit) > 0 then
        result true
    else
        result false
    end if
end getHit

proc displayTotalBet (total : int)

    locate (mid + 1, gap + 11)
    put total : 4 ..
end displayTotalBet

proc restartGame
var dummy : string
    locate (1, y - 13)
    put "Press <enter> to continue."
    locate (x + 1, y)
    put "You lose! Your bank is empty."
    locate (x + 1, y - 1)
    get dummy : *
    locate (x + 1, y)
    put ""
    locate (1, y - 13)
    put "" : 26
    bank := 100
end restartGame

proc onGameEnd (dealerScore, playerScore : int)
    if dealerScore < playerScore and dealerScore <= 21 and playerScore <= 21 then
        displayMsg (2)
        bank := totalBet * 2 + bank
    elsif dealerScore > playerScore and dealerScore <= 21 and playerScore <= 21 then
        displayMsg (3)
    elsif dealerScore = playerScore and dealerScore = 21 and playerScore = 21 then
        displayMsg (4)
    elsif dealerScore > 21 and playerScore <= 21 then
        displayMsg (2)
        bank := totalBet * 2 + bank
    elsif dealerScore <= 21 and playerScore > 21 then
        displayMsg (3)
    elsif dealerScore > 21 and playerScore > 21 then
        displayMsg (4)
    end if
    no_cards (player) := 2
    no_cards (dealer) := 2
    for i : 1 .. 5
        hand (player, i) := 0
        hand (dealer, i) := 0
    end for

    if bank = 0 then
        restartGame
    end if
end onGameEnd


%================================= main =======================================%

bank := 100

loop
    cls
    shuffle
    displayPrompts

    for i : 1 .. 5
        hand (player, i) := 0
        hand (dealer, i) := 0
    end for
    for i : 1 .. 2
        hand (player, i) := deal
        drawCard (player, i, hand (player, i), true)
        hand (dealer, i) := deal
        if i = 1 then
            drawCard (dealer, i, hand (dealer, i), false)
        else
            drawCard (dealer, i, hand (dealer, i), true)
        end if
    end for
    no_cards (player) := 2
    no_cards (dealer) := 2
    totalBet := 0
    playerHit := true
    loop
        displayBank (bank)
        displayTotal (player)
        bet := getBet (bank)
        totalBet := totalBet + bet
        displayTotalBet (totalBet)
        bank := bank - bet
        playerHit := getHit
        if playerHit then
            no_cards (player) := no_cards (player) + 1
            hand (player, no_cards (player)) := deal
            drawCard (player, no_cards (player), hand (player, no_cards (player)), true)
        else
            exit
        end if
        exit when no_cards (player) = 5 or countHand (player) >= 21
    end loop
    %dealer stuff
    drawCard (dealer, 1, hand (dealer, 1), true)
    loop
        displayTotal (dealer)
        dealerTotal := countHand (dealer)
        no_cards (dealer) := no_cards (dealer) + 1
        hand (dealer, no_cards (dealer)) := deal
        drawCard (dealer, no_cards (dealer), hand (dealer, no_cards (dealer)), true)
        exit when dealerTotal >= 16 or no_cards (dealer) >= 5
    end loop
    onGameEnd (dealerTotal, countHand (player))
end loop