home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 December
/
simtel1292_SIMTEL_1292_Walnut_Creek.iso
/
msdos
/
info
/
dostrain.arc
/
TTT.BAS
< prev
next >
Wrap
BASIC Source File
|
1985-05-01
|
11KB
|
299 lines
10 REM TTT
20 REM This program plays tic-tac-toe with the user.
30 REM
40 REM Variable Explanation
50 REM BDROW upper left row of the board
60 REM BDCOL upper left col of the board
70 REM SCROW top row of scorecard display
80 REM INTROW interaction area row
90 DIM BR (9)' row coordinates of centers of squares for display
100 DIM BC (9)' column coordinates of centers of squares for display
110 REM BD1$, BD2$ constants used in drawing the board
120 REM PLAYERNM$ the player's name
130 REM MAXNML maximum name length of "The cat (ties)" or PLAYERNM$
140 REM PLS$ player's symbol
150 REM CPS$ computer's symbol
160 REM PW the number of games the player has won so far
170 REM CW the number of games the computer has won so far
180 REM CATW the number of cat's games (ties) so far
190 DIM BOARD (9)' current tic-tac-toe board, with values as follow:
200 REM 0 = untaken square
210 REM 1 = player has the square
220 REM -1 = computer has the square
230 REM PLAYS the number of plays so far this game
240 REM TURN switch: if 0, computer is next, if 1, the player
250 REM WINNER the winner of the current game:
260 REM 0 = no winner so far
270 REM 1 = the player won
280 REM -1 = the computer won
290 REM ERRTYPE error types for players move
300 REM 1 = choice out of 1-9 range
310 REM 2 = square already taken
320 REM MORE 0 if the user is ready to quit, 1 for a new game
330 REM
500 REM Main program
510 CLS
520 GOSUB 1000' initialize screen format parameters
530 GOSUB 1500' ask user for options
540 GOSUB 2000' display rules, if requested
550 GOSUB 2500' display skeleton board
560 GOSUB 3000' display scoreboard
570 GOSUB 3500' clear the internal board
580 GOSUB 4000' display cleared board
590 IF TURN = 0 THEN GOSUB 4500: TURN = 1 ELSE GOSUB 5000: TURN = 0
600 GOSUB 4000' display board
610 GOSUB 5500' check for a winner
620 IF PLAYS < 9 AND WINNER = 0 THEN GOTO 590' no winner yet
630 GOSUB 6000' display winner's name and update score
640 GOSUB 6500' display the score
650 GOSUB 7000' check for more games
660 IF MORE = 1 THEN GOTO 570
670 GOSUB 7500' display ending message
680 STOP
690 REM
1000 REM Initialize the screen format parameters.
1010 REM
1020 REM Outputs: BDROW, BDCOL, SCROW, INTROW, BR(), and BC()
1030 REM
1040 BDROW = 2: BDCOL = 45
1050 SCROW = 2: INTROW = 18
1060 BR(1)=1: BR(2)=1: BR(3)=1: BR(4)=5: BR(5)=5: BR(6)=5: BR(7)=9: BR(8)=9: BR(9)=9
1070 BC(1)=1: BC(2)=9: BC(3)=17: BC(4)=1: BC(5)=9: BC(6)=17: BC(7)=1: BC(8)=9: BC(9)=17
1080 FOR I = 1 TO 9
1090 BR(I) = BR(I) + BDROW
1100 BC(I) = BC(I) + BDCOL + 5
1110 NEXT I
1120 RETURN
1130 REM
1500 REM Input the user-selected options.
1510 REM
1520 REM OUTPUTS: PLAYERNM$, MAXNML, TURN, CPS$, AND PLS$
1530 REM
1540 INPUT "What's your name? ", PLAYERNM$
1550 PLAYERNM$ = LEFT$ (PLAYERNM$, 76 - BDCOL)
1560 MAXNML = LEN (PLAYERNM$) + 4
1570 IF MAXNML < 14 THEN MAXNML = 14
1580 PRINT
1590 INPUT "Do you want to go first? ", TEMP$
1600 IF LEFT$(TEMP$,1) = "n" OR LEFT$(TEMP$,1) = "N" THEN TURN = 0 ELSE TURN =1
1610 PRINT
1620 IF TURN = 0 THEN PRINT "Okay, I'll take X, you've got O." ELSE PRINT "Okay, I'll take O and you have X."
1630 IF TURN = 0 THEN CPS$ = "X": PLS$ = "O" ELSE CPS$ = "O": PLS$ = "X"
1640 PRINT
1650 RETURN
1660 REM
2000 REM Display the rules of the game, if requested by the user.
2010 REM
2020 INPUT "Would you like to see the rules? ", TEMP$
2030 CLS
2040 IF LEFT$ (TEMP$, 1) = "n" OR LEFT$ (TEMP$, 1) = "N" THEN RETURN
2050 PRINT
2060 PRINT " The idea of the game is to get three squares in a horizontal,"
2070 PRINT "vertical or diagonal row. The board is layed out like this:"
2080 PRINT
2090 PRINT TAB(25) "1 : 2 : 3"
2100 PRINT TAB(25) "--:---:--"
2110 PRINT TAB(25) "4 : 5 : 6"
2120 PRINT TAB(25) "--:---:--"
2130 PRINT TAB(25) "7 : 8 : 9"
2140 PRINT
2150 PRINT "When the computer asks for your move, just enter the number of"
2160 PRINT "the square you want to take."
2170 PRINT
2180 INPUT "Hit ENTER when you have finished reading the rules.", TEMP$
2190 CLS
2200 RETURN
2210 REM
2500 REM Display the board outline.
2510 REM
2520 REM Inputs are BDROW and BDCOL.
2530 REM
2540 REM Variable Explanation
2550 REM BD1$ first line used in drawing the board
2560 REM BD2$ second line used in drawing the board
2570 REM
2580 BD1$ = " : :"
2590 BD2$ = " --------+-------+-------"
2600 FOR I = 0 TO 10
2610 LOCATE BDROW + I, BDCOL
2620 IF I = 3 OR I = 7 THEN PRINT BD2$ ELSE PRINT BD1$
2630 NEXT I
2640 RETURN
2650 REM
3000 REM Display the scoreboard.
3010 REM
3020 REM Inputs are SCROW and MAXNML
3030 REM
3040 LOCATE SCROW, 1
3050 TEMP = (MAXNML +10) / 2 - 5
3060 PRINT TAB(TEMP); "Scoreboard"
3070 PRINT
3080 PRINT "Player"; TAB(MAXNML + 4) + "Games"
3090 PRINT TAB(MAXNML + 5) + "Won"
3100 PRINT
3110 PRINT PLAYERNM$ + " (" + PLS$ + ")"
3120 PRINT "Computer (" + CPS$ + ")"
3130 PRINT "The cat (ties)"
3140 RETURN
3150 REM
3500 REM Clear board and reset the number of plays this game
3510 REM
3520 REM Outputs: BOARD() AND PLAYS
3530 REM
3540 PLAYS = 0
3550 FOR I=1 TO 9
3560 BOARD (I) = 0
3570 NEXT I
3580 RETURN
3590 REM
4000 REM Display the current board.
4010 REM
4020 REM Inputs are BOARD(), BR(), BC(), PLS$, and CPS$
4030 REM
4040 FOR I = 1 TO 9
4050 LOCATE BR (I), BC (I)
4060 IF BOARD (I) = 1 THEN PRINT PLS$;
4070 IF BOARD (I) = 0 THEN PRINT " ";
4080 IF BOARD (I) = -1 THEN PRINT CPS$;
4090 NEXT I
4100 RETURN
4110 REM
4500 REM The computer chooses its next move.
4510 REM
4520 REM Inputs are BOARD() and PLAYS.
4530 REM Outputs are BOARD() and PLAYS.
4540 REM
4550 PLAYS = PLAYS + 1' count the turn
4560 IF BOARD (5) = 0 THEN BOARD (5) = -1: RETURN' try the center
4570 FOR I = 1 TO 9 STEP 2' try a corner
4580 IF BOARD (I) = 0 THEN BOARD (I) = -1: RETURN
4590 NEXT I
4600 FOR I = 2 TO 8 STEP 2' take a side
4610 IF BOARD (I) = 0 THEN BOARD (I) = -1: RETURN
4620 NEXT I
4630 REM
5000 REM This subroutine gets a move from the player.
5010 REM It does not return until a valid move is entered.
5020 REM
5030 REM Inputs are INTROW, PLAYS, and BOARD().
5040 REM Outputs are PLAYS and BOARD().
5050 REM
5060 REM Variable Explanation
5070 REM ERRTYPE type of user error
5080 REM 1 = selection out of 1-9 range
5090 REM 2 = tried to take an occupied square
5100 REM
5110 PLAYS = PLAYS + 1' count the turn
5120 LOCATE INTROW, 1
5130 GOSUB 8500' clear the current display line
5140 INPUT "Which square do you want? ", TEMP$
5150 TEMP = VAL (TEMP$)
5160 IF TEMP < 1 OR TEMP > 9 THEN ERRTYPE = 1: GOSUB 8000: GOTO 5120
5170 IF BOARD (TEMP) <> 0 THEN ERRTYPE = 2: GOSUB 8000: GOTO 5120
5180 BOARD (TEMP) = 1
5190 GOSUB 9000' clear the interaction area
5200 RETURN
5210 REM
5500 REM Check to see if someone has won. If so WINNER
5510 REM is set and the appropriate win count is incremented.
5520 REM
5530 REM Input is BOARD()
5540 REM Output is WINNER
5550 REM
5560 REM check for a row winner
5570 FOR I = 1 TO 7 STEP 3
5580 TEMP = BOARD (I) + BOARD (I+1) + BOARD (I+2)
5590 IF TEMP = 3 THEN WINNER = 1: RETURN
5600 IF TEMP = -3 THEN WINNER = -1: RETURN
5610 NEXT I
5620 REM check for a column winner
5630 FOR I = 1 TO 3
5640 TEMP = BOARD (I) + BOARD (I+3) + BOARD (I+6)
5650 IF TEMP = 3 THEN WINNER = 1: RETURN
5660 IF TEMP = -3 THEN WINNER = -1: RETURN
5670 NEXT I
5680 REM check for diagonal winner
5690 TEMP = BOARD (3) + BOARD (5) + BOARD (7)
5700 IF TEMP = 3 THEN WINNER = 1: RETURN
5710 IF TEMP = -3 THEN WINNER = -1: RETURN
5720 TEMP = BOARD (1) + BOARD (5) + BOARD (9)
5730 IF TEMP = 3 THEN WINNER = 1: RETURN
5740 IF TEMP = -3 THEN WINNER = -1: RETURN
5750 REM no winner so far
5760 WINNER = 0: RETURN
5770 REM
6000 REM The game is over. Display winner and update score.
6010 REM
6020 REM Inputs are WINNER, PW, CW, CATW, PLAYERNM$, and INTROW.
6030 REM Outputs are PW, CW, and CATW.
6040 REM
6050 LOCATE INTROW, 1
6060 IF WINNER = 1 THEN PW = PW+1: PRINT "You got me that time, "; PLAYERNM$;".
6070 IF WINNER = -1 THEN CW=CW+1: PRINT "I beat you this game, "; PLAYERNM$; ".
6080 IF WINNER = 0 THEN CATW = CATW+1: PRINT "This one is a tie game "; PLAYERNM$; "."
6090 RETURN
6100 REM
6500 REM Display the current scores.
6510 REM
6520 REM Inputs are SCROW, MAXNML, CW, PW, AND CATW.
6530 REM
6540 LOCATE SCROW + 5, MAXNML + 5
6550 PRINT PW;
6560 LOCATE SCROW + 6, MAXNML + 5
6570 PRINT CW;
6580 LOCATE SCROW + 7, MAXNML + 5
6590 PRINT CATW;
6600 RETURN
6610 REM
7000 REM Check to see if the player wants another game. Return
7010 REM with MORE = 1 if so, otherwise return with MORE = 0.
7020 REM
7030 REM Input is INTROW.
7040 REM Output is MORE.
7050 REM
7060 LOCATE INTROW + 2, 1
7070 LINE INPUT "Do you want to play again (ENTER = yes)? ", TEMP$
7080 MORE = 1
7090 IF LEFT$ (TEMP$,1) = "n" OR LEFT$ (TEMP$,1) = "N" THEN MORE = 0
7100 GOSUB 9000' clear interaction area
7110 RETURN
7120 REM
7500 REM Display the final message.
7510 REM
7520 REM Inputs are PLAYERNM$, PW and CW.
7530 REM
7540 LOCATE INTROW, 1
7550 PRINT "Thanks for playing "; PLAYERNM$; "."
7560 IF CW = PW THEN PRINT "I guess we're about even."
7570 IF CW > PW THEN PRINT "Sorry to have humiliated you."
7580 IF PW > CW THEN PRINT "You showed me a thing or two."
7590 RETURN
7600 REM
8000 REM Display error message.
8010 REM
8020 REM Inputs are INTROW and ERRTYPE.
8030 REM
8040 LOCATE INTROW + 2, 1
8050 GOSUB 8500' clear the line
8060 IF ERRTYPE = 1 THEN PRINT "Your choice must be a number between 1 and 9."
8070 IF ERRTYPE = 2 THEN PRINT "Don't try to fool me; that square is taken."
8080 RETURN
8090 REM
8500 REM Clear the current line and leave the cursor unmoved.
8510 REM
8520 TEMP = CSRLIN' save the original cursor line
8530 LOCATE TEMP, 1' place cursor at left edge of line
8540 PRINT SPACE$ (80);
8550 LOCATE TEMP, 1' restore cursor to original line
8560 RETURN
8570 REM
9000 REM Clear the interaction area on the screen.
9010 REM
9020 REM Input is INTROW.
9030 REM
9040 LOCATE INTROW, 1
9050 FOR I = 1 TO 3
9060 PRINT SPACE$ (80)
9070 NEXT I
9080 RETURN