home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
100 Great Games for Palm OS 1
/
100PalmV1.iso
/
Cards
/
pocketjack
/
PJ12.csl
< prev
next >
Wrap
Text File
|
1999-03-06
|
21KB
|
920 lines
# PocketJack, Version 1.2 release.
# Copyright (C) March 1999, Frank O'Brien,
# dianfrank@worldnet.att.net.
# PocketJack name, program, manual, and source code are
# copyrighted by Frank O'Brien. Absolutely no warranty is
# given.
# This program is freeware. The program and source code
# may be distributed as long as there is no fee.
# Source code may be changed and distributed. Distributed
# changes shall be documented with name, type and date.
# Please also add a note to about box. Distribution shall
# be under these same terms (no fee).
# when compiling this file, FOPJ has been reserved as a
# unique creator id. "PocketJack" is used as desktop name.
# PJ11.cic is provided for use as pilot icon image.
#BlackJack program for the Pilot
#20Dec97, Frank O'Brien
# BJ10.csl version 1.0
# *** bugs/new features
# on windows, after draw, all labels are updated on display,
# causes some clear of down card
# 25-26Dec97
# OK- only deduct 1 ace, not all
# OK - clear hands at game start, not wide enough
# OK - pilot hit loop takes too many cards
# OK - when dealer busts, Wins not updated
# OK - doesn't deal cards down yet
# OK - pay double for bj and 5cc, triple for 6cc
# OK - feature to change bet 1-5, replace new button with
# pick bet event
# OK - after human busts, flip pilot last card
# OK - add total, Wins, and who won labels
# OK - about box
# OK - basic info on rules
# OK - when player gets 6cc, pilot for some reason still goes,
# winner is player with triple bet.
#27Dec97
# OK - improve GUI look, maybe add lines - added little side bordering,
# standardized all positions and sizes with variables
# and functions
#28dec97
# added delay to pilot play
# added delay to deal loop
# added sound to deal
# added menu options to change delay and sound defaults
# added win/lose sound, plays double if win double.
# adjusted cards to be a little shorter, little narrower,
# labels a little wider.
#29dec97
# hit/stay row lower a little, sometimes overlapped with label
#30dec97
# added char graphic history list to remove long redraw after
# toggle off msgbox frame
#31dec97
# still need redraw labels after msgbox, changed put to show
# no text box overflow auto scroll, so sRules must stay small,
# took out math symbols, added english.
#1jan98
# start of BJ11.csl version 1.1
# started to clean up logic flow
#2jan98
# continue with timer loops, determined user controls won't
# get "focus" if inside while or timer loops. therefore
# kept control similar to before. game starts with slBet,
# then control to btHit/btStay, then repeats.
# per manual, added "call" statement to all function calls
# gave things shorter names, added more comments
# added tie push option
#12jan98
# start of PJ11.csl
# changed program name to PocketJack
# added slight changes to rules and about msg
#6Mar99, fob
# for all those card counters out there, the deck is now a single
# deck. after its been dealt, there's a reshuffle, (no last
# card option stuff).
# added better help and about info.
# source code headings
# 0 compiler variables
# 1 variables
# 1.1 game
# 1.2 reserved
# 1.3 options
# 1.5 screen coordinate constants for card graphics
# 2 objects
# 2.1 main frame objects
# 2.2 help frame objects
# 2a library files
# 3 functions
# 3.1 returns 1 dimen index, given 2 dimen index
# 3.2 visual object origin functions
# 3.3 help functions
# 3.4 option stuff
# 3.5 game data 2 dimen access functions
# 3.6 draw card functions
# 3.7 game play functions
# 3.8 player interface functions
# 3.9 system startup/shutdown
# 0 compiler variables
variables;
# compiler setting, i distribute in single folder,
# but on my machine, some cpk files are in library folder
# default is true()
numeric bSingleFolder=false();
end;
# 1 variables
variables;
#
# 1.1 game
#
# 0-11 player*cards arrays are actually accessed with
# player 0-1, and card 0-5 indexes,
# see nfIndex() for 2 dimen access
numeric nMax=6;
numeric nFace[2*nMax];
numeric nSuit[2*nMax];
numeric nValue[2*nMax];
# 0-1 player arrays
numeric nLast[2]=-1; # last dealt card index, 0-5
numeric nTotal[2]=0; # total in hand
# betting status
numeric nWins=0;
numeric nBet=1;
# selector contents
string sBet[5]="1","2","5","10","20";
# "state control" with hide/show BetSelector, Hit, Stay
# buttons
# other state control variables
numeric nDealState; # 0-4 for deal timer loop
numeric bUp; # flag=f=pilot 2nd card down, =t=up
#
# 1.3 options
#
numeric bSound; # boolean flag
string sSound[2]=" Sound"," Silent";
numeric nDelay; # ms
string sSpeed[3]="Fast","Normal","Slow";
numeric nSpeed[3]=0,200,500;
numeric bPush; # boolean flag
string sPush[2]=" Tie=0"," Tie=-1";
# 1.5 screen coordinate constants for card graphics.
#
# screen is grid of "slots", 0-2 columns, 0-10 rows.
# the grid is positioned on screen at L, T.
# larger frame things "Ob1", like cards borders, are
# positioned within a slot at BW1, BH1.
# smaller internal things "Ob2", like labels, are postioned
# at BW1+BW2, BH1+BH2.
# the size of larger things Ob1 are SlotW-2*BW1,
# SlotH-2*BH1.
# the size of smaller things Ob2 are SlotW-2*(BW1+BW2),
# SlotH-2*(BH1+BH2).
# the size of the card border is SlotW-2*BW1,
# 3*SlotH-2*BH1.
numeric nPx=1000/160;
numeric nL=5*nPx;
numeric nT=16*nPx;
numeric nChW=10*nPx;
numeric nChH=11*nPx; #9
numeric nBW1=2*nL;
numeric nBH1=nPx;
numeric nBW2=nPx;
numeric nBH2=0;#nPx;
numeric nOb2W=3*nChW;
numeric nOb2H=nChH;
numeric nOb1W=2*nBW2+nOb2W;
numeric nOb1H=2*nBH2+nOb2H;
numeric nSlotW=2*nBW1+nOb1W;
numeric nSlotH=2*nBH1+nOb1H;
numeric nCardW=nOb1W;
numeric nCardH=3*nSlotH-2*nBH1;
end;
# 2 objects
# constants are used for pixel sizing all visual objects,
# postioning of objects is done during program startup,
# using defined origin functions
# 2.1 main frame objects
frame frMain;
display "PocketJack";
end;
button btHit, frMain;
display "Hit";
pixel_size nOb1W,nOb1H;
hidden;
end;
button btStay, frMain;
display "Stay";
pixel_size nOb1W,nOb1H;
hidden;
end;
label lbFace[12], frMain;
font "stdfont","",0;
pixel_size nOb2W,nOb2H;
end;
label lbTotal[2], frMain;
font "stdfont","",0;
pixel_size nOb2W,nOb2H;
end;
label lbWins, frMain;
font "stdfont","",0;
pixel_size nOb2W,nOb2H;
end;
label lbWhoseHand[2], frMain;
pixel_size 1*nOb2W,1*nOb2H;
end;
label lbWinsLabel, frMain;
pixel_size nOb2W,nOb2H;
display "Wins";
end;
text lbStatus, frMain;
font "stdfont","",0;
pixel_size nOb1W,2*nSlotH-2*nBH1;
no_input;
display "";
end;
selector slBet, frMain;
pixel_size nOb2W, 5*nSlotH-2*(nBH1+nBH2);
list sBet;
end;
label lbBetLabel, frMain;
pixel_size nOb2W,nOb2H;
display "Bet";
end;
label lbBet, frMain;
font "stdfont","",0;
pixel_size nOb2W,nOb2H;
display sBet[0];
hidden;
end;
menu_top mtOptions, frMain;
display "Options";
end;
menu_item miOption[5], mtOptions
;end;
# 2a library files
compile_if bSingleFolder;
# default machines
include "msgbox_fo.cpk";
include "deckofcards_fo.cpk";
compile_else;
# my machine
include "..\library\msgbox_fo.cpk";
include "..\library\deckofcards_fo.cpk";
compile_end_if;
# 3 functions
# 3.1 returns 1 dimen index, given 2 dimen index
# p=player=0-1, i=index=0-5, returns 1 dimen index 0-11
function nfIndex(numeric p, numeric i) as numeric;
nfIndex=nMax*p+i;
end;
# 3.2 visual object origin functions
# constants are defined for all sizeW, sizeH, and
# borderW, borderH.
function nfSlotX(numeric c, numeric r) as numeric;
nfSlotX=nL+c*nSlotW;
end;
function nfSlotY(numeric c, numeric r) as numeric;
nfSlotY=nT+r*nSlotH;
end;
function nfOb1X(numeric c, numeric r) as numeric;
nfOb1X=nfSlotX(c,r)+nBW1;
end;
function nfOb1Y(numeric c, numeric r) as numeric;
nfOb1Y=nfSlotY(c,r)+nBH1;
end;
function nfOb2X(numeric c, numeric r) as numeric;
nfOb2X=nfOb1X(c,r)+nBW2;
end;
function nfOb2Y(numeric c, numeric r) as numeric;
nfOb2Y=nfOb1Y(c,r)+nBH2;
end;
# 3.3 help message box functions
#define help constants
#
function InitHelp;
mb_sTitle="PocketJack";
mb_sVersion="1.2";
mb_sDate="6Mar99";
mb_sAbstract="PalmPilot version of the standard ";
mb_sAbstract=mb_sAbstract+"BlackJack card game. ";
mb_sDocName="pj12doc.htm";
mb_sHelpBody="Object: Get closer to 21 than Pilot without "+
"going over."+char(10)+char(10)+"Rules: Cards are worth their ";
mb_sHelpBody=mb_sHelpBody+"face value, except J, Q, K are worth 10, ";
mb_sHelpBody=mb_sHelpBody+"and A is worth 11 or 1. ";
mb_sHelpBody=mb_sHelpBody+"21 in 2 cards wins ";
mb_sHelpBody=mb_sHelpBody+"double. 5 cards not over 21 wins ";
mb_sHelpBody=mb_sHelpBody+"double. 6 cards not over 21 wins ";
mb_sHelpBody=mb_sHelpBody+"triple. ";
mb_sHelpBody=mb_sHelpBody+"Pilot hits on 16 or ";
mb_sHelpBody=mb_sHelpBody+"less."+char(10)+char(10);
mb_sHelpBody=mb_sHelpBody+"To Play: 1) select bet, 2) ";
mb_sHelpBody=mb_sHelpBody+"tap Hit or Stay, 3) repeat.";
mb_sWebSite="http://home.att.net/~dianfrank/pilot_apps.htm";
call mb_AppendAbout(dc_sCredits);
end;
# pilot evidently redraws objects then graphics, since
# graphics cover card face label objects, must
# redraw card face labels
#
function mb_Redraw;
variables;
numeric p;
numeric i;
numeric n;
end;
p=0;
while p<2;
i=0;
while i<=nLast[p];
n=nfIndex(p,i);
if p<>1 or i<>1 or bUp;
show lbFace[n];
end_if;
i=i+1;
end_while;
p=p+1;
end_while;
end;
# 3.4 option stuff
function CardSound;
if bSound;
play "131,50"; # play lowest C, 1/20 s
end_if;
end;
function WinSound(numeric w);
if bSound and w<>0;
play "494,100"; # play middle B, 1/10 s
play "392,100"; # play middle G, 1/10 s
play "523,200"; # play high C, 2/10 s
call WinSound(w-1);
end_if;
end;
function LoseSound(numeric w);
if bSound and w<>0;
play "294,100"; # play middle D, 1/10 s
play "392,100"; # play middle G, 1/10 s
play "262,200"; # play middle C, 2/10 s
call LoseSound(w-1);
end_if;
end;
function TieSound;
if bSound;
play "392,100"; # play middle G, 1/10 s
play "392,100"; # play middle G, 1/10 s
play "392,100"; # play middle G, 1/10 s
end_if;
end;
function SetPush(numeric p);
bPush=p;
put miOption[4],sPush[p];
end;
function SetSound(numeric s);
bSound=s;
put miOption[3],sSound[s];
end;
function SetSpeed(numeric s);
variables;
numeric i;
end;
nDelay=nSpeed[s];
i=0;
while i<3;
if i=s;
# dot for selected option
put miOption[i],char(149)+sSpeed[i];
else;
put miOption[i],char(32)+sSpeed[i];
end_if;
i=i+1;
end_while;
end;
function miOption;
if invokersub=4;
# push toggle
call SetPush(not bPush);
else;
if invokersub=3;
# sound toggle
call SetSound(not bSound);
else;
# speed choice
call SetSpeed(invokersub);
end_if;
end_if;
end;
# 3.5 game data 2 dimen access functions
# f=face num=0-12, s=suit=0-3, returns string of card label
#
function sfFace(numeric f, numeric s) as string;
variables;
string ss;
end;
# do suit string for each platform, windows font doesn't have suit char
if platform = "windows";
if s=0;
ss="d";
end_if;
if s=1;
ss="c";
end_if;
if s=2;
ss="h";
end_if;
if s=3;
ss="s";
end_if;
else;
# pilot platform
ss=Char(141+s);
end_if;
# do face string
f=f+1; # 1-13
if f = 1; # ace
sfFace=Char(65)+ss;
end_if;
if f = 10; # 10
sfFace=Char(49)+Char(48)+ss;
end_if;
if f = 11; # jack
sfFace=Char(74)+ss;
end_if;
if f = 12; # queen
sfFace=Char(81)+ss;
end_if;
if f = 13; # king
sfFace=Char(75)+ss;
end_if;
if f > 1 and f < 10; # number 2-9
sfFace=Char(48+f)+ss;
end_if;
end;
# f=face num=0-12, returns bj value of card
#
function nfValue(numeric f) as numeric;
f=f+1; # 1-13
if f = 1; # ace
nfValue=11; # dealer makes low ace, as needed
end_if;
if f = 10; # 10
nfValue=10;
end_if;
if f = 11; # jack
nfValue=10;
end_if;
if f = 12; # queen
nfValue=10;
end_if;
if f = 13; # king
nfValue=10;
end_if;
if f > 1 and f < 10; # number 2-9
nfValue=f;
end_if;
end;
# 3.6 draw card functions
# p=player=0-1, generate a card as if a full deck
# is shuffled for each dealt card
# never call if already have max cards, will overflow arrays
#
function GenerateCard(numeric p);
variables;
numeric i;
numeric n;
numeric c;
end;
# update nLast index
i=nLast[p]+1;
nLast[p]=i;
n=nfIndex(p,i);
# store card data
c=dc_nfNextCard;
nFace[n]=dc_nfFace(c)-2; # 0-12
nSuit[n]=dc_nfSuit(c); # 0-3
nValue[n]=nfValue(nFace[n]); # 2-11
end;
# p=player=0-1, i=card index=0-nLast[p],
# draw card face, update total, display total,
# i may be modified.
#
function DrawFace(numeric p, numeric i);
variables;
numeric n;
end;
n=nfIndex(p,i);
# draw card face
set frMain, pen, nfOb1X(p,i), nfOb1Y(p,i);
draw frMain, rectangle, nCardW, nCardH;
put lbFace[n], sfFace(nFace[n],nSuit[n]);
# update total
nTotal[p]=nTotal[p]+nValue[n];
# adjust for any high ace, high ace stored as
# 11 in nValue[], low ace as 1
i=0;
while nTotal[p]>21 and i<=nLast[p];
n=nfIndex(p,i);
if nValue[n]=11;
# high ace found, deduct 10
nValue[n]=1;
nTotal[p]=nTotal[p]-10;
end_if;
i=i+1;
end_while;
# display total
if nTotal[p]>21;
put lbTotal[p], "Bust";
else;
put lbTotal[p], nTotal[p];
end_if;
# provides optional sound
call CardSound;
end;
# p=player=0-1, i=card index=0-nLast[p],
# display black card back, no data update
#
function DrawBack(numeric p, numeric i);
# draw card down, don't update card total
set frMain, pen, nfOb1X(p,i), nfOb1Y(p,i);
fill frMain, rectangle, nCardW, nCardH;
# provides optional sound
call CardSound;
end;
# p=player=0-1, deals card up, updates all data arrays and
# display for card and total, adjusts for ace
# never call if already have max cards, will overflow arrays
#
function NewCardU(numeric p);
variables;
numeric i;
end;
# generate a card
call GenerateCard(p);
i=nLast[p];
# draw card face
call DrawFace(p,i);
end;
# p=player=0-1, deals card up, updates card data arrays and
# displays black card back
# never call if already have max cards, will overflow arrays
#
function NewCardD(numeric p);
variables;
numeric i;
numeric n;
end;
# generate a card
call GenerateCard(p);
i=nLast[p];
call DrawBack(p,i);
end;
# p=player=0-1, flips last card up, updates total data array and
# displays card and total, adjusts for ace
#
function LastCardU(numeric p);
variables;
numeric i;
end;
# calculate last card pointers
i=nLast[p];
# draw card face
call DrawFace(p,i);
# set pilot 2nd card up flag
if p=1;
bUp=true();
end_if;
end;
# 3.7 game play functions
# hide/show user controls
#
function SetState(numeric s);
if s=0;
# processing state, user inactive
hide btHit;
hide btStay;
hide slBet;
show lbBet;
else;
if s=1;
# get bet state
hide btHit;
hide btStay;
hide lbBet;
show slBet;
else;
# state=2, get human move state
hide slBet;
show lbBet;
show btHit;
show btStay;
end_if;
end_if;
end;
# p=0=human won, p=1=pilot won, w=weight=1-3
#
function UpdateWins(numeric p, numeric w);
variables;
string s;
end;
if p=0;
# player wins
nWins=nWins+w*nBet;
s="Win"+string(w*nBet," ##")+"!";
call WinSound(w);
else;
if p=1;
# pilot wins
nWins=nWins-w*nBet;
s="Lose"+string(w*nBet," ##");
call LoseSound(w);
else;
# tie
s="Tie";
call TieSound;
end_if;
end_if;
put lbWins, nWins;
put lbStatus, s;
end;
# stay, determine winner, already known player
# total<=21 and not bj, not 5cc, not 6cc
#
function PilotStays;
if nTotal[1]>21;
# pilot busted
call UpdateWins(0,1);
else;
if nTotal[0]>nTotal[1];
# human wins
call UpdateWins(0,1);
else;
if bPush and nTotal[0]=nTotal[1];
# tie
call UpdateWins(2,0);
else;
# pilot wins
call UpdateWins(1,1);
end_if;
end_if;
end_if;
# new game state
call SetState(1);
end;
# pilot plays loop
#
function PilotPlays;
if nTotal[1]<17 and nLast[1]<5;
call NewCardU(1);
timer PilotPlays, nDelay;
else;
# loop exit
call PilotStays;
end_if;
end;
# recursive timer loop, deal 1 card up to each player,
# 1 card down, flip human's last card up
# uses nDealState to guide control
#
function Deal;
if nDealState=0; # p=0 and i=0;
call NewCardU(0);
nDealState=1;
timer Deal, nDelay;
else;
if nDealState=1; # p=1 and i=0;
call NewCardU(1);
nDealState=2;
timer Deal, nDelay;
else;
if nDealState=2; # p=0 and i=1;
call NewCardD(0);
nDealState=3;
timer Deal, nDelay;
else;
if nDealState=3; # p=1 and i=1
call NewCardD(1);
# set pilot 2nd card up flag
bUp=false();
nDealState=4;
timer Deal, nDelay;
else;
# nDealState=4, exit loop
call LastCardU(0);
# determine if human has bj, otherwise human plays
if nTotal[0]=21;
# human wins double, show pilot's card, update Wins
call LastCardU(1);
call UpdateWins(0,2);
call SetState(1); # new game
else;
# human's turn
call SetState(2);
end_if;
end_if;
end_if;
end_if;
end_if;
end;
# init new game
#
function InitGame;
variables;
numeric i;
numeric p;
end;
# clear graphic history list
clear frMain, rectangle, 0,0;
# clear card display
i=0;
while i<12;
put lbFace[i],"";
i=i+1;
end_while;
set frMain, pen, nfSlotX(0,0), nfSlotY(0,0);
clear frMain, rectangle, 2*nSlotW, 8*nSlotH;
# init last card and total arrays, and total display
put lbStatus,"";
p=0;
while p<2;
nLast[p]=-1;
nTotal[p]=0;
put lbTotal[p], 0;
p=p+1;
end_while;
end;
# 3.8 player interface functions
# set global bet variable, starts new game
#
function slBet;
variables;
numeric i;
end;
get slBet, i;
put lbBet, sBet[i];
call SetState(0);
nBet=value(sBet[i]);
call InitGame;
nDealState=0;
timer Deal, nDelay;
end;
# give human new card, stay in state if not auto win/loss
#
function btHit;
call NewCardU(0);
# check if busted
if nTotal[0]>21;
# show pilot's card, update Wins
call LastCardU(1);
call UpdateWins(1,1);
call SetState(1); # new game
else;
# not busted, check if 6 card charlie
if nLast[0]=5;
# 6cc auto win, show pilot's card, update Wins
call LastCardU(1);
call UpdateWins(0,3);
call SetState(1); # new game
end_if;
end_if;
end;
# passes to pilot state if not auto win
#
function btStay;
# not busted, check if 5 card charlie
if nLast[0]=4;
# 5cc, show pilot's card, update Wins
call LastCardU(1);
call UpdateWins(0,2);
call SetState(1); # new game
else;
call SetState(0); # pilot turn
# flip card
call LastCardU(1);
timer PilotPlays, nDelay;
end_if;
end;
# 3.9 system startup/shutdown
# start up game stuff
#
function startup;
variables;
numeric i;
numeric j;
numeric n;
end;
# init help
call InitHelp;
# init deck
call dc_InitDeck;
# init menu option items
call SetPush(1);
call SetSound(1);
call SetSpeed(1);
# columns=player 0-1, rows=card slot 0-5,
# total labels rows 8-9
# draw lines around play table
set frMain, pen, nfSlotX(2,0), nfSlotY(2,0);
draw frMain, line, 0, 8*nSlotH;
set frMain, pen, nfSlotX(0,8), nfSlotY(0,8);
draw frMain, line, 2*nSlotW, 0;
# move card and player labels into place
i=0;
while i<2;
j=0;
while j<nMax;
# move card labels into place
n=nfIndex(i,j);
move lbFace[n], nfOb2X(i,j), nfOb2Y(i,j);
j=j+1;
end_while;
# move total labels into place
move lbWhoseHand[i], nfOb2X(i,8), nfOb2Y(i,8);
move lbTotal[i], nfOb2X(i,9), nfOb2Y(i,9);
i=i+1;
end_while;
put lbWhoseHand[0],"You";
put lbWhoseHand[1],"Pilot";
# move other visual objects into place
move lbBetLabel, nfOb2X(2,0), nfOb2Y(2,0);
move lbBet, nfOb2X(2,1), nfOb2Y(2,1);
move slBet, nfOb1X(2,1), nfOb1Y(2,1);
move lbStatus, nfOb1X(2,6), nfOb1Y(2,6);
move lbWinsLabel, nfOb2X(2,8), nfOb2Y(2,8);
# BH1=0, so used Ob2 origin position for these OB1's
move lbWins, nfOb2X(2,9), nfOb2Y(2,9);
move btHit, nfOb1X(0,10), nfOb1Y(0,10);
move btStay, nfOb1X(1,10), nfOb1Y(1,10);
# display intial wins
nWins=0;
put lbWins,0;
# force redraw in case movement caused any problems
show frMain;
# init new game
call SetState(1);
end;