'=========================================================================== ' Subject: TIC TAC TOE GAME Date: 10-31-99 (11:09) ' Author: Jeff Sweeting Code: QB, QBasic, PDS ' Origin: sweeting_j@hotmail.com Packet: GAMES.ABC '=========================================================================== ' tic tac toe ' this game is nearly unbeatable...so be happy if you do win.. ' Please send any comments, questions, or ways to make this program better to: ' sweeting_j@hotmail.com DECLARE SUB drawboard () CLS RANDOMIZE TIMER SCREEN 13 SLEEP 1 DIM spot(10) hwin = 0 pcwin = 0 CLS SLEEP 2 LOCATE 8, 10 COLOR 100 PRINT "THE UNBEATABLE" LOCATE 10, 10 COLOR 80 PRINT "TIC TAC TOE!" LOCATE 15, 8 PRINT "Press space to continue.." a$ = INKEY$ DO LOOP UNTIL INKEY$ = CHR$(32) CLS LOCATE 8, 4: PRINT "COMPUTER IS X YOU ARE O !" PRINT "" LOCATE 10, 4: PRINT "Press spacebar to continue..." a$ = INKEY$ DO LOOP UNTIL INKEY$ = CHR$(32) game: CLS COLOR 14 8002 a = INT(RND * 2) + 1 IF a >= 3 THEN GOTO 8002 IF a = 0 THEN GOTO 8002 808 n = INT(RND * 2) + 1 IF n >= 3 THEN GOTO 808 IF n = 0 THEN GOTO 808 IF n = a THEN LET m = 1 SLEEP 1 CLS b = 0 z = 0 pcmoves = 0 FOR i = 1 TO 9 LET spot(i) = 0 NEXT i COLOR 48 LOCATE 1, 5: PRINT "Your wins: "; hwin LOCATE 1, 25: PRINT "PC wins: "; pcwin top: CALL drawboard ' uses the sub drawboard to make the board and the num's COLOR 48 LOCATE 1, 5: PRINT "Your wins: "; hwin LOCATE 1, 25: PRINT "PC wins: "; pcwin IF m = 1 THEN GOTO player GOSUB checkforwinner drawletters: COLOR 48 LOCATE 1, 5: PRINT "Your wins: "; hwin LOCATE 1, 25: PRINT "PC wins: "; pcwin IF spot(1) = 1 THEN LINE (30, 135)-(85, 175), 12: LINE (30, 175)-(85, 135), 12 IF spot(1) = 2 THEN CIRCLE (55, 155), 25, 12 IF spot(2) = 1 THEN LINE (120, 135)-(175, 175), 12: LINE (120, 175)-(175, 135), 12 IF spot(2) = 2 THEN CIRCLE (145, 155), 25, 12 IF spot(3) = 1 THEN LINE (210, 135)-(265, 175), 12: LINE (210, 175)-(265, 135), 12 IF spot(3) = 2 THEN CIRCLE (240, 155), 25, 12 IF spot(4) = 1 THEN LINE (30, 75)-(85, 110), 12: LINE (30, 110)-(85, 75), 12 IF spot(4) = 2 THEN CIRCLE (55, 95), 25, 12 IF spot(5) = 1 THEN LINE (120, 75)-(175, 110), 12: LINE (120, 110)-(175, 75), 12 IF spot(5) = 2 THEN CIRCLE (145, 95), 25, 12 IF spot(6) = 1 THEN LINE (210, 75)-(265, 110), 12: LINE (210, 110)-(265, 75), 12 IF spot(6) = 2 THEN CIRCLE (240, 95), 25, 12 IF spot(7) = 1 THEN LINE (30, 20)-(85, 55), 12: LINE (30, 55)-(85, 20), 12 IF spot(7) = 2 THEN CIRCLE (55, 35), 25, 12 IF spot(8) = 1 THEN LINE (120, 20)-(175, 55), 12: LINE (120, 55)-(175, 20), 12 IF spot(8) = 2 THEN CIRCLE (145, 35), 25, 12 IF spot(9) = 1 THEN LINE (210, 20)-(265, 55), 12: LINE (210, 55)-(265, 20), 12 IF spot(9) = 2 THEN CIRCLE (240, 35), 25, 12 IF z = 1 THEN GOSUB checkforwinner GOTO player END IF pc: GOSUB checkforwinner LET r = r + 1 IF r >= 3 THEN GOTO offence offence: IF spot(1) = 1 AND spot(2) = 1 AND spot(3) = 0 THEN LET spot(3) = 1: GOTO putx IF spot(1) = 1 AND spot(2) = 0 AND spot(3) = 1 THEN LET spot(2) = 1: GOTO putx IF spot(1) = 0 AND spot(2) = 1 AND spot(3) = 1 THEN LET spot(1) = 1: GOTO putx IF spot(4) = 1 AND spot(5) = 1 AND spot(6) = 0 THEN LET spot(6) = 1: GOTO putx IF spot(4) = 1 AND spot(5) = 0 AND spot(6) = 1 THEN LET spot(5) = 1: GOTO putx IF spot(4) = 0 AND spot(5) = 1 AND spot(6) = 1 THEN LET spot(4) = 1: GOTO putx IF spot(7) = 1 AND spot(8) = 1 AND spot(9) = 0 THEN LET spot(9) = 1: GOTO putx IF spot(7) = 1 AND spot(8) = 0 AND spot(9) = 1 THEN LET spot(8) = 1: GOTO putx IF spot(7) = 0 AND spot(8) = 1 AND spot(9) = 1 THEN LET spot(7) = 1: GOTO putx IF spot(7) = 1 AND spot(4) = 1 AND spot(1) = 0 THEN LET spot(1) = 1: GOTO putx IF spot(7) = 1 AND spot(4) = 0 AND spot(1) = 1 THEN LET spot(4) = 1: GOTO putx IF spot(7) = 0 AND spot(4) = 1 AND spot(1) = 1 THEN LET spot(4) = 1: GOTO putx IF spot(8) = 1 AND spot(5) = 1 AND spot(2) = 0 THEN LET spot(2) = 1: GOTO putx IF spot(8) = 1 AND spot(5) = 0 AND spot(2) = 1 THEN LET spot(5) = 1: GOTO putx IF spot(8) = 0 AND spot(5) = 1 AND spot(2) = 1 THEN LET spot(8) = 1: GOTO putx IF spot(9) = 1 AND spot(6) = 1 AND spot(3) = 0 THEN LET spot(3) = 1: GOTO putx IF spot(9) = 1 AND spot(6) = 0 AND spot(3) = 1 THEN LET spot(6) = 1: GOTO putx IF spot(9) = 0 AND spot(6) = 1 AND spot(3) = 1 THEN LET spot(9) = 1: GOTO putx IF spot(1) = 1 AND spot(5) = 1 AND spot(9) = 0 THEN LET spot(9) = 1: GOTO putx IF spot(1) = 1 AND spot(5) = 0 AND spot(9) = 1 THEN LET spot(5) = 1: GOTO putx IF spot(1) = 0 AND spot(5) = 1 AND spot(9) = 1 THEN LET spot(1) = 1: GOTO putx IF spot(3) = 1 AND spot(5) = 1 AND spot(7) = 0 THEN LET spot(7) = 1: GOTO putx IF spot(3) = 1 AND spot(5) = 0 AND spot(7) = 1 THEN LET spot(5) = 1: GOTO putx IF spot(3) = 0 AND spot(5) = 1 AND spot(7) = 1 THEN LET spot(3) = 1: GOTO putx defence: IF spot(1) = 2 AND spot(2) = 2 AND spot(3) = 0 THEN LET spot(3) = 1: GOTO putx IF spot(1) = 2 AND spot(2) = 0 AND spot(3) = 2 THEN LET spot(2) = 1: GOTO putx IF spot(1) = 0 AND spot(2) = 2 AND spot(3) = 2 THEN LET spot(1) = 1: GOTO putx IF spot(4) = 2 AND spot(5) = 2 AND spot(6) = 0 THEN LET spot(6) = 1: GOTO putx IF spot(4) = 2 AND spot(5) = 0 AND spot(6) = 2 THEN LET spot(5) = 1: GOTO putx IF spot(4) = 0 AND spot(5) = 2 AND spot(6) = 2 THEN LET spot(4) = 1: GOTO putx IF spot(7) = 2 AND spot(8) = 2 AND spot(9) = 0 THEN LET spot(9) = 1: GOTO putx IF spot(7) = 2 AND spot(8) = 0 AND spot(9) = 2 THEN LET spot(8) = 1: GOTO putx IF spot(7) = 0 AND spot(8) = 2 AND spot(9) = 2 THEN LET spot(7) = 1: GOTO putx IF spot(7) = 2 AND spot(4) = 2 AND spot(1) = 0 THEN LET spot(1) = 1: GOTO putx IF spot(7) = 2 AND spot(4) = 0 AND spot(1) = 2 THEN LET spot(4) = 1: GOTO putx IF spot(7) = 0 AND spot(4) = 2 AND spot(1) = 2 THEN LET spot(4) = 1: GOTO putx IF spot(8) = 2 AND spot(5) = 2 AND spot(2) = 0 THEN LET spot(2) = 1: GOTO putx IF spot(8) = 2 AND spot(5) = 0 AND spot(2) = 2 THEN LET spot(5) = 1: GOTO putx IF spot(8) = 0 AND spot(5) = 2 AND spot(2) = 2 THEN LET spot(8) = 1: GOTO putx IF spot(9) = 2 AND spot(6) = 2 AND spot(3) = 0 THEN LET spot(3) = 1: GOTO putx IF spot(9) = 2 AND spot(6) = 0 AND spot(3) = 2 THEN LET spot(6) = 1: GOTO putx IF spot(9) = 0 AND spot(6) = 2 AND spot(3) = 2 THEN LET spot(9) = 1: GOTO putx IF spot(1) = 2 AND spot(5) = 2 AND spot(9) = 0 THEN LET spot(9) = 1: GOTO putx IF spot(1) = 2 AND spot(5) = 0 AND spot(9) = 2 THEN LET spot(5) = 1: GOTO putx IF spot(1) = 0 AND spot(5) = 2 AND spot(9) = 2 THEN LET spot(1) = 1: GOTO putx IF spot(3) = 2 AND spot(5) = 2 AND spot(7) = 0 THEN LET spot(7) = 1: GOTO putx IF spot(3) = 2 AND spot(5) = 0 AND spot(7) = 2 THEN LET spot(5) = 1: GOTO putx IF spot(3) = 0 AND spot(5) = 2 AND spot(7) = 2 THEN LET spot(3) = 1: GOTO putx i = INT(RND * 9) + 1 IF i >= 10 THEN GOTO pc IF i < 1 THEN GOTO pc IF spot(i) <> 0 THEN GOTO pc: LET spot(i) = 1 putx: LET z = 1 GOTO drawletters player: LOCATE 23, 5 INPUT "Enter the square number ", a IF a > 9 THEN GOTO player IF a < 1 THEN GOTO player IF spot(a) = 1 THEN GOTO player LET spot(a) = 2 CLS LET z = 0 LET m = 0 FOR i = 1 TO 15000: NEXT i CALL drawboard GOSUB drawletters GOSUB checkforwinner GOTO top LET r = 1 checkforwinner: IF spot(1) = 1 AND spot(2) = 1 AND spot(3) = 1 THEN GOTO pcwins IF spot(4) = 1 AND spot(5) = 1 AND spot(6) = 1 THEN GOTO pcwins IF spot(7) = 1 AND spot(8) = 1 AND spot(9) = 1 THEN GOTO pcwins IF spot(7) = 1 AND spot(4) = 1 AND spot(1) = 1 THEN GOTO pcwins IF spot(8) = 1 AND spot(5) = 1 AND spot(2) = 1 THEN GOTO pcwins IF spot(9) = 1 AND spot(6) = 1 AND spot(3) = 1 THEN GOTO pcwins IF spot(7) = 1 AND spot(5) = 1 AND spot(3) = 1 THEN GOTO pcwins IF spot(1) = 1 AND spot(5) = 1 AND spot(9) = 1 THEN GOTO pcwins IF spot(1) = 2 AND spot(2) = 2 AND spot(3) = 2 THEN GOTO humanwins IF spot(4) = 2 AND spot(5) = 2 AND spot(6) = 2 THEN GOTO humanwins IF spot(7) = 2 AND spot(8) = 2 AND spot(9) = 2 THEN GOTO humanwins IF spot(7) = 2 AND spot(4) = 2 AND spot(1) = 2 THEN GOTO humanwins IF spot(8) = 2 AND spot(5) = 2 AND spot(2) = 2 THEN GOTO humanwins IF spot(9) = 2 AND spot(6) = 2 AND spot(3) = 2 THEN GOTO humanwins IF spot(7) = 2 AND spot(5) = 2 AND spot(3) = 2 THEN GOTO humanwins IF spot(1) = 2 AND spot(5) = 2 AND spot(9) = 2 THEN GOTO humanwins IF spot(1) <> 0 AND spot(2) <> 0 AND spot(3) <> 0 AND spot(4) <> 0 AND spot(5) <> 0 AND spot(6) <> 0 AND spot(7) <> 0 AND spot(8) <> 0 AND spot(9) <> 0 THEN GOTO tiegame RETURN pcwins: SLEEP 1 CLS COLOR 45 LOCATE 12, 12 PRINT "THE COMPUTER WINS!!!" LET pcwin = pcwin + 1 PRINT "": COLOR 12 INPUT "Would you like to play again?", b$ IF b$ = "Y" OR b$ = "y" THEN CLS : GOTO game IF b$ = "N" OR b$ = "n" THEN END END humanwins: SLEEP 1 CLS COLOR 45 LOCATE 12, 12 LET hwin = hwin + 1 PRINT "YOU WIN!!!" PRINT "" COLOR 12 INPUT "Would you like to play again?", b$ IF b$ = "Y" OR b$ = "y" THEN CLS : GOTO game IF b$ = "N" OR b$ = "n" THEN END END tiegame: SLEEP 1 CLS COLOR 45 LOCATE 12, 12 PRINT "TIE GAME!" PRINT "": COLOR 12 INPUT "Would you like to play again?", b$ IF b$ = "Y" OR b$ = "y" THEN CLS : GOTO game IF b$ = "N" OR b$ = "n" THEN END END SUB cwin (w, x, y) END SUB SUB drawboard LINE (100, 10)-(100, 180), 14 LINE (190, 10)-(190, 180), 14 LINE (20, 65)-(275, 65), 14 LINE (20, 125)-(275, 125), 14 COLOR 20 LOCATE 5, 8: PRINT "7" LOCATE 5, 19: PRINT "8" LOCATE 5, 30: PRINT "9" LOCATE 12, 8: PRINT "4" LOCATE 12, 19: PRINT "5" LOCATE 12, 30: PRINT "6" LOCATE 20, 8: PRINT "1" LOCATE 20, 19: PRINT "2" LOCATE 20, 30: PRINT "3" END SUB