HP Forums
(35s) MasterMind game - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (35s) MasterMind game (/thread-2375.html)



(35s) MasterMind game - John Galt - 11-01-2014 11:26 PM

This is my first attempt at posting code on this forum. The formatting isn't what I'd like it to be but I'll work on that if someone is kind enough to post some hints. As the date implies I wrote it some time ago.

Code:
MasterMind for HP 35s by John Galt 5-1-2009

Comments are in the right column followed by the semicolon. Some commands like "roll down" are difficult to translate into text, so the comments explain where necessary.

LBL A
SF 10            ;cause messages to be displayed to prompt user input
SF 7             ;display only integers with no radix
-1 
STO N            ;N is the user's guess counter, used for maximum limit
STO M            ;M is an iteration counter for the random 
                 ;number generator. 
                 ;Use for curiosity or debugging. It's not required.
LBL B            
1
STO+ M
4
XEQ R001        ;generate a four digit number
1000
X > Y?        ;make sure it's larger than 1000
GTO B001            ;if not, discard it and get another number

                ;The following routine strips each digit from 
                ;the pseudorandom number ABCD and stores them 
                ;in memories A,B,C,D

RDN                 ;RDN means "roll down"
STO E            ;random number is > 1000 so store it in E
1                ;get the ones position digit
XEQ S001
STO D            ;put it in D
RCL E
2                ;get the tens position digit
XEQ S001
STO C            ;put it in C
RCL E
3                ;get the hundreds position digit
XEQ S001
STO B             ;put it in B
RCL E
4                ;get the thousands position digit
XEQ S001
STO A             ;put it in A

                ;begin target number qualification

RCL B
X=Y?            ;test for A=B
GTO B001
RDN                ;RDN means "roll down"
RCL C
X=Y?            ;test for A=C
GTO B001
RDN                 ;RDN means "roll down" and I'm not telling you again.
RCL D
X=Y?            ;test for A=D
GTO B001
RCL B
RCL C
X=Y?            ;test for B=C
GTO B001
RDN
RCL D
X=Y?            ;test for B=D
GTO B001
RCL C
RCL D
X=Y?            ;test for C=D
GTO B001
RCL M            ;random number iteration counter. Not required.

FIRST GUESS        ;Enter this prompt by using EQN mode,
                ;then RCL F, RCL I, RCL R, etc. 
                ;Program execution stops here and waits 
                ;for user input followed by R/S

LBL F            ;Main game loop
CF 10
RCL E
X=Y?            ;Test for winning guess
GTO W001
RDN
7                ;Maximum guess counter. 
                ;It can be anything you want, 7 is reasonable
RCL- N            ;compare max to the guess counter
X=0?
GTO L001            ;Maximum guesses reached - loser
RDN
XEQ C001            ;strip and store
XEQ D001            ;black check
XEQ E001            ;white check
1
STO+ N            ;increment guess counter
RCL F            ;display the user entered guess
[X,Y,N]            ;Use Equation mode to enter this.
STOP             ;Stop and display guess results.
GTO F001            ;Accept input then continue main game loop

LBL C            ;Strip digits from the entered guess.
                ;This routine takes the user's guess
    
            ;and stores each digit in Q,R,S,T
STO F            ;save guess
1                ;get the ones position digit
XEQ S001
STO T            ;put it in T
RCL F
2                ;get the tens
XEQ S001
STO S            ;put it in S
RCL F
3                ;get the hundreds
XEQ S001
STO R            ;put it in R
RCL F
4                ;get the thousands    
XEQ S001
STO Q            ;put it in Q
RTN


LBL D            ;Black check. 

                ;The following tests for correct digits
                ;in the correct positions.
0
STO X            ;initialize the black counter
RCL A
RCL Q
X=Y?            ;A=Q?
XEQ X
RCL B
RCL R
X=Y?            ;B=R?
XEQ X
RCL C
RCL S
X=Y?            ;C=S?
XEQ X
RCL D
RCL T
X=Y?            ;D=T?
XEQ X
RTN

LBL X            ;X is the black counter.
1
STO+ X
RTN

LBL E            ;White check

                ;The following four routines compare A,B,C,D 
                ;to Q,R,S,T respectively
0
STO Y            ;initialize the white counter
RCL A
RCL R
X=Y?            ;A=R?
XEQ Y
RCL A
RCL S
X=Y?            ;A=S?
XEQ Y
RCL A
RCL T
X=Y?             ;A=T?
XEQ Y

RCL B
RCL Q
X=Y?            ;B=Q?
XEQ Y
RCL B
RCL S
X=Y?            ;B=S?
XEQ Y
RCL B
RCL T
X=Y?             ;B=T?
XEQ Y

RCL C
RCL Q
X=Y?            ;C=Q?
XEQ Y
RCL C
RCL R
X=Y?            ;C=R?
XEQ Y
RCL C
RCL T
X=Y?             ;C=T?
XEQ Y

