[comp.sys.handhelds] hi

berg@cip-s02.informatik.rwth-aachen.de (SRB) (04/11/90)

Newsgroups: comp.sys.handhelds
Subject: Mastermind game (HP28C/28S/48SX)
Distribution: world 

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
  START OVER
    IF t
    THEN 8 R->B 0 5 PICK t 4 *
      START RL
      NEXT +
    ELSE "1 'r" STR-> STO+
    END ROT u ROT u ROT
  NEXT 3 ROLLD DROP2
>> 'v STO

<< RR RR RR RR
>> 'u STO

<< 9 6 + R->B AND B->R
>> 't STO

#0 's STO

1 'sam STO

8 'dig STO

10 'col STO

<< 0 R->B NOT 's' STO 1 dig
  FOR c 0
    DO DROP "0 'r" STR-> STO RAND col * IP s 2 c
      START DUP2 t SAME 'r' STO+ u
      NEXT DROP r
    UNTIL sam <
    END s SL SL SL SL "+ 's" STR-> STO
  NEXT
>> 'init STO

<< DUP s XOR SWAP OVER v s ROT v -9 6
  START u SWAP u OVER t OVER "t MIN 100 / 'r" STR-> STO+
  NEXT DROP2 RCLF 2 FIX "r 1 'r" STR-> PURGE DISP STOF
>> 'gues STO

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

This program is, as far as I know now (1990-04-09), 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 can never be F.
(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).
-------------------------------
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
--
Sincerely,                         | berg@cip-s01.informatik.rwth-aachen.de
           Stephen R. van den Berg | ...!uunet!mcsun!unido!rwthinf!cip-s01!berg