'===========================================================================
' Subject: FOUR IN A ROW                     Date: Unknown Date (00:00:00)
'  Author: Creative Computing                Code: QB, QBasic             
'    Keys: FOUR,IN,ROW,CONNECT             Packet: GAMES.ABC
'===========================================================================
FUNCTION TAB(N AS INTEGER) AS STRING
  TAB = SPACE$(N)
END FUNCTION

RANDOMIZE TIMER

10 PRINT TAB(22); "FOUR IN A ROW"
20 PRINT TAB(20); "CREATIVE COMPUTING"
30 PRINT TAB(18); "MORRISTOWN, NEW JERSEY"
40 PRINT : PRINT : PRINT
100 DIM B$(8, 8) AS STRING, L(8) AS DOUBLE, S(4) AS DOUBLE, F(4) AS DOUBLE
110 DIM V(16) AS DOUBLE, N(4) AS DOUBLE
130 DATA 1,100,500,1E20,1,800,4000,1E20
140 DATA 1,75,900,1E18,1,450,3000,1E18
150 FOR Z1 = 1 TO 16: READ V(Z1): NEXT
160 PRINT "THE GAME OF FOUR IN A ROW"
170 INPUT "DO YOU WANT INSTRUCTIONS? "; A$
180 IF A$ = "NO" THEN: GOTO 270: END IF
190 IF A$ = "YES" THEN: GOTO 210: END IF
200 PRINT "YES OR NO": GOTO 170
210 PRINT "THE GAME CONSISTS OF STACKING X'S"
220 PRINT "AND O'S (THE COMPUTER HAS O) UNTIL"
230 PRINT "ONE OF THE PLAYER'S GETS FOUR IN A"
240 PRINT "ROW VERTICALLY, HORIZONTALLY, OR "
250 PRINT "DIAGONALLY."
260 PRINT : PRINT
270 X$ = "X": O$ = "O"
280 FOR I = 1 TO 8: FOR J = 1 TO 8: B$(I, J) = "-": NEXT J: NEXT I
290 FOR Z1 = 1 TO 8: L(Z1) = 0: NEXT Z1
300 INPUT "DO YOU WANT TO GO FIRST? "; A$
310 IF A$ = "NO" THEN: GOTO 610: END IF
320 GOSUB 340
330 GOTO 450
340 FOR I = 8 TO 1 STEP -1
350 FOR J = 1 TO 8
360 PRINT "  "; B$(I, J);
370 NEXT J
380 PRINT
390 NEXT I
400 PRINT "  ";
410 FOR I = 1 TO 8: PRINT I;"  "; : NEXT I
420 PRINT : PRINT
430 RETURN
440 PRINT "ILLEGAL MOVE, TRY AGAIN."
450 INPUT "A NUMBER BETWEEN 1 AND 8: "; M$
460 M = INT(VAL(M$))
470 IF M < 1 OR M > 8 THEN: GOTO 440: END IF
480 LL = L(M)
490 IF LL > 7 THEN: GOTO 440: END IF
500 L(M) = LL + 1: LL = LL + 1
510 B$(LL, M) = X$
520 PRINT
530 GOSUB 340
540 P$ = X$
550 GOSUB 1240
560 FOR Z = 1 TO 4
570 IF S(Z) < 4 THEN: GOTO 600: END IF
580 PRINT "Y O U   W I N !!!"
590 GOTO ENDGAME
600 NEXT Z
610 M9 = 0: V1 = 0
620 N1 = 1
630 FOR M4 = 1 TO 8
640 LL = L(M4) + 1
650 IF LL > 8 THEN: GOTO 1080: END IF
660 VV = 1
670 P$ = O$: W = 0
680 M = M4
690 GOSUB 1240
700 FOR Z1 = 1 TO 4: N(Z1) = 0: NEXT Z1
710 FOR Z = 1 TO 4
720 SS = S(Z)
730 IF SS - W > 3 THEN: GOTO 1130: END IF
740 T = SS + F(Z)
750 IF T < 4 THEN: GOTO 780: END IF
760 VV = VV + 4
770 N(SS) = N(SS) + 1
780 NEXT Z
790 FOR I = 1 TO 4
800 NN = N(I) - 1
810 IF NN = -1 THEN: GOTO 840: END IF
820 I1 = 8 * W + 4 * SGN(NN) + I
830 VV = VV + V(I1) + NN * V(8 * W + I)
840 NEXT I
850 IF W = 1 THEN: GOTO 880: END IF
860 W = 1: P$ = X$
870 GOTO 690
880 LL = LL + 1
920 IF LL > 8 THEN: GOTO 1020: END IF
930 GOSUB 1240
940 FOR Z = 1 TO 4
950 IF S(Z) > 3 THEN: VV = 2: END IF
960 NEXT Z
1020 IF VV < V1 THEN: GOTO 1080: END IF
1030 IF VV > V1 THEN: N1 = 1: GOTO 1060: END IF
1040 N1 = N1 + 1
1050 IF RND(1) > 1 / N1 THEN: GOTO 1080: END IF
1060 V1 = VV
1070 M9 = M4
1080 NEXT M4
1090 IF M9 <> 0 THEN: GOTO 1120: END IF
1100 PRINT "T I E   G A M E ..."
1110 GOTO ENDGAME
1120 M = M9
1130 PRINT "COMPUTER PICKS COLUMN "; M: PRINT
1140 LL = L(M) + 1: L(M) = L(M) + 1
1150 B$(LL, M) = O$
1160 P$ = O$: GOSUB 340
1170 GOSUB 1240
1180 FOR Z = 1 TO 4
1190 IF S(Z) < 4 THEN: GOTO 1220: END IF
1200 PRINT "C O M P U T E R   W I N S !!!"
1210 GOTO ENDGAME
1220 NEXT Z
1230 GOTO 450
1240 Q$ = X$
1250 IF P$ = X$ THEN: Q$ = O$: END IF
1260 D2 = 1: D1 = 0
1270 Z = 0
1280 GOSUB 1360
1290 D1 = 1: D2 = 1
1300 GOSUB 1360
1310 D2 = 0: D1 = 1
1320 GOSUB 1360
1330 D2 = -1: D1 = 1
1340 GOSUB 1360
1350 RETURN
1360 D = 1: SS = 1
1370 T = 0
1380 Z = Z + 1
1390 C = 0
1400 FOR K = 1 TO 3
1410 M5 = M + K * D1: L1 = LL + K * D2
1420 IF M5 < 1 OR L1 < 1 OR M5 > 8 OR L1 > 8 THEN: GOTO 1510: END IF
1430 BB$ = B$(L1, M5)
1440 IF C = 0 THEN: GOTO 1480: END IF
1450 IF BB$ = Q$ THEN: K = 3: GOTO 1510: END IF
1460 T = T + 1
1470 GOTO 1510
1480 IF BB$ = P$ THEN: SS = SS + 1: GOTO 1510: END IF
1490 C = 1
1500 GOTO 1450
1510 NEXT K
1520 IF D = 0 THEN: GOTO 1550: END IF
1530 D = 0: D1 = -D1: D2 = -D2
1540 GOTO 1390
1550 S(Z) = SS
1560 F(Z) = T
1570 RETURN

ENDGAME:

