Mastermind

# Mastermind for the HP-41

Overview

1°) 6-digit numbers

a)  Program#1
b)  Program#2 ( faster )

2°) From 1- to 10-digit numbers

a)  Program#1
b)  Program#2 ( faster )
c)  M-Code Routine
d)  Program#3

-A number is chosen by the HP-41 and you have to find it by several guesses
-The calculator tells you how many digits of your guess are in a right position and how many are in a wrong position
-The digits range from 1 to 9

0 is not allowed in the following versions

1°)  6-digit numbers

a)  Program#1

Data Registers:           •  R00 = pseudo-random numbers                                     ( This register is to be initialized before executing "MMD" )

R01 thru R06 = +/- the digits of the hidden number          R09 = number of your guesses
R07 to R09 = counters                                                    R11 thru R16 = the digits of your guess or 0
Flag:  F29
Subroutines: /

-Lines 02 thru 16 computes the 6 digits of the mystery number
-If, for instance, you want to use digits from 1 to 7 only, replace line 10 by  7
-The append character is denoted  >

 01 LBL "MMD"  02 6  03 STO 09  04 LBL 00  05 SIGN  06 RCL 00            07 R-D  08 FRC  09 STO 00  10 9  11 *  12 +  13 INT  14 STO IND 09  15 DSE 09  16 GTO 00  17 X<>Y  18 FIX 0 19 CF 29  20 LBL 01  21 " "  22 ARCL X  23 16.01  24 STO 07  25 STO 08            26 0  27 STO 10  28 6  29 R^  30 AVIEW  31 ISG 09  32 LBL 02  33 ENTER  34 INT  35 10  36 ST/ Z 37 MOD  38 STO IND 07  39 RCL IND Z  40 ABS  41 STO IND T  42 X=Y?  43 DSE 10  44 CLX  45 R^  46 DSE X  47 R^  48 DSE 07  49 GTO 02  50 RCL 10            51 ABS  52 6  53 X=Y?  54 GTO 05 55 >" "  56 ARCL Y  57 SIGN  58 LBL 03  59 RCL 08            60 STO 07  61 LBL 04  62 RCL IND 07  63 RCL IND L  64 X#Y?  65 GTO 04  66 CHS  67 ST+ IND 07  68 STO IND L  69 ISG 10  70 LBL 04  71 DSE 07 72 GTO 04  73 DSE L  74 GTO 03  75 >"-"  76 ARCL 10  77 PROMPT  78 GTO 01  79 LBL 05  80 >"//"  81 9  82 RCL 09            83 X<=Y?  84 >"0"  85 ARCL 09  86 FIX 4  87 AVIEW  88 END

( 142 bytes / SIZE 017 )

 STACK INPUT OUTPUT X Nj Nb of guesses

Example:     1  STO 00

111111   XEQ "MMD"  >>>>  "111111"     "111111 1-0"                there is one "1"
122222           R/S                    "122222"      "122222 1-0"               the "1" is in right position and there is no "2"
133333           R/S                    "133333"      "133333 3-0"               the "1" & two "3"s are in right position
133444           R/S                    "133444"      "133444 1-2"               the "1" is in right position but the two "3"s are  in a wrong location. There is no "4"
155335"          R/S                    "155335"      "155335 2-1"               the "1" and one "3" are well placed. There is no "5"
166363           R/S                    "166363"      "166363 4-0"               the "1" and the two "3"s are well placed and there is one "6"
167373           R/S                    "167373"      "167373 3-1"               the "1" and the two "3"s are well placed, the "6" is in a wrong location and there is no "7"
186383           R/S                    "186383"      "186383 4-0"               the "1" "6" "3" "3" are in right position. There is no "8"
196393           R/S                    "196393"      "196393//09"                the hidden number was 196393

Notes:

-The above strategy is not the best one !
-You have to wait  for 13 or 14 seconds ( approximately )
-The hidden number is computed after your first guess but independently - of course !

b)  Program#2

-Instead of only comparing all the digits, the following version also calculates the number of digits in each number.
-"MMD" becomes faster and just a little longer.
-However, more data registers are used.

Data Registers:           •  R00 = pseudo-random numbers                                     ( R00 is to be initialized before executing "MMD" )

R01 to R06 = the digits of the hidden number          R07 = number of your guesses       R08 = number of correct digits
R09 to R17 = number of 1 , ..... , number of 9 in the hidden number
R18 to R26 = ............................................... in your guess
Flag:  F29
Subroutines: /

