Mastermind

See all articles from SYNC v1 n3

This game is played much like the original board version. The computer selects a code of four colors from a possible six: red, orange, yellow, green, blue, and white (duplicates allowed). It is now up to you to find, in nine moves or less, the exact color and position of each element of the code by entering four colors of your choice, first letters only, one at a time. The computer then compares the guess with the pre-selected code, first for black pegs and then for white pegs. The pegs are displayed next to the corresponding guess. A black peg means a right color in the right position, a white peg means a right color in the wrong position.

For example, suppose the hidden code was R B W B, and the first guess was R G B O. You would be given one black peg and one white peg because ‘red’ is in the right position and ‘blue’ is correct but in the wrong position.

These features make this program superior to many other versions of this game.

The game continues until 1) the code is broken and “uncovered,” 2) “Q” is entered, signifying ‘quit,’ or 3) all nine tries are used.

Conversion

This program is designed for a 2K system, but can be reduced to 1K by doing the following:

Delete lines 10-140, 250-320, 720-770.

Change lines:

330 PRINT "ROYGBW   " (3 sp.)
480 POKE C+214+2*E,B(E)
560 POKE C+203+E,9
660 POKE C+203+E,128
800 POKE C+D*20+2*B+11,0
810 POKE C+D*20+2*B+12,A(B)
830 POKE C+D*20+19,0
840 STOP
5 RANDOMIZE
10 PRINT ," MASTERMIND" (After " leave 2 sp.)
20 PRINT
30 PRINT " I WILL SELECT A CODE OF FOUR" (2 sp.)
40 PRINT "COLORS AND YOU MUST TRY TO BREAK IT USING THE CLUES I GIVE YOU."
50 PRINT
60 PRINT " A BLACK PEG MEANS A RIGHT" (3 sp.)
70 PRINT " COLOR IN THE RIGHT POSITION." (1 sp.)
75 PRINT
80 PRINT " A WHITE PEG MEANS A RIGHT" (2 sp.)
85 PRINT " COLOR IN THE WRONG POSITION." (1 sp.)
90 PRINT
100 PRINT " YOU HAVE 9 TRIES." (6 sp.)
110 PRINT
115 PRINT " INPUT Q TO QUIT AND/OR"," P TO PLAY AGAIN." (4 sp.and 7 sp.)
120 PRINT
125 PRINT " TYPE IN P TO PLAY." (6 sp.)
130 INPUT A$
135 IF NOT A$="P" THEN STOP
140 CLEAR
150 DIM A(3)
160 DIM B(3)
170 DIM C(3)
180 FOR B=0 TO 3
190 LET A(B)=RND(6)
200 IF A(B)=1 OR A(B)=2 OR A(B)=3 THEN LET A(B)=A(B)*8+36
210 IF A(B)=4 THEN LET A(B)=39
220 IF A(B)=5 OR A(B)=6 THEN LET A(B)=A(B)*7+20
230 NEXT B (Generate code)
250 CLS
300 PRINT "B = BLUE Y = YELLOW O = ORANGER = RED G = GREEN W = WHITE
310 PRINT " ▒ = BLACK PEG",CHR$(128);" = WHITE PEG
320 PRINT (▒ Shift A)
330 PRINT ," ";
340 FOR F=1 TO 11
360 PRINT CHR$(128);
370 NEXT F
380 FOR C=1 TO 9
390 PRINT
400 PRINT
410 PRINT 10-C,," ";
420 NEXT C
430 FOR D=1 TO 9
440 FOR E=0 TO 3
450 INPUT A$
460 LET B(E)=CODE(A$)
470 GO SUB 900
480 POKE C+310+2*E,B(E)
490 IF A$="Q" THEN GO TO 720
500 LET C(E)=A(E)
510 NEXT E
520 LET E=0
530 FOR B=0 TO 3
540 IF NOT B(B)=C(B) THEN GO TO 590 (Check for black peg)
550 LET E=E+2
560 POKE C+299+E,9
570 LET B(B)=0 (Destroy matching pairs)
580 LET C(B)=1
590 NEXT B
600 IF E=8 THEN GO TO 780
610 FOR B=0 TO 3
620 FOR F=0 TO 3
630 IF C(B)<2 THEN GO TO 700
640 IF NOT C(B)=B(F) THEN GO TO 690
650 LET E=E+2 (Check for white pegs)
660 POKE C+299+E,128
670 LET B(F)=0
680 GO TO 700
690 NEXT F
700 NEXT B
710 NEXT D
720 LET A$=" TOO BAD"
730 FOR B=97 TO 103
740 LET A$=TL$(A$)
750 GO SUB 900
760 POKE B+C+D*20,CODE(A$)
770 NEXT B
780 GO SUB 900
790 FOR B=0 TO 3
800 POKE C+D*20+2*B+107,0
810 POKE C+D*20+2*B+108,A(B)
820 NEXT B
830 POKE C+D*20+115,0
840 GO TO 130
900 LET C=PEEK(16396)+PEEK(16397)*256-D*20
910 RETURN
[Instructions]

Sample Runs (2K)

MASTERMIND

I WILL SELECT A CODE OF FOUR
COLORS AND YOU MUST TRY TO
BREAK IT USING THE CLUES I
GIVE YOU.

A BLACK PEG MEANS A RIGHT
COLOR IN THE RIGHT POSITION.

A WHITE PEG MEANS A RIGHT
COLOR IN THE WRONG POSITION.

YOU HAVE 9 TRIES.

INPUT Q TO QUIT AND/OR
P TO PLAY AGAIN.

TYPE IN P TO PLAY.


B = BLUE Y = YELLOW O = ORANGE
R = RED G = GREEN W = WHITE
■ = BLACK PEG  □ = WHITE PEG

         Y R B W
     [Code is only "uncovered" after it is
     guessed, all turns are used, or a quit]

9
8
7
6 ■ ■ ■ ■   Y R B W    [Correct answer]
5 ■ □       Y Y G R
4 ■ ■       Y Y O W
3 ■         G W G W    ■ is chr 9
2 ■         O R O R    □ is chr 128
1 □ □       B B Y Y
"P"


B = BLUE Y = YELLOW O = ORANGE
R = RED G = GREEN W = WHITE
■ = BLACK PEG  □ = WHITE PEG

TOO BAD □ R B O G □

9
8
7               Q            [Quit]
6 ■ □ □ □   B G O R
5 □ □ □ □   B R G O
4 ■ □ □ □   B O R G
3 ■ ■       R R R G
2 ■ □       O O O B
1 ■         B B B Y
9:135


Sample Runs (1K)

ROYGBW □  G R G Y □
     [Code is only "uncovered" after it is
     guessed, a quit, or all turns are used]

9
8
7
6 ■ ■ ■ ■   G R G Y    [Correct answer]
5 ■ □       G W B R
4 ■ ■       G W O Y
3 ■         Y B B Y
2 □         R O R O
1 ■ □       G G W W
9:840
Cartoon — "It says the odds of you making that hand are 2,385,000 to 1, and the odds are 3 to 2 that a nut like you will try for it."
Cartoon — "It says the odds of you making that hand are 2,385,000 to 1, and the odds are 3 to 2 that a nut like you will try for it."

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.