From berg@cip-s01.informatik.rwth-aachen.de Mon Apr 30 19:00:58 1990
From: berg@cip-s01.informatik.rwth-aachen.de (SRB)
Newsgroups: comp.sys.handhelds
Subject: Mastermind game (HP28C/28S/48SX)
Date: 26 Apr 90 12:33:44 GMT

There may have been a previous accidental posting of this article, because
I have been struggling with 'rn' a bit.  For anyone who has seen the previous
posting: this one is commented and has checksums!


Have you ever tried to play mastermind on your own?  Here's the solution:

Note: it was written originally for the HP28C (i.e. memory conservative),
therefore the speed of this program can be improved considerably.
But, considering the application, the response time may be well within bounds.

Playing instructions:
----------------------------------
To start up a mastermind session execute 'init' first, then enter your guesses.
To enter a guess, first type a #-sign, then type the colours, then
execute 'gues'.

Every time you execute 'init', a new permutation is made up by your calculator
according to the restrictions in 'col', 'dig', and 'sam'; it is stored in 's'.

If you want to check (or peek at) the correct guess, just execute 's':
disregard the leading F's and you've got the desired guess.

You could even have someone else make up a guess, and put it into 's' manually.
Though, be sure to adjust 'dig' to the desired number of digits.

The correctness of your guess is reported after executing 'gues'.
In line one you'll see appear two numbers:
  Before the point: the no. of colours in correct positions
  After the point:  the no. of colours misaligned

You can adjust 'col', 'dig', and 'sam' to your personal taste, but all the
following conditions have to be met:
1<=col<=16  1<=sam<=15  1<=dig<=16  dig<=col*sam

'col' specifies the maximum number of colours used.
'dig' specifies the number of digits used.
'sam' specifies the maximum number of digits of one same colour.

The colours are defined as: 0 1 2 3 4 5 6 7 8 9 A B C D E F
---------------------------------
Enter it as follows:

HEX 64 STWS

\<< 0 R\->B "1 0 'r" STR\-> STO dig   @ #0 initializes colour counter, reset 'r'
                                      @ 'r' will count exact matches.
  START OVER                          @ Get the entered digits xor'ed with 's'
    IF t                              @ Is this digit a perfect match?
    THEN 8 R\->B 0 5 PICK t 4 *        @ No, then get entered colour and count
      START RL                         @ it in our colour counter on the stack.
      NEXT +
    ELSE "1 'r" STR\-> STO+            @ Yes, count it in 'r'.
    END ROT u ROT u ROT               @ Shift both xor'ed and entered digits.
  NEXT 3 ROLLD DROP2                  @ Return the matching colour count
\>> 'v STO  [6FFD]                    @ And return 'r' off course.

\<< RR RR RR RR
\>> 'u STO  [9370]

\<< 9 6 + R\->B AND B\->R             @ Memory conservative version of: #Fh AND
\>> 't STO  [7457]

#0 's STO

1 'sam STO

8 'dig STO

10 'col STO

\<< 0 R\->B NOT 's' STO 1 dig         @ Initialize 's' with 16 #Fh (I have
  FOR c 0                             @ my reasons).
    DO DROP "0 'r" STR\-> STO RAND col * IP s 2 c   @ Make up new colour.
      START DUP2 t SAME 'r' STO+ u    @ Count how many of this colour already
      NEXT DROP r                     @ exist.
    UNTIL sam <                       @ Check if we exceed maximum duplicates.
    END s SL SL SL SL "+ 's" STR\-> STO   @ Apparently not, so add it to list.
  NEXT
\>> 'init STO  [11BF]

\<< DUP s XOR SWAP OVER v s ROT v -9 6  @ Check the colour counts for both
                                        @ the entered digits and for 's'
                                        @ and count the number of exact
                                        @ matches.
  START u SWAP u OVER t OVER "t MIN 100 / 'r" STR\-> STO+   @ Now find the
                                        @ highest number of COMMON colours
                                        @ by taking the minimum of the 2
                                        @ colour counts.
  NEXT DROP2 RCLF 2 FIX "r 1 'r" STR\-> PURGE DISP STOF
\>> 'gues STO  [A0D1]

--------------------------------
Known features:

This program is, as far as I know now (1990-04-26), a minimum-memory-usage
solution; I could have speeded it up, but every change I make to its listed
form, causes it to use more memory (especially important to those HP28C users);
currently it uses 592.5 bytes (=static memory usage, dynamic memory usage is
larger).

'init' always leaves a temporary variable 'r' in memory, you can purge it
manually or you can just leave it there, because 'gues' always cleans it up.

If 'col' contains 16, then the leftmost digit will never be F.  This is a
limitiation of 'init', not of 'gues'.
(I do not intend to fix this, because you rarely use 16 colours and the extra
memory it would cost to fix this, doesn't seem worth it).

'init' and 'gues' assume that you have a word size of 64 (I use the philosophy
that if any program uses another word size than 64, then it should restore it
to 64 afterwards).

You have to enter the number in HEX mode.

A possible improvement (especially for HP28S users might be the following
little program, which presses the #-sign for you (it's on the other keyboard):
\<< \->STR "#" SWAP + STR\-> gues \>>
-------------------------------
I'd be happy to receive comments, suggestions or yet-unknown-'features' about
this program.
I dare anyone to further compact the code (impossible, if you ask me) without
cripling functionality!
--
Sincerely,                         | berg@cip-s01.informatik.rwth-aachen.de
           Stephen R. van den Berg | ...!uunet!mcsun!unido!rwthinf!cip-s01!berg

