Thanks to Julio Marchi for this space in MSX All
 

Mastermind



  Portugues

  This a code-breaker game, where the computer created a colored code sequence and the player tries to discover it. The earlier he/she finds it, the greater the score.


  1. Game rules
  The game has 8 different color pegs, where the computer creates a sequence of 4 differents colors. The player must discover the code by guessing the sequence.
  For each player guess, the computer analyze the player's sequence and reveal if:
  • There are pegs with right color and on the right place
  • There are pegs with the right color, but on the wrong place
  • There are pegs with wrong color

  Obs: the original game has 6 colored pegs and codes with repeated colors are not allowed. Thus, we may change some rules to let the game easier or harder by changing the number of colored pegs and allowing repeated colors on code.


  2. Game routines
  The color code sequence

  The following routine picks randomly 4 of 8 colored pegs.
10 DIM SE(4)
500 '
501 ' Create code
502 '
510 FOR F=1 TO 4
520 SE(F)=0:S=INT(8*RND(-TIME))+1
530 FOR P=1 TO F:IF SE(P)=S THEN 60 ELSE NEXT P
540 SE(F)=S
550 NEXT F
560 RETURN

  The array "SE" is created in order to save the computer color code.
  The line 520 picks a number between 1 and 8. In addition, the current array position is reseted. This is necessary once the line 530 sweeps the array "SE" from 1 to F trying to find if the selected number "S" aready exists. If SE(F) is a "garbage", this check may fail.
  If the value "S" does not exist, it is stored at "SE(F)".


  Checking the players's guess

  After the player write his guess, we have to compare both vectors "SE" and "JO". If we find right colors on the right place, we must include the value 2 on the analysis array (AN) for each occurence. Though, if we find the right color only, we must notify "AN" with the value 1. The value 0 indicates a wrong color.

  Routine 1
10 DIM SE(4):DIM JO(4):DIM AN(4)
600 '
601 ' Check play
602 '
610 FOR F=1 TO 4:AN(F)=0:NEXT F
620 P=1:FOR F=1 TO 4:FOR G=1 TO 4
630 IF JO(F)=SE(G) THEN AN(P)=-(F=G)+1:P=P+1:G=5
640 NEXT F,G
650 RETURN

  The line 610 clears the "AN" array for another analysis.
  The line 630 compares each player peg with all computer pegs. If the same color is found, an aditional test is performed to verify if the peg is on the rigth place: "-(F=G)+1". So, if F=G, the returned value is -1 and the expression result is 2. If F is not equal to G, the expression returned value is 1.
  The little "trick" applied on the line 630 allow us to test and store at the same time the values 1 or 2 for each right colored peg.
  Obs: If the player's peg has the same computer's peg color, we may interrupt the inner loop and go to the next player's peg. For that, we make "G=5" in order to interrupt the "G" FOR loop. This works as a "break" command for the Basic FOR.

  Note: the routine 1 is quite simple, but the returned pattern may help us on the code breaking task once there are some kind of extra implicit hints. For example, the following array "1,2,0,0" give us the following hint: "there is a peg with a right color on a right place and the peg before has the right color only".
  Once we grant the value 2 always before the value 1, this kind of problem is solved.

  Routine 2
600 '
601 ' Check play
602 '
610 FOR F=1 TO 4:AN(F)=0:NEXT F
620 P=1
630 FOR F=1 TO 4
640 IF SE(F)=JO(F) THEN AN(P)=2:P=P+1
650 NEXT F : IF P>4 THEN W=1:RETURN
660 FOR F=1 TO 4:FOR G=1 TO 4 
670 IF JO(F)=SE(G) AND F<>G THEN AN(P)=1:P=P+1:G=5
680 NEXT G,F
690 RETURN

  This routine checks for the right color and right place before checking the right color only situation. This ensure the value 2 always before 1 in "AN" array.
  The line 650 checks if the "AN" array was completed with 2's. In that case, the player broke the code and the game is over. The flag "W" returns 1.


  Decoding the player's command

  This routine aims at analyzing the player's output string array, by identifying the right values from 1 to 8, and copying the values to the "JO" array.
400 '
401 ' Decode play
402 '
410 F=1:IF LEN(JJ$)<>4 THEN F=0:RETURN
420 FOR P=1 TO 4
430 V=VAL(MID$(JJ$,P,1))
440 IF V<1 OR V>8 THEN F=0:RETURN
450 JO(P)=V
460 NEXT P
470 RETURN


  Printing the hints

  After analyzing the play, it is time to print the results:
 * - Right color + right place
 . - Right color only
 X - Wrong color
  Code:
300 '
301 ' Print analysis
302 '
310 FOR F=1 TO 4
320 IF AN(F)=2 THEN PRINT"*"; ELSE IF AN(F)=1 THEN PRINT"."; ELSE PRINT"X";
330 NEXT F:PRINT
340 RETURN


  3. The whole game
5 ' MarMSX 2018
10 DIM SE(4):DIM JO(4):DIM AN(4)
20 COLOR 15,1,1:SCREEN0:WIDTH 40:KEY OFF
30 PRINT"Mastermind game":PRINT"===============":PRINT
40 R=1:W=0:GOSUB 500
50 LINE INPUT"Type the sequence (1-8): ";JJ$
60 GOSUB 400:IF F=0 THEN 50
70 GOSUB 600:GOSUB 300
80 IF W=0 AND R=8 THEN PRINT"You did not found the code. Game over.":END
90 IF W=1 THEN PRINT"You found the right code in";R;"plays.":END
100 R=R+1:GOTO 50
300 '
301 ' Print analysis
302 '
310 FOR F=1 TO 4
320 IF AN(F)=2 THEN PRINT"*"; ELSE IF AN(F)=1 THEN PRINT"."; ELSE PRINT"X";
330 NEXT F:PRINT
340 RETURN
400 '
401 ' Decode play
402 '
410 F=1:IF LEN(JJ$)<>4 THEN F=0:RETURN
420 FOR P=1 TO 4
430 V=VAL(MID$(JJ$,P,1))
440 IF V<1 OR V>8 THEN F=0:RETURN
450 JO(P)=V
460 NEXT P
470 RETURN
500 '
501 ' Create code
502 '
510 FOR F=1 TO 4
520 SE(F)=0:S=INT(8*RND(-TIME))+1
530 FOR P=1 TO F:IF SE(P)=S THEN 60 ELSE NEXT P
540 SE(F)=S
550 NEXT F
560 RETURN
600 '
601 ' Check play
602 '
610 FOR F=1 TO 4:AN(F)=0:NEXT F
620 P=1
630 FOR F=1 TO 4
640 IF JO(F)=SE(F) THEN AN(P)=2:P=P+1
650 NEXT F:IF P>4 THEN W=1:RETURN
660 FOR F=1 TO 4:FOR G=1 TO 4 
670 IF SE(F)=JO(G) AND F<>G THEN AN(P)=1:P=P+1:G=5
680 NEXT G,F
690 RETURN


Marcelo Teixeira Silveira
Systems and Computing Engineer - UERJ
Computing Engeneer Master (M.Sc.) - UERJ

© MarMSX 1999-2018