Post Reply 
(newRPL) Mastermind solver
08-26-2023, 03:22 PM (This post was last modified: 09-02-2023 12:54 PM by Gilles.)
Post: #1
(newRPL) Mastermind solver
This small program plays the role of the decoder in the classic Mastermind game (4 holes, 6 colors). It uses newRPL LstX library v1.01d
Code:
DIRECTORY
'Init' « 1 6 LSEQR 4 NDUPN →LIST LCPRD 'Pool' STO 0 'Try' STO Guess »
'Guess' 
 « IF Try TYPE 62 == THEN 
     DUP  { 'GP' 'WP' } STO 
     Pool 
     « IF DUP Try == ΣLIST GP == THEN 
         IF Try SWAP LINCL SIZE GP - WP == THEN 1 ELSE 0 END
       ELSE DROP 0 END
     » LFILT 'Pool' STO
   END
   Pool DUP SIZE RAND * 1 + IP GET DUP 'Try' STO 
 »
ENDDIR

To use it:
- Think of a combination (a combination of 4 digits, with numbers from 1 to 6, for example 4524)
- Press 'Init', a combination proposal is made by the calculator
- Enter according to the rule of the Mastermind the number of correctly placed digits and fine but misplaced digits in the form of a list { well placed, misplaced }
- Press Guess, a new guess is made, answer as in the previous point then 'Guess' etc until the solution is found.

About the algorithm: The algorithm is simple:
- Init generates all the possible combinations (Cartesian product: 1296 possibilities) and stores them as a list in the "Pool" variable. It offers a first random proposal.
- At each game turn, the program deletes in 'Pool' all the impossible solutions (this replays the last attempt and checks that the answer is correct), and chooses a new random proposal from the update 'Pool' list
- We start all over again with new guess until the prg found the solution

That's all ;D Very simple but very effective!
Nota : It could be easily adapted for stock RPL and ListExt library
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)