(11C) Code guessing game
|
04-25-2018, 06:18 PM
(This post was last modified: 04-25-2018 07:03 PM by Rogier.)
Post: #1
|
|||
|
|||
(11C) Code guessing game
Also known as "Mastermind". I wrote it somewhere around 1985. The calculator takes a number of 3-10 digits and up to 9 colours for you to guess. C starts a new game, D enters a guess. Result: number in the form a,b where a = number of digits with right colour, and b is the number of digits of the right colour which are in the right place. Or: number FIX9 > you guessed it.
21-Mastermind-11C.pdf (Size: 61.14 KB / Downloads: 39) |
|||
04-25-2018, 08:50 PM
Post: #2
|
|||
|
|||
RE: (11C) Code guessing game
Looks great!
I tried to get this working on a DM15L (HP15C clone), but couldn't. I changed register 1 to 7 to make sure that it's cleared by Clear Sum, but it's not enough. When I make a totally wrong guess I get 0.0, but other guesses only return 1.0 or 1.1. (Haven't tried it on my 11C emulator yet, but that one is inaccurate anyways). Which other difference besides CLEAR SUM clearing 0-5 on 11C but 2-7 on 15C are there between these calculators? Also, do you have a "human readable" version of the program? |
|||
04-25-2018, 10:58 PM
Post: #3
|
|||
|
|||
RE: (11C) Code guessing game
Never mind, I just had a typo (confused FRAC with INT in one place). It works like that on the Android 11C app and if you change R1 to R7 it also works on the DM15L (and probably 15C).
Really great program! |
|||
04-26-2018, 06:57 PM
(This post was last modified: 05-02-2018 08:23 PM by Dieter.)
Post: #4
|
|||
|
|||
RE: (11C) Code guessing game
(04-25-2018 06:18 PM)Rogier Wrote: Also known as "Mastermind". I wrote it somewhere around 1985. The calculator takes a number of 3-10 digits and up to 9 colours for you to guess. C starts a new game, D enters a guess. Result: number in the form a,b where a = number of digits with right colour, and b is the number of digits of the right colour which are in the right place. Or: number FIX9 > you guessed it. Ah, Mastermind / Bagels / Code Breaker – one of the first useful programs I wrote for the 34C back then. I used a different approach that stored the code digits in individual registers. And the code always consisted of four different colors. So let me add the following 11C/15C version to this thread. (15C commands in brackets, where required) Code: 001 LBL A Edit: changed the roles of R,0 and R7 for 11C compliance and removed an obsolete line. Hint for 15C users – you may replace the final lines after "GTO 0" with RCL 9 FIX 0 SF 9 RTN. If the code has been correctly guessed this yields a flashing display with the number of required guesses. ;-) You can also replace line 72...75 with DSE 0. The code length is set in line 005, change it as desired to something between 1 and 6. The number of different colors is set in line 010. Must be ≥ code length and ≤ 9. Default is a 4-digit code with each digit between 1 and 6. For your first attemps you may change this to a 3-digit code with 5 colors. [A] generates the code => 0,0 Enter your guess and press [R/S] or [B] => x,y Here x is the number of right colors in the right place, and y is the number of colors that are correct, but in the wrong position. Example: [A] => 0,0 Note: generated code is assumed to be 4532 1234 [B] => 1,2 1325 [B] => 0,3 3254 [B] => 0,4 2534 [B] => 2,2 2435 [B] => 1,3 4532 [B] => 4,0 06 So you finally got all 4 colors right and required 6 guesses. Dieter |
|||
04-26-2018, 10:10 PM
(This post was last modified: 04-26-2018 10:13 PM by Rogier.)
Post: #5
|
|||
|
|||
RE: (11C) Code guessing game
I'll give your program a try, Dieter. Looks fine too. The scoring is more in line with "official" Mastermind rules and it is shorter.
Michaelzinn, what do you mean by human readable form? (By the way: I added a PDF because I haven't figured out yet how to add a code block in a post.) |
|||
04-27-2018, 06:54 AM
Post: #6
|
|||
|
|||
RE: (11C) Code guessing game
(04-26-2018 10:10 PM)Rogier Wrote: (By the way: I added a PDF because I haven't figured out yet how to add a code block in a post.) The message editor of this forum has a few buttons on top of the message window. There is a "#" Symbol. If you klick on it a "code" tag (enclosed in square brackets) is inserted. Press on "close tags" and the respective end tag is added. Now you can place your code between these two tags. If you read this post and press the "quote" button below it you can see how it works: Code: This Of course you can also insert the code-tags manually. That's how I usually do it. Dieter |
|||
04-27-2018, 12:52 PM
(This post was last modified: 04-30-2018 09:13 AM by Dieter.)
Post: #7
|
|||
|
|||
RE: (11C) Code guessing game
(04-26-2018 10:10 PM)Rogier Wrote: I'll give your program a try, Dieter. Looks fine too. The scoring is more in line with "official" Mastermind rules and it is shorter. I have tried to understand how your program works, especially the scoring routine, but I admit I haven't understood all details. For comparison here is how my version works: LBL A: code generation. This generates four individual random digits. A counter in R0 is incremented on each loop and the loop at LBL 2 checks if the digit is equal to one of the previously generated ones. In this case a new digit is generated. Else the number is stored in the next higher data register. This way (assuming the default settings) R1...R4 are filled one after another with four different numbers between 1 and 6. The code length (here 4) is kept in R,0 so that it can be recalled from there later. LBL B: score evaluation. The guess is stored in R7, the guess counter in R9 incremented and the loop counter in R0 is initialized with 4 (guess digits to compare). The main loop at LBL 3 splits off a single guess digit (from right to left) and checks if it matches one of the code digits. This is done in the loop at LBL 4. This way first the 4th guess digit is compared with the code digits #4, #3, #2, #1, then the 3rd guess digit is compared with the code digits #4, #3, #2, #1, ... etc. If a match is detected the loop is exited (GTO 6) and the score is added: if the loop counter in R0 - which indicates the current digit's position in the guess - matches the position in the code - which is held in R I - the score in R8 is increased by 1. If the positions are not equal only 0,1 is added. This can be done very elegantly with a simple test that skips over the decimal point if the positions match. Finally (LBL 5) the loop counter is decremented until it becomes zero. 15C owners may also use a simple DSE 0 here. After all digits have been compared the program checks if the score equals the code length, which means that all guess digits were correct. If not it jumps back to LBL 0 to display the score and stop, else it moves the number of guesses into the tens exponent so that the final display in scientific notation shows the score along with the number of required guesses, e.g. "4,0 12". I hope this was clear enough. ;-) If not, just ask. Dieter Edited to reflect the program's updated register use. |
|||
04-28-2018, 12:24 PM
(This post was last modified: 04-28-2018 05:42 PM by Rogier.)
Post: #8
|
|||
|
|||
RE: (11C) Code guessing game
Thanks for the help on the "code" tag. I'll try it with another program.
As to my scoring routine: I slice off digits from code and guess and compare them. If they are equal, I add 1 to the score, if the integer part of (log [remaining part code] - log [remaining part guess]) = 0 then the digits are in the same place and I add 0,1 tot the score. What the program doesn't do, is check is whether a number in your guess has already been examined. So: Code = 451 Guess = 112 > 2,0 Two digits correct, not in the right place Guess = 111 > 3,1 All correct, one in the right place Code = 445 Guess = 411 > 1,1 One digit correct and in the right place I give a bit more information than Mastermind rules, but with 10 places, 9 colours it's still quite a challenge. (And time-consuming; scoring 10 digits takes 2min24sec to 2min54sec on my HP-11C. Touch 11i free on my phone is even slower. Plus: it displays the X-register during program execution, which is a bit of a spoiler, because that shows you the code.) |
|||
04-28-2018, 07:10 PM
(This post was last modified: 04-28-2018 07:17 PM by Michael Zinn.)
Post: #9
|
|||
|
|||
RE: (11C) Code guessing game
(04-26-2018 10:10 PM)Rogier Wrote: Michaelzinn, what do you mean by human readable form? To me, it was very difficult to understand how the program works. Comments, especially for the labels, would help. I reverse engineered the code and commented it a bit (Ignore the parens I'm currently experimenting with some ideas to "compile" code from edn): Code:
|
|||
04-28-2018, 07:23 PM
(This post was last modified: 05-02-2018 06:56 PM by pier4r.)
Post: #10
|
|||
|
|||
RE: (11C) Code guessing game
Question about Mastermind, that is a short but intense game (ever tried to make little tournaments?).
Did anyone create some "computer opponent" on calculators for the game? So an heuristic search for solutions. Wikis are great, Contribute :) |
|||
04-29-2018, 09:00 PM
Post: #11
|
|||
|
|||
RE: (11C) Code guessing game
(04-28-2018 12:24 PM)Rogier Wrote: Code = 451 Shouldn't this be 2,1 ? The left and middle "1" are correct, but in the wrong place (1,0 + 1,0). The right "1" is correct and in the right place (0,1). Suppose the code is 333. What should be returned for a guess of 333 (if it isn't already detected as "correct" at the beginning). The first "3" in the guess is correct and in the right place (0,1) and also 2x correct but in the wrong place (2,0). This yields 2,1. Or, after all three digits have been evaluated: 6,3. Is this correct? Dieter |
|||
04-29-2018, 09:58 PM
(This post was last modified: 04-29-2018 10:31 PM by Rogier.)
Post: #12
|
|||
|
|||
RE: (11C) Code guessing game
Michealzinn, I'll try and add more comments to other programs, but since I wrote most of my programs over 30 years ago, you're not the only one having problems understanding the structure. At the time I was more interested in how to operate the programs than in documenting how they work.
Dieter, about the scoring: I compare each digit of the code to the last digit of the guess, then I divide the guess by 10 and repeat the procedure until the guess is all sliced up. The scores I quoted are correct (at least: as intended within the parameters of the program): Code = 451 Guess = 112 > 2,0 Two digits correct, not in the right place Guess = 111 > 3,1 All correct, one in the right place If the code were 333, a guess of 333 should score 3,3; 3 digits correct, all of which in the right place. You never see that, but entering 3333 yields 4,3. (I don't check for the right length when you enter a guess. Would be relatively easy to fix of course.) I'll have to adapt your program by the way Sto/,0 doesn't work on an HP-11c. Pier4r: I have thought about reversing the roles and let the calculator guess, but gave up on it. Not a chance of fitting it in an HP-11c, and too complex (at least for me) to program it in my ZX-Spectrum or in GW-basic. |
|||
04-30-2018, 08:11 AM
(This post was last modified: 04-30-2018 08:55 AM by Dieter.)
Post: #13
|
|||
|
|||
RE: (11C) Code guessing game
(04-29-2018 09:58 PM)Rogier Wrote: I compare each digit of the code to the last digit of the guess, then I divide the guess by 10 and repeat the procedure until the guess is all sliced up. OK, this makes sense – let's do this for the example: (04-29-2018 09:58 PM)Rogier Wrote: The scores I quoted are correct (at least: as intended within the parameters of the program): Take the last digit of the guess, 1. Compare it with the code from right to left: 1: correct and in the right place => +0,1 5: no match 4: no match Slice off the rightmost digit, so the remaining guess is 11. Take the last digit of the guess, 1. Compare it with the code from right to left: 1: correct, but in wrong place => +1,0 5: no match 4: no match Slice off the rightmost digit, so the remaining guess is 1. Take the last digit of the guess, 1. Compare it with the code from right to left: 1: correct, but in wrong place => +1,0 5: no match 4: no match Slice off the rightmost digit, so the remaining guess is 0 => scoring completed. Score is 0,1 + 1,0 + 1,0 = 2,1 You say you get 3,1. So the three guess digit gets four (!) scores. On a real Mastermind board you this would be equivalent to 3 white pegs and 1 black one. The correctly positioned "1" gets two (!) ratings: both a black one and a white one. So the rightmost 1 in the code is evaluated this way: Take the last digit of the guess, 1. Compare it with the code from right to left: 1: correct (is part of the code) => +1,0 furthermore it also is in the right place => +0,1 5: no match 4: no match (04-29-2018 09:58 PM)Rogier Wrote: If the code were 333, a guess of 333 should score 3,3; 3 digits correct, all of which in the right place. OK, so again the 3 guess digits are rated twice: Once for the right digit being somewhere in the code, and another time for it being in the right position. If this is your intention the score is fine. But I wonder if it matches the Mastermind rules. (04-29-2018 09:58 PM)Rogier Wrote: You never see that, but entering 3333 yields 4,3. (I don't check for the right length when you enter a guess. Would be relatively easy to fix of course.) I suspected this (my good old 34C didn't allow storage arithmetics on R,0...R,9 either). Actually I wrote the program on a 15C emulator. #-) You may simply swap R,0 and R7. I now have edited the original listing. Thank you very much for this hint. (04-29-2018 09:58 PM)Rogier Wrote: Pier4r: I have thought about reversing the roles and let the calculator guess, but gave up on it. Not a chance of fitting it in an HP-11c, and too complex (at least for me) to program it in my ZX-Spectrum or in GW-basic. Back in the days I have been thinking about such a program either, but even if it could have been done on a more powerful calculator like the 41C I admit my skills were (and still are) too limited for such a project. Dieter |
|||
04-30-2018, 07:29 PM
(This post was last modified: 04-30-2018 08:05 PM by Rogier.)
Post: #14
|
|||
|
|||
RE: (11C) Code guessing game
I'm pretty sure my scoring isn't official Mastermind, but it's what I came up with.
The minor problem is the double scoring of digits of a correct colour AND in the right place. That could easily be solved (multiply by 10 and subtract from the total of correct digits). I just never bothered. I can live with a result "a correct, of which b in the right place". The major problem is that I don't check whether a colour has already been tested and with what kind of result: - not present > ignore for the rest of this turn, - present, wrong place > ignore unless you find one in the right place, then adjust score - present, in the right place > ignore for the rest of this turn I don't remember why I chose this system (I didn't know all Mastermind rules? Couldn't fit in a better one?). With it's limitations I still think it's one of my better programs and fun to play (and a challenge if you use 10 places, 9 colours, possibility of one or more colours appearing more than once). Playing your program with a slightly different set of rules should be fun too. I'm going to give it a try. |
|||
05-02-2018, 06:49 PM
(This post was last modified: 05-02-2018 08:10 PM by Rogier.)
Post: #15
|
|||
|
|||
RE: (11C) Code guessing game
I've had a chance to look at your program now Dieter. Works great. I didn't clock them, but I think it feels faster than my routine as well. The use of the exponent to show the number of guesses is a very nice touch. Never thought of that.
You solve the problem of the double scores by not permitting multiple identical digits in the code. I think I have come up with a way to make my program Mastermind-proof, while retaining the option to use identically coloured pegs in the code. If I get it running I'll post it. |
|||
07-23-2018, 01:14 AM
Post: #16
|
|||
|
|||
RE: (11C) Code guessing game
This program is based on Donald Knuth's paper The Computer as Master Mind.
It works only with 4 digits and the 6 colours 0-5. The following formula to count the total number of misses is used: \[ \max(n_1-n_1', 0)+\max(n_2-n_2', 0)+\ldots+\max(n_6-n_6', 0) \] From this value the number of "black hits" is subtracted to get the number of "white hits". Usage Generate Code [B] Check Test Pattern 1 ENTER 2 ENTER 3 ENTER 4 [A] Result 1.2 Code: LBL A Well, not half as fancy as Dieter's solution but maybe you find the approach still interesting. (04-28-2018 07:23 PM)pier4r Wrote: Did anyone create some "computer opponent" on calculators for the game? So an heuristic search for solutions. Not sure if that's what you are looking for but in the linked paper Donald Knuth proves that the codebreaker can always succeed in five moves or less. Cheers Thomas |
|||
07-23-2018, 03:22 PM
(This post was last modified: 07-24-2018 09:51 PM by SlideRule.)
Post: #17
|
|||
|
|||
RE: (11C) Code guessing game
(04-30-2018 07:29 PM)Rogier Wrote: … The minor problem is the double scoring of digits of a correct colour AND in the right place. That could easily be solved (multiply by 10 and subtract from the total of correct digits) … I don't check whether a colour has already been tested and with what kind of result … A modest suggestion: go ahead and score the color twice, irrespective of the correct position using the 1's digit BUT score the correct position(s) by adding nine to the score rather than ten. That is to say, score the colors first with sums of 1-digit in the one's column and then score the position second with sums of the 9-digit in the one's column. Four color example: a) 3 correct colors = 3 b) 1 correct position = 9 c) 3 + 9 = 12 d) 1 correct color in correct position, 2 correct colors in wrong position Make sense? Give it a go! [attachment=6155] example work flow BEST! SlideRule |
|||
05-31-2020, 07:43 PM
Post: #18
|
|||
|
|||
RE: (11C) Code guessing game
(07-23-2018 01:14 AM)Thomas Klemm Wrote: This program is based on Donald Knuth's paper The Computer as Master Mind. I read with interest all your interventions in all your posts for this thread. Thanks for the link, Thomas Klemm. So, I propose to everyone, the program I wrote for the HP11C. Be assured, this is one of my achievements that I did not do in one step. This program indeed required several phases spread over several calculators. One of my last versions, among the most successful, was intended for the HP12C and works without the use of the indirect addressing mode on the registers. I note with pleasure (and I reassure myself) by noting that my successive versions were indeed in accordance with the principles formulated in the document published by Donald E. Knuth. So, this version gives the possibility to choose the size of the code to find (4 or 5 digits, for example) and the number of distinct colors that can be used (from 1 to 9) to compose the code. Here is how to use it : On LBL A, you choose the number of digits in the code : 4 or 5 On LBL B, you give the number of colors : 6 or 9 On LBL C, you enter the number of authorized trials before the calculator reveals the code if you have not yet discovered it (usually 6 or 12, but personally I choose 99) Compose GSB D to prepare the code Key in 4 or 5 digits and press GSB E (or R/S) to examine a proposition The return format is c1c2c3c4c5.BW where c1c2c3c4c5 is your try, B the number of Black positions and W the number of White positions Code:
Example of use : 0.123 STO f RAN# GSB D --> 5.00 (see RCL 1 with 26829) 22689 R/S (or GSB E) --> 22689.23 22222 R/S --> 22222.20 26829 R/S --> 26829.50 x ↔ y --> 3.00 so you win in 3 tries !! |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)