Mastermind on the Apple-1

This describes Mastermind on the Apple-1. It starts with how to load it using the cassette tape from Apple. Next is an example of gameplay. Finally, there is a transcript of the magazine article which includes instructions on how to play and the source code.


D:\mame> mame64 apple1 mastmind <Enter>
C100R <RET>
0300.03FFR — don’t press <RET>
Play the tape from the internal UI then press <RET>
0300R <RET> to run the program.

The game starts with READY?@ so press <RET> then 01 @ is displayed. Enter a 5 digit number (digits 0-7). Results are + for correct number and correct location and – for correct number and incorrect location.


Apple 1 – Mastermind

A Number Game for the 6502
by Steve Wozniak, Apple Computers, 770 Welch Rd., Suite 154, Palo Alto CA 94304; (415) 326-4248
Dr. Dobbs Journal, Volume 1, Number 8, page 253

MASTERMIND is a number guessing game. In this version the computer generates a 5-digit, random number where the digits are limited to 0 through 7. The user attempts to guess the number in the fewest possible tries. After each guess the computer informs the user as to how many digits were correct (contained in the random number) and in the correct position by printing *+’ signs, and how many digits are correct and in the wrong position with ‘ signs. The user is not informed as to exactly which digit positions the *+’ and the ‘ signs correspond. A skilled player can usually win in 6 or 7 tries.

1. Computer prints ‘READY?’
2. User hits any key when ready (not echoed). A random number is generated by counting from the ‘READY?’ message to key depression.
3. Computer prints ’01’ (cursor left here) for move number.
4. User enters his 5-digit guess following the move number. Remember that valid digits are 0 through 7 only. Entering any invalid character cancels the line and the computer repeats step 3 for the current move. This may be used to cancel errors.
5. Immediately after entering the 5th digit of his guess, the computer will print a number of ‘+’ signs (for correct digits in correct positions) followed by a number of ‘- ‘ signs (for correct digits in incorrect positions). This ‘score’ is indicated to the right of the guess and does not indicate the specific digit positions involved. Play resumes at 3 for the next move number except for a win. Examples follow:

Scoring Examples for Random Number 12154
Move Guess  Score
01   33366  (none correct)
02   00018  (line cancelled due to invalid digit, 8)
02   00011  -- (2 digits right, in wrong positions)
03   11234  ++-- (4 digits right, 2 in correct positions, 2 in wrong positions)
04   11325  +--- (4 digits right, 1 in correct position)
05   13216  +-- (3 digits right, 1 in correct position)
06   44444  + (1 digit right in correct position)
07   55555  + (l digit right, in correct position)
08   12154  +++++ YOU WIN (win message)
READY? (for next game)

The source and object listings provided will run on APPLE-1 systems. The program loads in locations $300 through $3AE and uses the following page 0 locations for variables:
$F2 TRIES     (no. of tries – 0 to 99 BCD)
$F3 RNDL    } Binary
$F4 RNDH    } random number
$F5 RND2L     (Temp.)
$F6-$FA N     (5 digits of unpacked random no.)
$FB-$FF GUESS (User guess)

1. The LDA STROBE instruction at loc. $313 (on supplied listing) senses for a key down condition after the READY? message. This is used to generate a random number corresponding to the delay before a key is depressed. The code is written for a negative value to indicate ‘key down’ (b y = 1) and a positive value (b y = 0) to indicate no key down. This instruction is followed by a JSR CHARIN instruction whose only purpose is to clear the strobe. The character returned is not used. Thus the user must provide an address for the LDA STROBE instruction corresponding to his hardware and must insure that the CHARIN subroutine clears the strobe.
2. A CHARIN subroutine must be provided to read one ASCII character with b-y set. Do not use the CHARIN subroutine provided as it uses APPLE-1 I/O assignments. The character read must be returned in the A-Reg. The Y-Reg may be altered by CHARIN but not the X-Reg. If by is returned clear (=0) then the EOR #$B0 instruction at loc. $34E (on supplied code) must be changed to an EOR #$30.
3. A COUT subroutine must be provided which outputs one ASCII character (passed in A-Reg). If the user output device requires line feeds with carriage returns then the COUT routine must intercept the carriage return character ($8D) and output the necessary CR-LF combination. All computer generated text has by=l. No registers (A,X,Y) may be altered.
4. PRBYTE subroutine must be provided which outputs one byte (passed in A-Reg) in hexadecimal (printing 2 digits). No registers need be preserved. The following routine will do:

