The Museum of HP Calculators

# Mastermind for the HP-41

This program is Copyright © 2007 by Jean-Marc Baillard and is used here by permission.

This program is supplied without representation or warranty of any kind. Jean-Marc Baillard and The Museum of HP Calculators therefore assume no responsibility and shall have no liability, consequential or otherwise, of any kind arising from the use of this program material or any part thereof.

## Overview

1°) Program#1   ( 6-digit numbers only )
2°) A Micro-Code Routine
3°) Program#2   ( from 1- to 10-digit numbers )

-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°) Program#1 ( 6-digit numbers )

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

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

01  LBL "MMD"
02  6
03  STO 10
04  LBL 00                         Lines 02 thru 16 computes the 6 digits of the mystery number
05  SIGN
06  RCL 00
07  R-D
08  FRC
09  STO 00
10  9                                   If, for instance, you want to use digits from 1 to 7 only, replace this line by  7
11  *
12  +
13  INT
14  STO IND 10
15  DSE 10
16  GTO 00
17  X<>Y
18  FIX 0
19  CF 29
20  LBL 01
21  " WAIT . . ."
22  AVIEW
23  CLA
24  ARCL X
25  16.01
26  STO 07
27  STO 08
28  6
29  STO 09
30  RCL Z
31  ISG 10
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  X#Y?
42  GTO 02
43  "~*"                        append  *
44  ST- IND 07
45  CHS
46  LBL 02
47  STO IND T
48  R^
49  DSE X
50  R^
51  DSE 07
52  GTO 02
53  ALENG                 if you don't have an X-functions module, replace lines 53-54  by  RCL 17   6
54  12                                                                                       add   ISG 17  after line 45
55  X=Y?                                                                          and  add   0  STO 17  RDN  after line 20
56  GTO 05
57  LBL 03
58  RCL 08
59  STO 07
60  LBL 04
61  RCL IND 07
62  RCL IND 09
63  X#Y?
64  GTO 04
65  "~/"                        append  /
66  ST- IND 07
67  CHS
68  STO IND 09
69  LBL 04
70  DSE 07
71  GTO 04
72  DSE 09
73  GTO 03
74  PROMPT
75  GTO 01
76  LBL 05
77  RCL 10
78  FIX 4
79  SF 29
80  AVIEW
81  END

( 138 bytes / SIZE 017 )

Example:     1  STO 00

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

-Press the backarrow-key to see that you've made 9 guesses ( this number is also in register R10 )

Notes:

-Each "*" represents a well placed digit, each "/" denotes a digit in a wrong position
-Other characters are of course possible.
-The above strategy is not the best one!
-You have to " WAIT. . ."  for 14 seconds ( approximately )
-The hidden number is computed after your first guess but independently - of course!

-This program can be easily generalized for more than 6-digit numbers
but you'll have to wait a relatively long time if you want to play with 10-digit numbers
-The following M-code routine does the main job in at most  0.25 second

2°) A Micro-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:

-Key in the codes very carefully!
-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.

3°) Program#2 ( from 1- to 10-digit numbers )

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

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

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

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

01  LBL "MMD2"
02  FIX 0
03  CF 29
04  CLA                              If you don't have a X-Functions module, replace lines 04 to 06 by       ENTER^   LOG   INT   1   +
05  ARCL X                        but in this case, your first guess must be different from   999999999    if you play with 9-digit numbers
06  ALENG                                                                                  and smaller than   9999999989  --------------- 10-digit numbers
07  STO 02                         because roundoff-errors would miscalculate the number of digits!
08  STO 03
09  CLX
10  LBL 00                         Lines 09 thru 25 computes the mystery number
11  10
12  *
13  RCL 00
14  R-D
15  FRC
16  STO 00
17  9                                   If, for instance, you want to use digits from 1 to 7 only, replace this line by  7
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?                           the M-code routine
34  RCL 03
35  X=Y?
36  GTO 02
37  "~: "                            append : space
38  ARCL Y
39  "~-"                            append -
40  ARCL Z
41  PROMPT
42  GTO 01
43  LBL 02
44  "~//"                           append / /
45  ARCL 02
46  FIX 4
47  SF 29
48  AVIEW
49  END

( 83 bytes / SIZE 004 )

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

1111111   XEQ "MMD2"  >>>>      "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 -------------------------- 2 digits are in a ------------ there is one "4"
3451355           R/S                           "3451355:  1-3"               1 -----  is  ----------------  3 -------------------------- there is no "5"
3314666           R/S                           "3314666:  1-4"               1 -------------------------  4 -------------------------- 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 -------------------------------------------------  There are two "9"s
3936914           R/S                           "3936914 / / 10"               you've found the hidden number in 10 guesses.

-Unlike "MMD", "MMD2" displays  "M-N" after your guess to mean that M digits are in right positions and N digits are in wrong positions.
-Using "*" & "/" would be difficult to read for more than 6-digit numbers.
-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: this program does not check!