PageMac
Home
   dot Projects
   dot Pics and Images
   dot WREK
D++
   dot About / FAQ
   dot Download
   dot Code Library
   dot Documentation
   dot VB6 Runtime Files
About

  Source Code - dpp/source/blackjack.dpp

Download link (right click, save as)



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:
newvar cards[51], numat;
newvar money=100;

function main()
{
    newvar card1, card2, tcard1, playerhand, bet;
    newvar dealer1, dealer2, tcard2, dealerhand;
    newvar value="h", looping=1;
    newvar tempcard1;
    
    title "BlackJack";
    
    makecards();
    screenput "Welcome to BlackJack!  "; screen; screen;
    shuffle();
    
    do while looping;        
        if money = 0 then
            screenput "You're out of money.  Go get a job, deadbeat.";
            pause 1;
            clear;
            screenput "Resetting.  You now have $100."; screen;
            money = 100;
            pause 1;
        endif
        screenput "You have $" & money & ".  Bet how much?  ";
        screenin bet;
        if bet > money || bet < 0 || ! isnumeric(bet) then
            screenput " <-- Invalid bet"; screen;
            continue;
        endif
        money = money - bet;
        card1 = taketopcard();
        card2 = taketopcard();
        dealer1 = taketopcard();
        dealer2 = taketopcard();
        screen; screen;
        
        screenput "You drew ";
        playerhand = handtotal(card1, card2, true); 
        screenput ", a total of " & playerhand & "."; screen;
        screenput "The dealer drew "; printCard(dealer1);
        dealerhand = handtotal(dealer1, dealer2, false); 
        screenput " and another card."; screen;
        screen;
        
        if playerhand = 21 && dealerhand != 21 then
            screenput "BlackJack!  You win!";
            money = money + (bet * 2.5);
            continue;
        endif
        
        do while lcase(value) = "h";
            screenput "[H]it or [S]tand? ";
            screenin value; screen;
            if lcase(value) = "h" then
                tempcard1 = taketopcard();
                screenput "You drew "; printCard(tempcard1); screenput ".";  screen;
                playerhand = playerhand + cardTotal(tempcard1);
                screenput "Your total is now " & playerhand & ".";
                screen; screen;
            endif
            if playerhand > 21 then
                screenput "BUST!"; screen; screen;
                value = "s";
            endif
        loop
        
        screenput "The dealer has ";
        dealerhand = handtotal(dealer1, dealer2, true); 
        screenput ", a total of " & dealerhand & "."; screen;
        
        do while dealerhand <= 17 && playerhand < 22;
            tempcard1 = taketopcard();
            screenput "Dealer draws "; printCard(tempcard1);
            dealerhand = dealerhand + cardTotal(tempcard1);
            screenput ", a total of " & dealerhand & "."; screen;    
        loop
        screen;
        if dealerhand > 21 then
            screenput "Dealer busts!";
        else
            screenput "Dealer stands."; 
        endif
        screen; screen;
        
        if playerhand > dealerhand && playerhand < 22 || dealerhand > 21 then
            screenput "You win!";
            money = money + (bet * 2);
        elseif playerhand = dealerhand then
            screenput "Push!";
            money = money + bet;
        else
            screenput "You loose!";
        endif
         
        screen; screen; 
        pak; clear;      
        value = "h";
        shuffle();
    loop
}
    
function handTotal(card1, card2, printOut)
{
    if printOut then
        printCard(card1);
        screenput " and ";
        printCard(card2);
    endif
    
    return cardTotal(card1) + cardTotal(card2);
}

function cardTotal(card);
{
    if card > 10 then 
        card = 10; 
    endif
    return card;
}

function printCard(card);
{
    screenput "a ";
    if card = 11 then
        screenput "Jack";
    elseif card = 12 then
        screenput "Queen";
    elseif card = 13 then
        screenput "King";
    else
        screenput card;
    endif
}

function makeCards()
{
    newvar i, j;
    numat = 0;
    
    for i = 1 to 13;
        for j = 0 to 3;
            cards[numat] = i;
            numat++;
        next j;
    next i;
}

function shuffle()
{
    newvar i, c1, c2, c3;
    screenput "Now shuffling cards..."; doevents;
    for i = 0 to 200;
        c1 = rndnum(51);
        c2 = rndnum(51);
        
        c3 = cards[c1];
        cards[c1] = cards[c2];
        cards[c2] = c3;
    next i;
    numat = 0;
    screen;screen;
}

function takeTopCard();
{
    takeTopCard = cards[numat];
    numat++;
}




This code is Copyright (C) 2000-2012 PageMac Programming. Reproduction of this code must adhere to the GNU General Public License.