RCL D
RCL Q
X=Y?            ;D=Q?
XEQ Y
RCL D
RCL R
X=Y?            ;D=R?
XEQ Y
RCL D
RCL S
X=Y?             ;D=S?
XEQ Y
RTN

LBL Y            ;Y is the white counter. 
1 STO+ Y
RTN
                ;various routines follow

LBL R            ;x digit random number generator
10^x
RANDOM
*
IP
RTN                ;Returns an x digit integer

LBL S            ;Digit picker returns a single digit
                ;Takes 1,2,3, or 4 from X
                ;and picks off a single digit from Y

10^x            ;ten to the x
÷                ;divide
FP                ;strip the rest
10
*                 ;move the digit to the ones place
IP                ;strip the rest
RTN

LBL W            ;"Winner" routine
SF 10            ;reset to your desired behavior
CF 7                ;reset to your desired behavior
RCL N            ;recall the number of guesses
X<>Y            ;puts the solution in X
x YOU WIN x        ;use any appropriate text here.
GTO A001            ;reset program to the top.

LBL L            ;"Loser" routine
RCL E            ;reveal the number.
SF 10            ;reset to your desired behavior
CF 7                ;reset to your desired behavior
LOSER             ;use any appropriate text here.
GTO A001            ;reset program to the top.

*********************************

Memories used

A,B,C,D each contain single digits to guess in the number ABCD
E contains the four digit random number the user must determine
F contains the user's entered guess
M random number iteration counter (for debugging, etc)
N contains the users's guess counter
Q,R,S,T each contain the user entered digits in the number QRST
X black counter
Y white counter

No attempt is made to preserve memory or stack contents

*********************************

Instructions

This is a variant of the game "cows and bulls" in which the user must guess a four digit number. The HP replies with the number of digits that are in the correct position (black token), and the number of digits that are in the wrong position (white token).

Start at LBL A and press R/S. The program sets initial conditions, chooses the pseudo-random number, validates it, and then waits for user input. Game play is explained with an example following the program description below.

Program description

The number the HP chooses will be a four digit number greater than 1000 and that has no repeating digits. In other words the number will be in the range 1023 to 9876 but many numbers in between don't qualify. For program readability, the routine that qualifies the random number for these constraints uses a brute force method that disqualifies anything less than 1000, then proceeds to check for repeated digits. 

The purpose of this check is to make game play sufficiently challenging yet not overly time-consuming. The algorithm can be adapted to HP calculators with a real time clock to introduce a time constraint in addition to limiting the number of guesses.

Example

Let's assume the HP randomly chose the number 3510. 

At the "First Guess" prompt, enter a wild guess such as 1234 and press R/S.

The response will resemble the following

1234
[0,2,0]

The display indicates the user entered 1234. The three digits in the matrix [0,2,0] mean NONE of the entered digits are in the correct position, but TWO of them are contained in the four digit number the user has to guess. The final number 0 represents the number of guesses entered so far. The first guess is not counted because the user has no information on what number to enter.

Enter another number consistent with these hints such as 3456 and press R/S.

The response will resemble the following

3456
[1,1,1]

This means the user entered 3456. ONE of those digits is in the correct position. ONE additional digit is contained in the number you are attempting to determine, but it are not in the correct position. The last digit 1 means that this is your first attempt to determine the number. It is incremented with each guess. If it reaches the maximum (this program is written for a limit seven guesses) the user loses.

Enter another number, such as 3578 and press R/S.

The response will resemble the following

3578
[2,0,2]

The user entered 3578. TWO of those digits are in the correct position, which means you're getting closer. The 0 means NO other digits entered were contained in the number you are attempting to determine. The last digit 2 means that this was your second guess based upon the hints you've received.

3590
[3,0,3]

Now THREE of the digits entered are in the correct position and NO other digits are contained in the target.

Reviewing the previous entries should eventually lead you to correctly guessing the answer. Every digit from 0 to 9 has been entered at least once, so you have been playing efficiently. In this last guess, three of them are in the correct position. You have to decide which ones they are, as well as determine the fourth digit and where it goes.

Let's try 3591:

3591
[2,1,4]

What happened? TWO of the numbers are in the correct position. ONE additional number is contained in the target, but it is not in the correct position.

This is the point in the game that becomes especially challenging since by now you should have nearly all the information necessary to correctly guess the number. To win within the limit of seven guesses you should always enter a number that does not contradict previous results.

Why limit the number of guesses? The reason is that I have always been able to solve the puzzle in seven or eight attempts. If it took more than that, one of the guesses would have conflicted with earlier results. Entering trivial or nonsense guesses such as 1111 or 2222 will not return reliable results in this game's implementation. User input could be range-checked or otherwise qualified for an entry meeting the rules, but I have not attempted to do that in the 35s.

The maximum number of guesses required to determine any four digit number meeting the game's constraints can be mathematically determined but I have not attempted to do that. I'll leave that to you.