Can you guess the right code to win?
ZX Master Mind is December’s ZX81 program, a retro version of the classic Mastermind board game.
I used to play Mastermind when I was a kid, a simple code breaking game. One player would set up a sequence of colored pegs and the other would have to guess it in a set number of turns. My version, called ZX Master Mind, has the computer creating the code. Can you guess the three numbers in sequence before your ten turns run out?
For those unfamiliar with Mastermind, the goal is to guess the order and sequence of the code. In this case, the code is three non-repeating numbers. After each guess, the player, or computer, will display the number of correct digits and order. The number of guesses can vary. For ZX Master Mind, I chose ten as it fit on the screen while remaining difficult.
A fix for what’s broken.
I wrote the original version of ZX Master Mind in 1985. Yet, there were flaws in it making it hard to beat. In particular, the way it calculated the hints was wrong. Given this is a key piece of the game, it needed correcting.
In 1998, back when I was still maintaining my old ZX81 website, I rewrote the game to fix the flaws. This version also cleaned up some of the display routines and allowed you to replay it. Unreleased until now, it is this newer version that I present as December’s program of the month.
A coding we will go.
Digging into the code is unfair given this is a rewrite. That said, much of the original game is still intact. Other than some added text at the start and changing of the end graphics, the two look and play the same. Which is good as the game plays well.
Yet, inside the changes start to mount. The verification routines are quite different as is the flow of the program. It isn’t a complete rewrite, but the differences add up. But there is nothing new here. In fact, the original version added complexity where it wasn’t needed.
The meat of the game is the code checking routine captured in these nine lines of code.
190 IF A$(1)=A$(2) OR A$(1)=A$(
3) THEN LET A$(1)="-"
200 IF A$(2)=A$(3) THEN LET A$(
210 FOR G=1 TO 3
220 IF A$(G)="-" THEN GOTO 260
230 LET B=VAL A$(G)
240 IF B=A(G) THEN LET RP=RP+1
250 IF B=A(1) OR B=A(2) OR B=A(
3) THEN LET RN=RN+1
260 NEXT G
270 IF RP=3 THEN GOTO 350
Line 190 and 200 make sure you didn’t enter in the same number twice, which would through off the hints. If you do, it blanks out the number so it won’t count.
Then, the program runs a loop (line 210) to check each digit. 230 converts your guess from a string into a number. Then, line 240 determine if the number is in the right place. Line 250 checks if the number is correct, right place or not, using the ZX81’s boolean logic.
The last line, 270, will end the game if you guessed all three numbers. Otherwise, it displays your hints and loops until you get to 10 guesses and lose.
There is an easy strategy to follow to beat the game. You can see it in my test play and I’m not trying hard. Because of this, the game can too easy once you figure out how best to guess the numbers. I hardly ever lose which can make the game boring.
If I were to add to the game beyond fixing the flaws, I would have included a couple of changes. The first would be to vary the amount of guesses you get by asking at the beginning. With fewer guesses, the stress of guessing would go up forcing you to find a more efficient strategy.
The second change would be to allow more digits, again asking at the start or after breaking a previous code. Four digits gets to be pretty difficult, especially in 10 guesses. Although I enjoy playing the game, some variety would help. I don’t think the code changes would be too hard either. What do you think?
Well, that’s it for 2016 and my ZX81 program of the month. I already have a few programs lined up for 2017. Who knows, I could get around to finishing that adventure game this year.