Classic Computer Magazine Archive ANTIC VOL. 3, NO. 12 / APRIL 1985

bonus game

CRAZY EIGHTS!

by PRINCETON CHAN
How your computer plays cards

Take on your Atari in a fast-paced computer version of the well-known card game, Crazy Eights.  And read this article to find out how the BASIC program makes "intelligent" card-playing decisions.  All Atari computers of any memory size will RUN Crazy Eights.

Type in Listing 1, checking it with TYPO II, and SAVE a copy before you RUN the program.
  On the screen display, the numbers after the words DECK and COMPUTER refer to how many cards remain in the deck and in the computer's hand. Begin play by selecting an option from the main menu.
  When you type in the card you're playing, you only need to enter the first two letters (no numbers are allowed). For example, you can type KI instead of KING-or EI instead of EIGHT (but don't use [8] here).

CRAZY EIGHTS RULES
In case you don't know how to play Crazy Eights, the object is to be the first player who gets rid of all your cards.
  Each player is dealt five cards. To get rid of a card you must put it on the discard pile-and your discard must match the pile's top card in either Rank (ace, seven, king, etc.) or Type (spade; diamond, heart, club).
  If you don't have a match to discard, you must keep drawing more cards from the deck. The program will let you hold as many as 18 cards in your hand.
  In this version of the game, you can only pass your turn to the other player if you are holding 18 cards in your hand or the deck is all gone.
  One major thing-the eights are special cards in this game. You or the computer can put an eight onto the discard pile anytime and name whatever card type (suit) you now wish to be on top.
  I give you fair warning! Your Atari is very quick and skillful at playing this game. Here's how the program does it:

Crazy Eights

PROGRAM ANALYSIS
The computer's strategy is contained in lines 730 to 830. First the computer checks to see whether it has a card to put down. If it does, it may try to search for another before using the first card it found. If the computer has an eight, it decides which type of card to use-hearts, diamonds, spades, or clubs.
  In the event that there are no cards to put down, the computer will draw from the deck until there is, or else pass. This is all the computer s strategy consists of. Now let us look at lines 730 to 830 in detail.
  Line 730 does the job of clearing the bottom of the screen, pausing, and displaying the message which tells that it is the computer's turn.
  Line 740 uses a loop that checks to see whether the rank: HAND2(L1) and type: TYPE2(L1) of the computer's card matches that of the deck. It also checks to see if the computer has an eight. The variable L1 holds the location of the chosen card in the arrays HAND2 and TYPE2. When the computer neither has a matching card nor an eight the program jumps to line 800.
  The unchecked cards are tested in line 745. The loop begins at L1, the location of the first usable card in the arrays. It ends with 18, the maximum number of cards anyone can have. If there is no matching card, the computer jumps to line 750.
  However, if the computer finds another matching card on line 745, it makes a random decision as to whether it should use the first or second card it found. There is a 50/50 chance. If the random number is a 1, the variable L1 is equal to the second choice.
  Line 750 jumps the program to line 780 when the computer uses an eight. Lines 760 to 770 change the computer's variables and redraw the top card. The number of cards the computer has is subtracted: COUNT2 = COUNT2-1.
  Line 780 determines which type of card will be picked when the computer puts down an eight. PILE1 = INT(RND(0) *4) +1 determines which type of card. A one would choose a heart, two a diamond, three a club, and four a spade. The rest of the line checks to see if the computer has the type of card picked. It will also skip the card if its rank is an eight because that card will no longer be part of the computer's hand.
  In line 790, the array TYPE2(L1) which holds the location of the eight card, is changed according to the type of card the computer picked. Remember that with eights, you can pick any type of card you want.
  Line 800 checks to see if there is a tie by checking whether DECK< = 0 and the opponent's cards. The loop checks the player's cards by comparing the types and ranks of each card to the top card and checking for eights. If the opponent has no matching cards, it is an automatic tie. Don't forget that the computer got to this line when it had no matching cards back at line 740. At the time of a tie, the computer goes to line 1530 which ends the game.
  At line 810, when the computer holds the maximum of 18 cards and does not have a match, or DECK< = 0 (no more cards to draw), the computer must pass. A message on the screen tells this.
  Lines 820 to 830 are where the computer locates the first empty location in the array HAND2(L) by using a loop: FOR L=1 TO 18:IFHAND2(L) <>0 THEN NEXT L. The part of the array is blank when there is a zero. After the computer finds an empty space, it puts the top card's rank and type into HAND 2(L) and TYPE 2(L). The computer's number of cards are added (COUNT2 = COUNT2 + 1), and the number of cards in the deck subtracted (DECK = DECK-1)
  This whole process cycles again the next time the computer puts down a card. The strategy in this program is actually simple and could have been made more complex. As you can see, your Atari is just using its number-crunching power to match programmed values quickly and accurately.

Princeton Chan is a freshman at Lowell High School in the Richmond district of San Francisco.

Crazy Eights Take-Apart
Line 60   Dimensions arrays
70-80     Initializes display list interrupt
90-110    Initializes P/M Graphics
120-180   Redefines character set
190-200   Title page
210-320   Initializes cards and starts game
330-360   Main menu
370-410   Player draws card
420-710   Player puts down card
720       Player passes
730-830   Computer's turn
850-1410  Card drawing and positioning routines
1420      Clears bottom of screen
1430-1450 Pauses
1460      Waits for RETURN to be pressed
1470-1510 Used to check for input
1530-1590 End of game

List of Variables
CARD -   Rank of all cards of deck
CARD1 -  Type of all cards of deck
HAND1 -  Rank of player 1's cards
HAND2 -  Rank of computers cards
TYPE1 -  Type of player 1's cards
TYPE2 -  Type of computers cards
CHOICE$ -Holds input from user
CHAR$ -  Holds machine language routine
DL -     Used to find display list
L -      Dummy variable
D -      Dummy variable
PMBASE - Used to find highest memory for P/M Graphics
CHBASE - Used to find highest memory for new character set
L1 -     Dummy variable
COUNT1 - Number of player is cards
COUNT2 - Number of computers cards
COUNT -  Used in initializing
DECK -   Number of cards in  deck
A -      Dummy variable VALUE -Used for card drawing routine
VALUE1 - Used for card drawing routine
TOP -    Rank of top card
TOP1 -   Type of top card
X -      Position of card
Y -      Position of card
CHOICE - User input
PILE -   Rank of input card
PILE1 -  Type of input card
NMB -    Used in card drawing routine
NMB1 -   Used in card drawing routine
Step -   Used in card drawing routine
COL -    Used in card drawing routine

Listing 1....CRAZY8.BAS Download