-If you don't have an HP41CX, replace line 03 by

SIGN      STO IND L
CLX        ISG L
LBL 05    GTO 05

 01 LBL "MMD"  02 9.026  03 CLRGX  04 CLX  05 6  06 STO 07  07 LBL 00  08 SIGN  09 RCL 00  10 R-D  11 FRC  12 STO 00            13 9  14 *  15 +  16 INT  17 STO IND 07  18 8 19 +  20 SIGN  21 ST+ IND L  22 DSE 07  23 GTO 00  24 X<>Y  25 FIX 0  26 CF 29  27 LBL 01  28 " "  29 ARCL X  30 SIGN  31 ST+ 07  32 CLX  33 STO 08            34 6  35 LASTX  36 AVIEW 37 LBL 02  38 ENTER  39 INT  40 10  41 ST/ Z  42 MOD  43 RCL IND Z  44 X=Y?  45 DSE 08            46 CLX  47 CLX  48 17  49 +  50 SIGN  51 ST+ IND L  52 RDN  53 DSE Y  54 GTO 02 55 RCL 08            56 ABS  57 6  58 X=Y?  59 GTO 04  60 >" "  61 26.017  62 ARCL Z  63 17  64 LBL 03  65 ENTER  66 CLX  67 X<> IND Z  68 RCL IND Y  69 XY  71 RDN  72 ST+ 08 73 SIGN  74 -  75 DSE Y  76 GTO 03  77 >"-"  78 ARCL 08  79 PROMPT  80 GTO 01  81 LBL 04  82 >"//"  83 9  84 RCL 07            85 X<=Y?  86 >"0"  87 ARCL 07  88 FIX 4  89 AVIEW  90 END

( 146 bytes / SIZE 027 )

 STACK INPUT OUTPUT X Nj Nb of guesses

Note:

-Same usage but the execution time becomes about 7 or 8 seconds instead of 14 seconds.

2°)  From 1 to 10 digits

a)  Program#1

-With this program, your first guess determines the number of digits of the hidden number ( from 1 to 10 ).

Data Registers:           •  R00 = r = random seed                                             ( Registers R00 is to be initialized before executing "MMD" )

R01 to Rnn = +/- the n digits of the hidden number     R15 = number of guesses
R16 to R15+n: temp
Flag:  F29
Subroutines: /

 01 LBL "MMD"  02 ENTER  03 LOG  04 INT  05 1  06 +  07 STO 11  08 STO 15  09 LBL 00  10 SIGN  11 RCL 00            12 R-D  13 FRC  14 STO 00  15 9  16 *  17 +  18 INT  19 STO IND 15  20 DSE 15 21 GTO 00  22 X<>Y  23 FIX 0  24 CF 29  25 LBL 01  26 " "  27 ARCL X  28 0  29 STO 12  30 15  31 .1  32 %  33 +  34 RCL 11            35 +  36 STO 13  37 STO 14  38 LASTX  39 R^  40 AVIEW 41 ISG 15            42 LBL 02  43 ENTER  44 INT  45 10  46 ST/ Z  47 MOD  48 STO IND 13  49 RCL IND Z  50 ABS  51 STO IND T  52 X=Y?  53 DSE 12  54 CLX  55 R^  56 DSE X  57 R^  58 DSE 13  59 GTO 02  60 RCL 12 61 ABS  62 RCL 11  63 X=Y?  64 GTO 05  65 >" "  66 ARCL Y  67 SIGN  68 LBL 03  69 RCL 14            70 STO 13  71 LBL 04  72 RCL IND 13  73 RCL IND L  74 X#Y?  75 GTO 04  76 CHS  77 ST+ IND 13  78 STO IND L  79 ISG 12 80 LBL 04  81 DSE 13  82 GTO 04  83 DSE L  84 GTO 03  85 >"-"  86 ARCL 12  87 PROMPT  88 GTO 01  89 LBL 05  90 >"//"  91 9  92 RCL 15            93 X<=Y?  94 >"0"  95 ARCL 15  96 FIX 4  97 AVIEW  98 END

( 151 bytes / SIZE 016+n )

 STACK INPUT OUTPUT X Nj Nb of guesses

Example:     1  STO 00  and suppose you want to play with 7-digit numbers

