Qbasicnews.com

Full Version: Tic Tac Toe
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Does anyone have any idea on how to stop ai from choosing a already taken space?
It depends on how you do it. A simple way is to just have an array with 3 columns, 3 rows and have 0 for empty, 1 for X and 2 for 0. E.g.

DIM grid(1 to 3, 1 to 3)

You can then place an X in the center by doing

grid(1, 1) = 1

And check if one is ther by using an IF

IF grid(1, 1) = 0 THEN 'grid is empty else not
I think I answered this exact question(from a different person) here:

http://forum.qbasicnews.com/viewtopic.php?t=7219

*peace*

Meg.
yes but does the end loop statement always work?
i mean i can get it to block the space but it is only after 3 turns which leads me to believe that there is something wrong with my time variable.
I'm not sure exactly what you mean. It would be easier to see if you posted some code.

Here is a Tic-Tac-Toe program I put together. Feel free to look through it and see whether you can figure out how I made the computer move. The computer chooses its move randomly.

*peace*

Meg.

[syntax="QBASIC"]
'THIS IS A SIMPLE TIC-TAC-TOE PROGRAM. THE COMPUTER MOVES RANDOMLY.
'
' - Written 11/12/2004 by MB

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Subroutine declarations.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DECLARE SUB CheckWin (Winner%)
DECLARE SUB ComputerMove ()
DECLARE SUB YourMove ()
DECLARE SUB DrawBoard ()

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Create a 3x3 grid that holds numbers. 0 = empty, 1 = X, 2 = O
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DIM SHARED Board(1 TO 3, 1 TO 3) AS INTEGER

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Seed the RND function.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
RANDOMIZE TIMER

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'TotalMoves% counts total number of moves. Used to tell when board is full.
'WhoseTurn% is either 1 (your turn) or -1 (computer turn)
'Winner% will hold who wins the game. 0 = tie, 1 = X's, 2 = O's.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
TotalMoves% = 0
WhoseTurn% = -1
Winner% = 0

DO
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Update the board on the screen.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
CALL DrawBoard

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Toggle whose turn it is.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
WhoseTurn% = -WhoseTurn%

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Depending on whose turn it is, call one of two subroutines.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IF WhoseTurn% = 1 THEN
CALL YourMove
ELSE
CALL ComputerMove
END IF

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'See if either player has won. If so, Winner% will be set to 1 or 2.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
CALL CheckWin(Winner%)

TotalMoves% = TotalMoves% + 1

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'End the big loop when the board is full or a winner has been found.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

LOOP UNTIL TotalMoves% = 9 OR Winner% > 0

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Declare the winner (or tie).
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
CALL DrawBoard
SELECT CASE Winner%
CASE 0
PRINT "TIE GAME."
CASE 1
PRINT "YOU WIN."
CASE 2
PRINT "I WIN."
END SELECT

SYSTEM

SUB CheckWin (Winner%)

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Check rows for wins. If a row matches & isn't 0's, set Winner%.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
FOR y% = 1 TO 3
IF Board(1, y%) = Board(2, y%) AND Board(1, y%) = Board(3, y%) THEN
IF Board(1, y%) > 0 THEN Winner% = Board(1, y%)
END IF
NEXT y%

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Check columns for wins. If a column matches & isn't 0's, set Winner%.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
FOR x% = 1 TO 3
IF Board(x%, 1) = Board(x%, 2) AND Board(x%, 1) = Board(x%, 3) THEN
Winner% = Board(x%, 1)
IF Board(x%, 1) > 0 THEN Winner% = Board(1, y%)
END IF
NEXT x%

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Check diagonals. If a diagonal matches & isn't 0's, set Winner%.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IF Board(1, 1) = Board(2, 2) AND Board(1, 1) = Board(3, 3) THEN
IF Board(1, 1) > 0 THEN Winner% = Board(1, 1)
END IF

IF Board(1, 3) = Board(2, 2) AND Board(1, 3) = Board(3, 1) THEN
IF Board(1, 3) > 0 THEN Winner% = Board(1, 3)
END IF

END SUB

SUB ComputerMove

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Pick random squares until an empty one is encountered.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DO
Move.x% = INT(RND * 3) + 1
Move.y% = INT(RND * 3) + 1
LOOP UNTIL Board(Move.x%, Move.y%) = 0

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Set the square on the board to 2 (Y owns the square now).
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Board(Move.x%, Move.y%) = 2

END SUB

SUB DrawBoard

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Draw the empty board and the coordinates.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
CLS
PRINT " ³ ³ 1"
PRINT "ÄÅÄÅÄ "
PRINT " ³ ³ 2"
PRINT "ÄÅÄÅÄ "
PRINT " ³ ³ 3"
PRINT
PRINT "A B C "

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Fill the board with letters based off of the numbers in Board().
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
FOR y% = 1 TO 3
FOR x% = 1 TO 3
LOCATE y% * 2 - 1, x% * 2 - 1
SELECT CASE Board(x%, y%)
CASE 1
PRINT "X";
CASE 2
PRINT "O";
CASE 3
PRINT " ";
END SELECT
NEXT x%
NEXT y%

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Set the cursor two lines underneath the board.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
LOCATE 9, 1

END SUB

SUB YourMove

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Get user input until they've entered a valid move.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DO
INPUT "Enter your move (example: A1): ", Choice$
Choice$ = UCASE$(Choice$)

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Make sure they entered a valid choice
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ValidMove% = 0 'initialize flag
IF LEN(Choice$) = 2 THEN 'check length
Let$ = MID$(Choice$, 1, 1) 'parse letter
Num$ = MID$(Choice$, 2, 1) 'parse number
'
Move.x% = INSTR("ABC", Let$) 'get let value
Move.y% = INSTR("123", Num$) 'get num value
'
IF Move.x% >= 1 AND Move.x% <= 3 THEN 'check x range
IF Move.y% >= 1 AND Move.y% <= 3 THEN 'check y range
IF Board(Move.x%, Move.y%) = 0 THEN 'check empty
ValidMove% = 1 'OK! set flag
ELSE '
PRINT "Occupied Space." 'occupied space
END IF '
ELSE '
PRINT "Invalid Number." 'bad y range
END IF '
ELSE '
PRINT "Invalid Letter." 'bad x range
END IF '
END IF '
LOOP UNTIL ValidMove% = 1 'loop until flag

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Set the square on the board to 1 (X owns the square now).
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Board(Move.x%, Move.y%) = 1

END SUB[/syntax]