(11C) Code guessing game
04-27-2018, 12:52 PM (This post was last modified: 04-30-2018 09:13 AM by Dieter.)
Post: #7
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
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. ;-)