1111111   XEQ "MMD"  >>>>      "1111111  1-0"
1222222           R/S                        "1222222  0-1"
3133333           R/S                        "3133333  3-0"
3134444           R/S                        "3134444  1-3"
4153355           R/S                        "4153355  3-1"
4163636           R/S                        "4163636  2-3"
4176373           R/S                        "4176373  5-0"
4186383           R/S                        "4186383  5-0"
4196393           R/S                        "4196393 / / 09"

Notes:

-Your first guess must be different from   999999999    if you play with 9-digit numbers
and smaller than   9999999989  if you play with 10-digit numbers
-Otherwise, the number of digits would be miscalculated !

-With 10-digit numbers, execution time is about 30 seconds !

b)  Program#2

-This variant employs the same method as the program listed in §1b)

Data Registers:           •  R00 = pseudo-random numbers                                                ( R00 is to be initialized before executing "MMD" )

R01 to R10 = the digits of the hidden number          R11 = number of your guesses       R12 = number of correct digits

R14 to R22 = number of 1 , ..... , number of 9 in the hidden number       R13 = number of digits of the hidden number
R23 to R31 = ............................................... in your guess
Flag:  F29
Subroutines: /

-If you don't have an HP41CX, replace line 11 by

SIGN      STO IND L
CLX        ISG L
LBL 05    GTO 05

 01 LBL "MMD"  02 ENTER  03 LOG  04 INT  05 1  06 +  07 STO 11  08 STO 13            09 CLX  10 14.031  11 CLRGX  12 LBL 00  13 SIGN  14 RCL 00  15 R-D  16 FRC  17 STO 00  18 9  19 * 20 +  21 INT  22 STO IND 11  23 13  24 +  25 SIGN  26 ST+ IND L  27 DSE 11  28 GTO 00  29 X<>Y  30 FIX 0  31 CF 29  32 LBL 01  33 " "  34 ARCL X  35 SIGN  36 ST+ 11  37 CLX  38 STO 12 39 RCL 13            40 LASTX  41 AVIEW  42 LBL 02  43 ENTER  44 INT  45 10  46 ST/ Z  47 MOD  48 RCL IND Z  49 X=Y?  50 DSE 12  51 CLX  52 CLX  53 22  54 +  55 SIGN  56 ST+ IND L  57 RDN 58 DSE Y  59 GTO 02  60 RCL 12            61 ABS  62 RCL 13  63 X=Y?  64 GTO 04  65 >" "  66 31.022  67 ARCL Z  68 22  69 LBL 03  70 ENTER  71 CLX  72 X<> IND Z  73 RCL IND Y  74 XY  76 RDN 77 ST+ 12  78 SIGN  79 -  80 DSE Y  81 GTO 03  82 >"-"  83 ARCL 12  84 PROMPT  85 GTO 01  86 LBL 04  87 >"//"  88 9  89 RCL 11            90 X<=Y?  91 >"0"  92 ARCL 11  93 FIX 4  94 AVIEW  95 END

( 153 bytes / SIZE 032 )

 STACK INPUT OUTPUT X Nj Nb of guesses

Notes:

-Same usage but with 10-digit numbers, the execution time becomes 10 seconds instead of 30 seconds !
-Of course, an M-code routine will be even faster:

c)  M-Code Routine

Code   Mnemonic

0BF    "?"                             I've called this routine "MN?"
00E    "N"
00D    "M"
2A0    SETDEC                   the first executable word
046    C=0 S&X
10E    A=C ALL
046    C=0 S&X
02E    B=0 ALL
35C   PT=12
362    ?A#C @PT
057    JC +0A
310    LD@PT- C
3DC   PT=PT+1
0BA   A<>C M
350    LD@PT- D
3DC   PT=PT+1
0BA   A<>C M
0FA   C<>B M
23A   C=C+1 M
0FA   C<>B M
3D4   PT=PT-1
2E2   ?C#0 @PT
39F   JC -0D
0EE   C<>B ALL
0BC   RCR 5
2DC   PT=13
2E2   ?C#0 @PT
01B   JNC +03
33C   RCR 1
226   C=C+1 S&X
0E8   WRIT 3(X)                   here, X-register = the number of well-placed digits
04E   C=0 ALL
0EE   C<>B ALL
130    LDI S&X
013    CON: 19
3D4   PT=PT-1
2E2   ?C#0 @PT
04F   JC +09
0EE   C<>B ALL
0BC   RCR 5
2FE   ?C#0? MS
01B   JNC +03
33C   RCR 1
226    C=C+1 S&X
0A8   WRIT 2(Y)                  here, Y-register = the number of digits in a wrong position
3E0    RTN                            the routine always stops here
362    ?A#C @PT
057    JC +0A
310    LD@PT- C
3DC   PT=PT+1
0BA   A<>C M
350    LD@PT- D
3DC   PT=PT+1
0BA   A<>C M
0EE    C<>B ALL
23A    C=C+1 M
0EE    C<>B ALL
0AE   A<>C ALL
2FC   RCR 13
0AE   A<>C ALL
266    C=C-1 S&X
327    JC -1C
383    JNC -10

 STACK INPUTS OUTPUTS Y y N X x M

