Cribbage - the card game
from Nick Herrick
───────────────────────────────────────────────────────────────────────────────
Cribbage is an excellent card game, comprising equal skill and luck, for two
players. If you are not familiar with it, "potted" rules are included in the
program.
This version complies with the rules for six card cribbage published in the
'Penguin book of card games'. The skill is in selecting the 'box' and in the
play. I have played cribbage for about thirty years and I can beat the 'expert'
computer level about three times out of five. The computer does NOT cheat at
all in this game.
HOW THE PROGRAM WORKS
Each subroutine has an introductory REM line to explain its purpose. The
program consists of arrays, loops, GOSUBS and GOTO'S: inevitably it hops around
a lot as there are many different possible orders of play that have to be
catered for.
Arrays of less than 10 variables have not been specifically DIMensioned. As
the program is 36k long variables have on occasion been reused with different
meanings at different points of play.
Any variable beginning with c refers to computer's hand, with y to your (ie -
player's) hand and with x to temporary cards and loops used for both hands.
Lines 60-230: Main program: Line REM's are self explanatory.
Lines 240-890: mostly user defined characters for Ace, King, Queen,
Jack and 1/2 pips to give realistic cards.
Lines 900-1130: Shuffles numbers 1 to 52 into random order in array: to
ensure no number occurs more than once the loop
variable z is inserted in a random position [subscript
(d)]. The subroutine then defines each card
individually. This wholes subroutine takes 5-7 seconds,
which I feel is a reasonable time to shuffle a pack of
cards!
Lines 1140-1190: Self explanatory.
Lines 1200-1330: Takes alternate cards from top of the pack (dealing to
you first if it is "computers deal" or vice versa) and
directs to subroutines for printing these cards to
screen.
Lines 1340-1420: Prints numbers under each card (used in play to enter
your choice of card).
Lines 1430-1560: You choose 2 cards for your 'box or 'crib', these are
inserted into memory and the remainder of the hand
memorised into arrays (yhand(a) is a number, 1 to 13
corresponding to Ace to King yhand$(a) is the card eg
3♦ and y$(a) is the suit of the card.
Line 1570: computer only uses next section on play level 1
(beginner).
Line 1580-1700: Random selection of computer box. Variables start with
c, otherwise have same meaning.
Lines 1710-1880: (not use for level 1 play). All court cards are valued
at 10 in temporary array, which is then bubble sorted.
For level 3 (expert) Lines 1900-2260 are missed.
Lines 1890-2250: Fives weighting to various values and combinations of
cards: the more useful to the hand, the higher the
value of s for that card.
Lines 2260-2450: Selects cards with lowest s value and puts them in the
box, the remainder of the hand is arrayed as
previously.
Lines 2460-2570: Deals the selected cards to each player. Subroutines
print cards to screen.
Lines 2580-2750: You or computer 'cut' the remaining pack, take top card
off the lower portion and turn it up for use in later
scoring. Scores 2 points to dealer if Jack turned up.
Lines 2760-3300: Level 3 computer box selection, as 1890-2250. This is
based on my idea of the values of various hands. You
may wish to alter these values after trying the game (I
welcome suggestions). Logically this section should lie
between 2450-2460, but I wrote this section late in
developing the program and there was not enough gap
here in the original line numbers!
Lines 3310-3990: This is very complex and directs who plays next
according to who played last and whether a player can
go or not. GOSUBS from it to play subroutines, or 'turn
cards over' on reaching count of 31 or nearest. There
are more than 30 possible different orders of play so
this complexity is inevitable.
Lines 400-4060: v defines number of cards played, v1 to v4 previous
cards played and are used as subscripts for variable
for last 4 cards played plus current one added at end
of subroutine. (re uses xhnd and xhn arrays).
Line 4070: k is total score so far ("count"): if this <=21 you
must be able to play.
Lines 4080-4140: Self explanatory.
Lines 4150-4280: Prints and deletes cards, works out your score (if any)
from this play and adds the pip count to k. Variables
v1 etc. are used to calculate runs in subroutine 5320.
Lines 4290-4350: As 400-4060.
Lines 4360-4400: Decides if computer can play.
Lines 4410-4500: Decides if computer has high score combination and then
plays it: jumps to 4670 if card played.
Lines 4520-4590: As above for low score combination.
Line 4600: If one card only remains plays this.
Lines 4610-4710: Random selection of card for level 1, then handles
score and count.
Lines 4720-4920: Selects 'best' non scoring card to play in similar
manner to 'box' selection. Again you may wish to load
this differently.
Lines 4390-5100: "Intermediate" subroutines directing to printing and
deleting of correct cards on screen.
Lines 5110-5220: Prints top and bottom 2 lines of cards: the 'picture'
on the card is printed in subroutine 7150.
Lines 5230-5290: Self explanatory.
Lines 5300-5520: This was the most difficult part of the program to get
correct: as runs can be scored in ANY order of play of
3 or more cards, it had to select these, but not if
another card was intermediate eg. if cards were played
in this order 2,5,3,4 a run only results on play of 4th
card. The subroutine bubble sorts, "gets rid of" one
card in a pair then assesses if there is a run and
validates it with a checksum.
Lines 5030-6080: Self explanatory.
Lines 6090-6150: Enables hands to be scored in correct order (if non
dealer, who scores first, gets over 121 he wins, even
if dealer has a better hand).
Lines 6160-6280: Transfers various hands and box to temporary array for
scoring. Reuses xhnd and xhn arrays, rscore, q and k.
Lines 6390-7100: Prints out individual hands and 'lift card', bubble
sorts hands and runs through loops to calculate score
for every possible combination, then delays long enough
for you to check it. It works!
Lines 7110-7820: Prints each card in realistic manner: fits into middle
of lines 5320-5540 (the card graphics were written
after the program was running).
Lines 7830-end: Self explanatory. Data all LOCATE x&y coordinates of
top left of cards in various positions.
NOTE FROM THE EDITOR - I am a Cribbage fan and have played the game for moe
than 25 years now. I went straight to the "Expert" level and played Nick's
version for well over an hour. He has covered all the important points well and
it does provide a good game. Well done Nick!
───────────────────────────────────────────────────────────────────────────────