Coded up a quick Noughts and Crosses game. I thought it might help to share the code. I put this together rather quickly, so it's not the best example, but it works well and I think it illustrates a few concepts.
The check for a win condition could use a better algorithm, and it really only needs to check the last player that made a play. But I'm ok with the brute force method of checking coulumns, then rows, then the two diagonals. I'm sure there is a simpler method, but when I tried to combine them in a single loop, the concept for anyone learning from the code seemed to get lost.
If you spot an error,, let me know.
(I might add drawing a line through the win)
The media (a simple .png file for the X and O's )is attached.
version 1.3
Rem Project: xo
Rem Created: Monday, October 17, 2011
Rem Code by Zenassem
Rem Version 1.3
Rem ***** Main Source File *****
sync on `turn manual sync on
sync rate 60 `set sync rate to 60
backdrop off `get rid of the "blue" backdrop
`Declare Global variables & arrays
global player as byte `value for current player (1 or 2)
global release as byte `value for state mouse-button release (1=released 0=pressed)
global box_val as integer `value of the game board (-1=Free 1=X 2=O)
global win as byte `value for state of win condition (0=no one 1=player 1 X 2=player 2 O)
global nowin as byte `value for state of row/col win (o=a win is possible 1=a win is not possible)
global index_x as integer `general variable used for indexing x dimensions
global index_y as integer `general variable used for indexing y dimensions
global xloc as integer `value stores the mouse x position
global yloc as integer `value stores the mouse y position
#constant BOX_X_SIZE 64 `X size of a single box of the grid && image x size
#constant BOX_Y_SIZE 64 `Y size of a single box of the grid && image y size
dim Check(2) `array 3 items (0-2) used for checking win conditions
dim Board(2,2) `array 3x3 (0-2,0-2) stores the status of the game board
`Load Media
load image "xo.png",99,1 `load our image (no mipmaps)
paste image 99,0,0,1 `Paste the image (make use of transparency)
get image 1,0,0,BOX_X_SIZE,BOX_Y_SIZE,1 `Extract our image of X
get image 2,BOX_X_SIZE,0,(BOX_X_SIZE*2),(BOX_Y_SIZE),1 `Extract our image of O
delete image 99 `delte the temp loaded image
`Set all variables and board for New Game
gosub Init_New_Game
`Main Loop
DO
cls 0x00002A06 `Clear the screen with a chalkboard green
gosub Draw_Board
gosub Draw_Grid
gosub Turn
gosub Check_Win
if win=1
gosub Draw_Board
gosub Draw_Grid
ink 0xFFFF0000 `set ink to a bright RED
text 224,96, "X WINS!!! Press <any key> to restart ..."
sync
wait key
gosub Init_New_Game
endif
if win=2
Gosub Draw_Board
Gosub Draw_Grid
ink 0xFFFF0000 `set ink to a bright RED
text 224,96, "O WINS!!! Press <any key> to restart ..."
sync
wait key
gosub Init_New_Game
endif
sync
loop
END
`END Main Loop
Init_New_Game:
ink 0xFFFFFFFF `set Ink to a bright WHITE
player=1 `player 1 'X" starts the game
release=1 `mouse-button released
win=0 `The game has not been won
`initialize the board to all -1's: -1 = free space
`board is a 3x3 grid indexed from 0-2
for index_y=0 to 2
for index_x=0 to 2
Board(index_x,index_y)=-1
next index_x
next index_y
return
Draw_Grid:
`Draw horizontal lines
line 0,BOX_Y_SIZE,BOX_X_SIZE*3,BOX_Y_SIZE
line 0,BOX_Y_SIZE*2,BOX_X_SIZE*3,BOX_Y_SIZE*2
`Draw vertical lines
line BOX_X_SIZE,0,BOX_X_SIZE,BOX_Y_SIZE*3
line BOX_X_SIZE*2,0,BOX_X_SIZE*2,BOX_Y_SIZE*3
return
Draw_Board:
for index_y=0 to 2
for index_x=0 to 2
box_val=Board(index_x,index_y)
if box_val<>-1
paste image box_val,((index_x)*(BOX_X_SIZE)),((index_y)*(BOX_Y_SIZE)),1
endif
next index_x
next index_y
return
Turn:
if player=1
text 0,256,"Player X's turn"
else
text 0,256,"Player O's turn"
endif
`See if the mouse is over our grid, AND the mouse-button1 is clicked, and that it was previosly released
if mouseclick()=1 AND mousex()<192 AND mousey()<192 AND release=1
release=0 `mouse is not released
xloc=((mousex()/BOX_X_SIZE)) `find the column of the mouse
yloc=((mousey()/BOX_Y_SIZE)) `find the row of the mouse
if Board(xloc,yloc)=-1 `if the board was a free space then a turn has been played
Board(xloc,yloc)=player `update the board to reflect the play
if player=1 `change which player's turn it is
player=2
else
player=1
endif
endif
endif
if mouseclick()=0 `check if the mouse is released
release=1 `if it is set release to 1
endif
return
Check_Win:
`Check for vertical column wins
for index_x=0 to 2
for index_y=0 to 2
if Board(index_x,index_y)=-1 then nowin=1 `if there is a -1(free space) in any column than there isn't a win
Check(index_y)=Board(index_x,index_y)
next index_y
if Check(0)=Check(1) AND Check(1)=Check(2) AND nowin=0 `make sure all 3 values in a column are the same and that they aren't free
if Check(0)=1
win=1:return `if we have all 1's player 1 "X" wins
else
win=2:return `else player 2 "O" wins
endif
endif
nowin=0 `reset the nowin to 0 and check next vertical column
next index_x
`Check for horizontal row wins
`same as column check but now we do the same for rows
for index_y=0 to 2
for index_x=0 to 2
if Board(index_x,index_y)=-1 then nowin=1
Check(index_x)=Board(index_x,index_y)
next index_x
if Check(0)=Check(1) AND Check(1)=Check(2) AND nowin=0
if Check(0)=1
win=1:return
else
win=2:return
endif
endif
nowin=0
next index_y
`Check for Diagonal (top-left to bottom-right) win
if Board(0,0)=Board(1,1) AND Board(1,1)=Board(2,2) And Board(0,0)<>-1
if Board(0,0)=1
win = 1: return
else
win = 2: return
endif
endif
`Check for Diagonal (bottom_left to top-right win
if Board(0,2)=Board(1,1) AND Board(1,1)=Board(2,0) And Board(0,2)<>-1
if Board(0,2)=1
win = 1: return
else
win = 2: return
endif
endif
return
Your signature has been erased by a mod please reduce it to 600 x 120.