where

M = number of well placed digits
N  = number of digits in a wrong location

-Registers  L Z T and synthetic registers are unchanged

Example:

1234567791  ENTER^
2134567891  XEQ "MN?"  gives    7       3 4 5 6 7 9 1 ( the last "1" )  are well placed
X<>Y   2       2 1  ( the 2 first digits ) are in a wrong location

Notes:

-This routine gives exact results if all the digits are different from 0
-But whatever the inputs - any real number , alpha data or even non-standard bit patterns - there is no risk of "crash"
-In this case, however, the outputs ( which are always integers between 0 and 10 ) may be wrong.
-The execution time is at most 0.25 second.

d)  Program#3

-With the M-code routine above, "MMD" becomes much faster.

Data Registers:           •  R00 = pseudo-random numbers                     ( This register is to be initialized before executing "MMD" )

R01 = hidden number
R02 = number of guesses     R03 = number of digits

Flag:  F29
Subroutine:  "MN?"  the M-code routine listed in §2c)

-If you don't have a X-Functions module, replace lines 04 to 06 by       ENTER^   LOG   INT   1   +
but in this case, your first guess must be different from   999999999    if you play with 9-digit numbers
and smaller than   9999999989  if you play with 10-digit numbers
-Otherwise, the number of digits would be miscalculated !

 01  LBL "MMD" 02  FIX 0 03  CF 29 04  CLA  05  ARCL X  06  ALENG 07  STO 02  08  STO 03 09  CLX 10  LBL 00 11  10 12  * 13  RCL 00        14  R-D 15  FRC 16  STO 00 17  9  18  * 19  INT 20  1 21  + 22  + 23  DSE 02 24  GTO 00  25  STO 01        26  X<>Y 27  LBL 01 28  CLA 29  ARCL X 30  ISG 02 31  CLX 32  RCL 01 33  MN?  34  RCL 03  35  X=Y? 36  GTO 02       37  >": "  38  ARCL Y 39  >"-"  40  ARCL Z 41  PROMPT 42  GTO 01  43  LBL 02 44  >"//"  45  ARCL 02      46  FIX 4 47  SF 29 48  AVIEW 49  END

( 82 bytes / SIZE 004 )

 STACK INPUT OUTPUT X N /

Example:     1  STO 00  and suppose you want to play with 7-digit numbers

1111111   XEQ "MMD"  >>>>      "1111111:  1-0"               there is one "1"
1222222           R/S                        "1222222:  0-1"               the "1" is in wrong position and there is no "2"
3133333           R/S                        "3133333:  2-1"               2 digits are in right positions 1 digit is in a wrong position. There are two "3"s
3413444           R/S                        "3413444:  2-2"               2 digits are in right positions 2 digits are in a wrong position there is one "4"
3451355           R/S                        "3451355:  1-3"               1 digit  is  in right position   3 digits are in a wrong position there is no "5"
3314666           R/S                        "3314666:  1-4"               1 digit  is  in right position    4 digits are in a wrong position there is one "6"
4613377           R/S                        "4613377:  0-5"               4 6 1 3 3 are in a wrong location. There is no "7"
3866431           R/S                        "3866431:  2-3"              2 digits are in a right position, 3 in a wrong position. There is no "8"
3936149           R/S                        "3936149:  4-3"               4 digits are in a right position, 3 in a wrong position.  There are two "9"s
3936914           R/S                        "3936914 / / 10"               you've found the hidden number in 10 guesses.

Notes:

-"MMD" displays  "M-N" after your guess to mean that M digits are in right positions and N digits are in wrong positions.
-The hidden number is computed after your first guess but - of course - independently, except for the number of its digits.
-After your first guess, all your guesses must have the same number of digits: these programs do not check !