PRBYTE  PHA         Save for LSD
        LSR         MSD to LSD position
        JSR PRHEX*  Output MSD first
        PLA         Restore A
PRHEX   AND #$F     Mask LSD
PRHEX*  ORA #$BO    Add “0”
        CMP #$BA    {May be skipped if used for
        BCC TOCOUT  {MASTERMIND only, since
        ADC #$6     {only BCD digits supplied.
TOCOUT  JMP COUT Output ASCII and return
              TRIES  EQU
              RNDL   EQU     $F2
              RNDH   EQU     $F3
              RND2L  EQU     $F4
              N      EQU     $F5
              GUESS  EQU     $F6
              COUT   EQU     $FB
              PRBYTE EQU     $FFEF
              KBD    EQU     $FFDC
              STROBE EQU     $D011
                     ORG     $300
0300 A2 08    MSTMND LDX     #$8
0302 BD 91 03 MSGLP  LDA     MSG-1,X
0305 20 EF FF        JSR     COUT            PRINT 'READY?'
0308 CA              DEX
0309 D0 F7           BNE     MSGLP
030B 86 F2           STX     TRIES           SET TRIES TO ZERO.
030D E6 F3    RNDLP  INC     RNDL
030F D0 02           BNE     RND2            FORM 2-BYTE RANDOM NUMBER
0311 E6 F4           INC     RNDH            UNTIL KEY DOWN.
0313 AD 11 D0 RND2   LDA     STROBE
0316 10 F5           BPL     RNDLP
0318 20 A8 03        JSR     CHARIN          CLEAR STROBE.
031B 38       NXTRY  SEC
031C F8              SED
031D 8A              TXA
031E 65 F2           ADC     TRIES           ADD 1 TO TRIES IN DECIMAL MODE
0320 85 F2           STA     TRIES
0322 D8              CLD
0323 20 A3 03 NXTLIN JSR     CRLF
0326 A5 F2           LDA     TRIES           OUTPUT CRLF AND TRIES (IN BCD)
0328 20 DC FF        JSR     PRBYTE
032B A9 A0           LDA     #$A0            OUTPUT BLANK.
032D A8              TAY
032E 20 EF FF        JSR     COUT
0331 A5 F3           LDA     RNDL
0333 85 F5           STA     RND2L
0335 A5 F4           LDA     RNDH
0337 A2 05           LDX     #$5            SET ARRAY N TO 5 DIGITS OF
0339 94 F5    DIGEN  STY     N-1,X          RANDOM NUMBER.  DIGITS ARE
033B A0 03           LDY     #$3            0 THROUGH 7.
033D 4A       BITGEN LSR     A
033E 26 F5           ROL     RND2L
0340 36 F5           ROL     N-1,X
0342 88              DEY
0343 D0 F8           BNE     BITGEN
0345 CA              DEX
0346 D0 F1           BNE     DIGEN
034B 20 EF FF        JSR     COUT
034E 49 B0           EOR     #$B0           CONVERTS DIGITS TO TRUE VALUE.
0350 C9 08           CMP     #$8            IF NOT 0 TO 7 THEN REPEAT LINE
0352 B0 CF           BCS     NXTLIN         WITH SOME TRIES VALUE.
0354 95 FF           STA     GUESS+4,X      SAVE USER DIGIT.
0356 CA              DEX
0357 E0 FB           CPX     #$FB           DONE 5 DIGITS?
0359 D0 ED           BNE     RDKEY
035B A0 FB           LDY     #$FB           WIN COUNT (FOR 5 MATCHES).
035D A9 A0           LDA     #$A0           PRINT BLANK.
035F 20 EF FF PLUS1  JSR     COUT
0364 D5 FB           CMP     N+5,X          FOR THIS DIGIT POSITION?
0366 D0 0D           BNE     PLUS3          NO, TRY NEXT POSITION.
0368 94 FB           STY     N+5,X          SETS DIG OF RAND NUMBER, TO 4FB
036A A9 AB           LDA     #$AB           -FF SO NO 'MINUS MATCH.
036C 95 00           STA     GUESS+5,X      SET DIGIT OF GUESS TO $AB SO
036E C8              INY                    *  NO 'MINUS' MATCH.
036F D0 EE           BNE     PLUS1          INCR. WIN COUNTER AND LOOP.
0371 A2 11           LDX     #$11           IF WIN, OUTPUT WIN MESSAGE
0373 D0 8D           BNE     MSGLP          *  AND BEGIN NEW GAME.
0375 E8       PLUS3  INX                    * NEXT DIGIT OF 'PLUS' SCAN.
0376 D0 EA           BNE     PLUS2
0378 A0 FB           LDY     #$FB
037C 8A              TXA
037D A2 FB           LDX     #$FB
0381 D0 07           BNE     MINUS3         NO MATCH.
0383 94 FB           STY     N+5,X          SET RAND DIGIT TO $FB-$FF
0385 A9 AD           LDA     #$AD           SUBSTITUTE $AD FOR GUESS DIGIT
0387 20 EF FF        JSR     COUT
038A E8       MINUS3 INX                    * NEXT RANDOM DIGIT.
038B D0 F2           BNE     MINUS2         *    LOOP.
038D C8              INY                    * NEXT USER DIGIT.
038E D0 EA           BNE     MINUS1         *    LOOP.
0390 F0 89           BEQ     NXTRY          UPDATE TRIES FOR NEXT LINE.
0392 BF       MSG    DFB     $BF            '?'
0393 D9       MSG    DFB     $D9            'Y'
0394 C4       MSG    DFB     $C4            'D'
0395 C1       MSG    DFB     $C1            'A'
0396 C5       MSG    DFB     $C5            'E'
0397 D2       MSG    DFB     $D2            'R'
0398 8D       MSG    DFB     $8D            CR.
0399 8D       MSG    DFB     $8D            CR.
039A CE       MSG    DFB     $CE            'N'
039B C9       MSG    DFB     $C9            'I'
039C D7       MSG    DFB     $D7            'W'
039D A0       MSG    DFB     $A0            BLANK
039E D5       MSG    DFB     $D5            'U'
039F CF       MSG    DFB     $CF            'O'
03A0 D9       MSG    DFB     $D9            'Y'
03A1 A0       MSG    DFB     $A0            BLANK
03A2 AB       MSG    DFB     $AB            '+'
03A3 A9 8D    CRLF   LDA     #$8D
03A5 4C EF FF        JMP     COUT
03AB 10 FB           BPL     CHARIN
03AD AD 10 D0        LDA     KBD            READ KEY AND CLEAR STROBE.
03B0 60              RTS
BITGEN  033D     0043    0047
CHARIN  03A8     0107    0024    0050    0108
COUT    FFEF     0008    0015    0036    0051    0061    0082    0106
CRLF    03A3     0105    0031
DIGEN   0339     0041    0049
GUESS   00FB     00FB    0007    0055    0062    0067    0075
KBD     D010     0010    0109
MINUS1  037A     0075    0086
MINUS2  037F     0078    0084
MINUS3  038A     0083    0079
MSG     0392     0088    0014
MSGLP   0302     0014    0017    0071
MSTMND  0300     0013
N       00F6     0006    0041    0045    0063    0065    0078    0080
NXTLIN  0323     0031    0054
NXTRY   031B     0025    0087
PLUS1   035F     0061    0069
PLUS2   0362     0062    0073
PLUS3   0375     0072    0064
PRBYTE  FFDC     0009    0033
RDKEY   0348     0050    0058
RND2    0313     0022    0020
RND2L   00F5     0005    0038    0044
RNDH    00F4     0004    0021    0039
RNDL    00F3     0003    0019    0037
RNDLP   030D     0019    0023
STROBE  D011     0011    0022    0107
TRIES   00F2     0002    0018    0028    0029    0032

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s