10 KEY OFF:WIDTH 80:CLS:DEFINT A-Z:GOSUB 59980:KEY(10) ON:ON KEY(10) GOSUB 2700
20 GOSUB 500:DIM GT(15,6),PO(6,4)
30 LOCATE 12,1:INPUT "What is your first name";NA$
40 LOCATE 13,1:PRINT"How many games do you wish to play, "NA$;:INPUT NG
50 IF NG<1 THEN LOCATE 13,1:PRINT SPACE$(80):GOTO 40
60 LOCATE 12,1:PRINT SPACE$(80):LOCATE 13,1:PRINT SPACE$(80)
70 LOCATE 12,1:PRINT"I will now toss a coin to determine the order of play."
75 COLOR 15:PRINT"     HEADS";:COLOR 7:PRINT" - you're the codemaker"
78 COLOR 15:PRINT"     TAILS";:COLOR 7:PRINT" - you're the codebreaker"
80 TIMEOUT=5:GOSUB 59950
90 CT=INT(RND*2)+1:IF CT<>1 AND CT<>2 THEN 90
100 IF CT=1 THEN M1$="heads":M2$="codemaker" ELSE M1$="tails":M2$="codebreaker"
120 PRINT"The coin came out " M1$ " so you will be the " M2$ "."
130 TIMEOUT=5:GOSUB 59950
140 GC=0:PC=0:PP=0
150 IF CT=1 THEN GOSUB 1000 ELSE GOSUB 2000
160 TIMEOUT=5:GOSUB 59950:IF CT=1 THEN GOSUB 2000 ELSE GOSUB 1000
170 IF GC=0 THEN GA$="game" ELSE GA$="games"
180 TIMEOUT=5:GOSUB 59950:CLS:GOSUB 500:LOCATE 12,10:PRINT"After" GC+1 GA$ " the score is:"
190 LOCATE 14,38-LEN(NA$):PRINT NA$ ": " PP
200 LOCATE 15,30:PRINT"computer: " PC
210 TIMEOUT=5:GOSUB 59950
220 GC=GC+1
230 IF GCPP THEN M1$="  I won"
260 IF PC0 THEN 1030
1070 LOCATE 3,9:PRINT STRING$(64,"?");:LOCATE 4,9:PRINT STRING$(64,"?");
1080 LOCATE 3,30:PRINT" CODE =" T(1) T(2) T(3) T(4) " ";
1090 FOR R=1 TO 6:FOR C=1 TO 4:PO(R,C)=R:NEXT C,R
1100 C1=1:C2=1:C3=1:C4=0:GU=0
1110 GU=GU+1
1120 IF GU=1 THEN FOR I=1 TO 4:C(I)=INT(RND*6)+1:NEXT I
1130 IF GU=2 THEN C(1)=C1:C(2)=C2:C(3)=C3:C(4)=C4
1140 IF GU>2 THEN FOR I=1 TO 4:C(I)=GT(GU-1,I):NEXT I
1150 IF GU>1 THEN GOSUB 3500
1160 TR$=""
1170 FOR I=1 TO 4:TR$=TR$+RIGHT$(STR$(C(I)),1):NEXT I
1180 BEEP:LOCATE 20,25:PRINT"Please evaluate try #" GU "- " TR$
1190 LOCATE 21,33:PRINT"Black pegs = ";
1200 GOSUB 59990:IF INSTR("01234",IKEY$)=0 THEN 1200 ELSE PRINT IKEY$:BB=VAL(IKEY$)
1210 LOCATE 22,33:PRINT"White pegs = ";
1220 GOSUB 59990:IF INSTR("01234",IKEY$)=0 THEN 1220 ELSE PRINT IKEY$:WW=VAL(IKEY$)
1230 FOR I=1 TO 4:GT(GU,I)=C(I):NEXT I:GT(GU,5)=BB:GT(GU,6)=WW
1240 NU$=CO$:GOSUB 3000
1250 FOR I=1 TO 4:C(I)=T(I):NEXT I
1260 NU$=TR$:GOSUB 3000
1270 GOSUB 4000 'evaluate guess
1280 FOR I=1 TO 4:T(I)=C(I):NEXT I
1290 IF BB=B AND WW=W THEN 1320
1300 LOCATE 23,18:PRINT"Your evaluation should have been B =" B " W =" W
1310 GT(GU,5)=B:GT(GU,6)=W:TIMEOUT=2:GOSUB 59950
1320 LOCATE YY,XX:PRINT USING" ## = \  \ - B = # W = #";GU,TR$,GT(GU,5),GT(GU,6);
1330 IF XX=43 THEN YY=YY+1:XX=12 ELSE XX=43
1340 FOR I=20 TO 23:LOCATE I,1:PRINT SPACE$(80);:NEXT I
1350 IF B=4 THEN PLAY "mbc16c16c16ge8g":PP=PP+GU:LOCATE 20,34:PRINT"I got it !!!":RETURN
1360 IF B+W=4 THEN GOSUB 5000
1370 IF B+W=0 THEN GOSUB 6000
1380 IF B=0 AND W<>0 THEN GOSUB 7000
1390 LOCATE 20,30:PRINT"Quiet!  I'm thinking."
1400 GOTO 1110
2000 CLS:GOSUB 500:YY=5:XX=12:ON KEY(10) GOSUB 2800
2010 FOR Y=2 TO 18:LOCATE Y,9:PRINT"?";:LOCATE Y,39:PRINT"??";:LOCATE Y,73:PRINT"?";:NEXT Y
2015 LOCATE 2,9:PRINT STRING$(64,"?");:LOCATE 18,9:PRINT STRING$(64,"?");
2020 LOCATE 20,10:PRINT"Now it's my turn to make a code.":TIMEOUT=5:GOSUB 59950
2030 LOCATE 20,1:PRINT SPACE$(80):LOCATE 20,10:PRINT"O.K.  I've got it now.  Take your first guess."
2040 NO=0:FOR I=1 TO 4:C(I)=INT(RND*6)+1:NEXT I:TIMEOUT=3:GOSUB 59950
2050 LOCATE 20,1:PRINT SPACE$(80):LOCATE 20,10:PRINT"Enter your guess (4 digits) - ";
2060 INPUT TR$:IF LEN(TR$)<>4 THEN 2050
2070 NU$=TR$:GOSUB 3000
2080 IF ER<>0 THEN 2050
2090 GOSUB 4000 'evaluate guess
2100 NO=NO+1:LOCATE YY,XX:PRINT USING" ## = \  \ - B = # W = #";NO,TR$,B,W;
2110 IF XX=43 THEN YY=YY+1:XX=12 ELSE XX=43
2120 FOR I=20 TO 23:LOCATE I,1:PRINT SPACE$(80);:NEXT I
2130 IF B=4 THEN PLAY "mbc16c16c16ge8g":PC=PC+NO:LOCATE 20,33:PRINT"You got it !!!":ON KEY(10) GOSUB 2700:RETURN ELSE 2050
2700 RUN "menu"
2800 LOCATE 25,10:PRINT"The code was "C(1) C(2) C(3) C(4);:TIMEOUT=5:GOSUB 59950
2810 GOTO 2700
3000 ER=0
3010 FOR I=1 TO 4:T(I)=VAL(MID$(NU$,I,1)):IF T(I)<1 OR T(I)>6 THEN ER=1
3020 NEXT I:RETURN
3500 GOSUB 3700 'get next entry from possibility table
3510 FOR L=1 TO GU-1:FOR J=1 TO 4:T(J)=GT(L,J):NEXT J
3520 GOSUB 4000 'evaluate old guess using new guess as code
3530 IF GT(L,5)=B AND GT(L,6)=W THEN NEXT L:RETURN ELSE 3500
3700 C4=C4+1:IF C4>=7 THEN C4=0:GOTO 3720
3710 IF PO(C4,4)=0 THEN 3700 ELSE C(4)=PO(C4,4):RETURN
3720 C3=C3+1:IF C3>=7 THEN C3=1:GOTO 3740
3730 IF PO(C3,3)<>0 THEN C(3)=PO(C3,3):GOTO 3700 ELSE 3720
3740 C2=C2+1:IF C2>=7 THEN C2=1:GOTO 3760
3750 IF PO(C2,2)<>0 THEN C(2)=PO(C2,2):GOTO 3730 ELSE 3740
3760 C1=C1+1:IF C1>=7 THEN PRINT"Error in probability table.":END
3770 IF PO(C1,1)<>0 THEN C(1)=PO(C1,1):GOTO 3750 ELSE 3760
4000 B=0:W=0:FOR I=1 TO 4:W(I)=C(I):NEXT I:FOR I=1 TO 4
4010 IF W(I)=T(I) THEN W(I)=-1:T(I)=-2:B=B+1
4020 NEXT I:FOR I=1 TO 4:FOR J=1 TO 4
4030 IF T(I)=W(J) THEN W(J)=-1:W=W+1:GOTO 4050
4040 NEXT J
4050 NEXT I:RETURN
5000 FOR I=1 TO 4:EL(I)=I:NEXT I:FOR I=1 TO 4:EL(GT(GU,I))=0:NEXT I
5010 FOR I=1 TO 6:IF EL(I)<>0 THEN PO(I,1)=0:PO(I,2)=0:PO(I,3)=0:PO(I,4)=0
5020 NEXT I:RETURN
6000 FOR I=1 TO 4:FOR J=1 TO 4:PO(GT(GU,I),J)=0:NEXT J,I:RETURN
7000 FOR I=1 TO 4:PO(GT(GU,I),I)=0:NEXT I:RETURN
59950 TIMEOUT$=TIME$:TIME2=VAL(LEFT$(TIMEOUT$,2))*120+VAL(MID$(TIMEOUT$,4,2))*60+VAL(RIGHT$(TIMEOUT$,2))
59960 TIMEOUT$=TIME$:TIME3=VAL(LEFT$(TIMEOUT$,2))*120+VAL(MID$(TIMEOUT$,4,2))*60+VAL(RIGHT$(TIMEOUT$,2))
59970 IF TIMEOUT > TIME3 - TIME2 THEN 59960 ELSE RETURN
59980 RNDTIME$=TIME$:RNDVAL=VAL(LEFT$(RNDTIME$,2))*120+VAL(MID$(RNDTIME$,4,2))*60+VAL(RIGHT$(RNDTIME$,2)):RANDOMIZE RNDVAL:RETURN
59990 IKEY$=INKEY$:IF IKEY$="" THEN 59990 ELSE RETURN