thats a really odd way to do this
i'd make it using more conventional method (^_^)
firstly we'll setup the main game
`// RavenVegeta | TicTacToe
`// 16 Feburary 2003
`-- environment ----
sync on : sync rate 0
set window on : set window size 640,480
remstart
/* only use if you have DarkBasic Enhanced
lock backbuffer
dwScreenHeight = get backbuffer height()
dwScreenWidth = get backbuffer width()
unlock backbuffer
set window position dwScreenWidth/2,dwScreenHeight/2
*/
remend
we then want to make a small arena for using as the tic tac toe board
`// draw tic tac toe
function draw_board( intX, intY, intStep )
`// range array
dim pLine(2,4) : x = 1 : y = 2
`// step equasions
StepEq1 = (intStep/2)*3 : StepEq2 = intStep/2
`// populate array
pLine(x,1) = intX - StepEq1 : pLine(x,2) = intX - StepEq2
pLine(x,3) = intX + StepEq2 : pLine(x,4) = intX + StepEq1
pLine(y,1) = intY - StepEq1 : pLine(y,2) = intY - StepEq2
pLine(y,3) = intY + StepEq2 : pLine(y,4) = intY + StepEq1
`// actually draw table
line pLine(x,1),pLine(y,2),pLine(x,4),pLine(y,2)
line pLine(x,1),pLine(y,3),pLine(x,4),pLine(y,3)
line pLine(x,2),pLine(y,4),pLine(x,2),pLine(y,1)
line pLine(x,3),pLine(y,4),pLine(x,3),pLine(y,1)
`// release memory
undim pLine(2,4)
endfunction
now we have our board we'll want position for the text or drawn X's and O's to go in
so next thing to do is to setup a 9 position array for this too
`// Tic Tac Toe game array
dim arTicGame(9,3)
`// fill position array
function fill_array( intX, intY, intStep )
x = 1 : y = 2
b = intX - intStep : b = intY + intStep
for indexX = a to a + (intStep*2) step intStep
for indexY = b to b + (intStep*2) step intStep-(intStep*2)
arTicGame(index,x) = indexX
arTicGame(index,y) = indexY
next indexY
inc index
next indexX
endfunction
notice that the input is identical to the _draw_board ... this is because both must be sized exactly the same to work
and as the game array is included in the function it shouldn't be placed in an external array because it has been setup exclusively for this game.
you notice we made 3 elements to each square?
well 1 & 2 are the X Y positions ... and the thrid will be used for the actual game itself as its to be the fill type.
so we can set this up now.
`// game types
Tic_U = 0 :`// unused
Tic_X = 1 :`// X
Tic_O = 2 :`// O
now we have our peices, how about we make something to draw them? (^_^)
here we'll cover 2 functions and a simple IF statement that checks the array.
`// check all Tic Tac Toe Spaces for peice to draw
for index = 1 to 9
if arTicGame(index,3) not Tic_U
draw_tic( arTicGame(index,1), arTicGame(index,2 ), arTicGame(index,3) )
endif
next index
function draw_tic( intX, intY, intType )
`// private equasion
Eq1 = dwStep/2 - 2
`// check type
if intType = Tic_X
dim ptrX(4) :`// create a quick array and populate it
ptrX(1) = intX + Eq1 : ptrX(2) = intY + Eq1
ptrX(3) = intX - Eq1 : ptrX(4) = intY - Eq1
`// draw X
line ptrX(3),ptr(2),ptr(1),ptr(4)
line ptrX(1),ptr(2),ptr(3),ptr(4)
else
if intType = Tic_O
`// draw circle
circle intX,intY,Eq1
else
bReturn = 1 :`// return 1 if there is an error
endif
endif
`// release memory
undim ptrX(4)
endfunction bReturn
you'll notice rather than using the = i used the not word, this basically means that if the answer is anything BUT that then run the if
oh man were almost finished now and perhaps might not seem like it, but there is just a little bit left to go.
what we have left is the player control and to check who wins.
firstly the player control as this is quite simplistic
`// make sure mouse isn't pressed
if mouseclick() > 0
bMClick = 1
else
bMClick = 0
endif
`// check where the mouse is for where its click
for index = 1 to 9
if mousex() > arTicGame( index,1 ) + (dwStep - 1)
and mousex() < arTicGame( index,1 ) - (dwStep - 1)
and mousey() > arTicGame( index,2 ) + (dwStep - 1)
and mousey() < arTicGame( index,2 ) - (dwStep - 1)
intTicArea = index
endif
next index
`// if the player clicks the mouse
`// put his peice here
if mouseclick() = 1 and bMClick = 0
if arTicGame( intTicArea,3 ) = Tic_U
arTicGame( intTicArea,3 ) = bPlayer
else
print "player peice already here!"
endif
endif
now that we have the player control lets give the players thier turns.
remember what you just wrote late, well now its time to edit that to accomidate for the player turns
`// swap turns
if bTurn = 1
bTurn = 0
if bPlayer = Tic_X
bPlayer = Tic_O
else
if bPlayer = Tic_O
bPlayer = Tic_X
endif
endif
endif
`// put his peice here
if mouseclick() = 1 and bMClick = 0
if arTicGame( intTicArea,3 ) = Tic_U
arTicGame( intTicArea,3 ) = bPlayer
bTurn = 1
else
print "player peice already here!"
endif
endif
final peice of actual code is to setup a winner check, which is quite lengthy - however this part can be presaved because it never changes
now again we'll make an array for this and this time a gosub for filling the information (^_^)
`// make win array
if file exist("win.arr") = 1
load array "win.arr",arTicWin(8,3)
else
dim array arTicWin(8,3)
gosub _fill_win_array
endif
_fill_win_array:
`// populate array from data
for indexA = 1 to 8
for indexB = 1 to 3
read dwReadData
arTicWin(indexA,indexB) = read
next indexB
next indexA
`// save array
save array "win.arr",arTicWin(8,3)
return
`// array data
data 1,4,7,2,5,8,3,6,9,1,2,3,4,5,6,7,8,9,1,5,9,3,5,7
now that we have our winning array the actual function to check if someone won is quite simple to make
function check_win()
dwCount = 0 :`// reset counter
`// check winning combinations
for indexA = 1 to 8
for indexB = 1 to 3
arTicGame(arTicWin(indexA,indexB),3) = dwBuffer
if dwBuffer = dwOldBuffer
inc dwCount
endif
dwOldBuffer = dwBuffer
if dwCount = 3 game_over(dwBuffer)
next indexB
next indexA
`// check to make sure its not a draw
dwCount = 0
for index = 1 to 9
if arTicGame(index,3) > 0
inc dwCount
endif
next index
`// all spaces are taken, but no winner then draw game
if dwCount = 9 then _game_over(Tic_U)
endfunction
`// game over options
function game_over( intType )
if intType = Tic_X
print "X Player Won!"
print "Press any key to play again"
wait key
goto __start
endif
if intType = Tic_O
print "O Player Won!"
print "Press any key to restart"
wait key
goto __start
endif
if intType = Tic_U
print "Draw Game!"
print "Press any key to restart"
wait key
goto __start
endif
endfunction
in this state however the code probably seems quite unusable as its all over the shop
well this last code section is the game completed and finished off
`// RavenVegeta | TicTacToe
`// 16 Feburary 2003
`// 1.0.0.1.0001
`-- environment ----
sync on : sync rate 0
set window on : set window size 640,480
remstart
/* only use if you have DarkBasic Enhanced
lock backbuffer
dwScreenHeight = get backbuffer height()
dwScreenWidth = get backbuffer width()
unlock backbuffer
set window position dwScreenWidth/2,dwScreenHeight/2
*/
remend
`// make win array
if file exist("win.arr") = 1
load array "win.arr",arTicWin(8,3)
else
dim array arTicWin(8,3)
gosub _fill_win_array
endif
`// restart game point
_start:
cls :`// clear screen
set cursor 0,0 :`// place cursor for text
dwX = 320
dwY = 240
dwStep = 10
`// game types
Tic_U = 0 :`// unused
Tic_X = 1 :`// X
Tic_O = 2 :`// O
`// Tic Tac Toe game array
dim arTicGame(9,3)
`// game variables
bPlayer = Tic_X
`-- main loop ----
do
cls :`// clear screen
draw_board( dwX, dwY, dwStep ) :`// update board
`// make sure mouse isn't pressed
if mouseclick() > 0
bMClick = 1
else
bMClick = 0
endif
`// swap turns
if bTurn = 1
bTurn = 0
if bPlayer = Tic_X
bPlayer = Tic_O
else
if bPlayer = Tic_O
bPlayer = Tic_X
endif
endif
endif
`// check where the mouse is for where its click
for index = 1 to 9
if mousex() > arTicGame( index,1 ) + (dwStep - 1)
and mousex() < arTicGame( index,1 ) - (dwStep - 1)
and mousey() > arTicGame( index,2 ) + (dwStep - 1)
and mousey() < arTicGame( index,2 ) - (dwStep - 1)
intTicArea = index
endif
next index
`// put his peice here
if mouseclick() = 1 and bMClick = 0
if arTicGame( intTicArea,3 ) = Tic_U
arTicGame( intTicArea,3 ) = bPlayer
bTurn = 1
else
print "player peice already here!"
endif
endif
`// check all Tic Tac Toe Spaces for peice to draw
for index = 1 to 9
if arTicGame(index,3) not Tic_U
draw_tic( arTicGame(index,1), arTicGame(index,2 ), arTicGame(index,3) )
endif
next index
sync :`// update game
loop
`-- SubRoutines ----
_fill_win_array:
`// populate array from data
for indexA = 1 to 8
for indexB = 1 to 3
read dwReadData
arTicWin(indexA,indexB) = read
next indexB
next indexA
`// save array
save array "win.arr",arTicWin(8,3)
return
`-- Functions ----
`// draw tic tac toe
function draw_board( intX, intY, intStep )
`// range array
dim pLine(2,4) : x = 1 : y = 2
`// step equasions
StepEq1 = (intStep/2)*3 : StepEq2 = intStep/2
`// populate array
pLine(x,1) = intX - StepEq1 : pLine(x,2) = intX - StepEq2
pLine(x,3) = intX + StepEq2 : pLine(x,4) = intX + StepEq1
pLine(y,1) = intY - StepEq1 : pLine(y,2) = intY - StepEq2
pLine(y,3) = intY + StepEq2 : pLine(y,4) = intY + StepEq1
`// actually draw table
line pLine(x,1),pLine(y,2),pLine(x,4),pLine(y,2)
line pLine(x,1),pLine(y,3),pLine(x,4),pLine(y,3)
line pLine(x,2),pLine(y,4),pLine(x,2),pLine(y,1)
line pLine(x,3),pLine(y,4),pLine(x,3),pLine(y,1)
`// release memory
undim pLine(2,4)
endfunction
`// fill position array
function fill_array( intX, intY, intStep )
x = 1 : y = 2
b = intX - intStep : b = intY + intStep
for indexX = a to a + (intStep*2) step intStep
for indexY = b to b + (intStep*2) step intStep-(intStep*2)
arTicGame(index,x) = indexX
arTicGame(index,y) = indexY
next indexY
inc index
next indexX
endfunction
`// draw tic piece
function draw_tic( intX, intY, intType )
`// private equasion
Eq1 = dwStep/2 - 2
`// check type
if intType = Tic_X
dim ptrX(4) :`// create a quick array and populate it
ptrX(1) = intX + Eq1 : ptrX(2) = intY + Eq1
ptrX(3) = intX - Eq1 : ptrX(4) = intY - Eq1
`// draw X
line ptrX(3),ptr(2),ptr(1),ptr(4)
line ptrX(1),ptr(2),ptr(3),ptr(4)
else
if intType = Tic_O
`// draw circle
circle intX,intY,Eq1
else
bReturn = 1 :`// return 1 if there is an error
endif
endif
`// release memory
undim ptrX(4)
endfunction bReturn
`// check for player win
function check_win()
dwCount = 0 :`// reset counter
`// check winning combinations
for indexA = 1 to 8
for indexB = 1 to 3
arTicGame(arTicWin(indexA,indexB),3) = dwBuffer
if dwBuffer = dwOldBuffer
inc dwCount
endif
dwOldBuffer = dwBuffer
if dwCount = 3 game_over(dwBuffer)
next indexB
next indexA
`// check to make sure its not a draw
dwCount = 0
for index = 1 to 9
if arTicGame(index,3) > 0
inc dwCount
endif
next index
`// all spaces are taken, but no winner then draw game
if dwCount = 9 then _game_over(Tic_U)
endfunction
`// game over options
function game_over( intType )
`// player 1 wins
if intType = Tic_X
print "X Player Won!"
print "Press any key to play again"
wait key
undim arTicGame(9,3) :`// release memory
goto __start
endif
`// player 2 wins
if intType = Tic_O
print "O Player Won!"
print "Press any key to restart"
wait key
undim arTicGame(9,3) :`// release memory
goto __start
endif
`// draw game
if intType = Tic_U
print "Draw Game!"
print "Press any key to restart"
wait key
undim arTicGame(9,3) :`// release memory
goto __start
endif
endfunction
`-- Data Declarations ----
`// array data
data 1,4,7,2,5,8,3,6,9,1,2,3,4,5,6,7,8,9,1,5,9,3,5,7
from this it isn't that hard to actually make a 3D version if you wanted
well enjoy
Tsu'va Oni Ni Jyuuko Fiori Sei Tau!
One block follows the suit ... the whole suit of blocks is the path ... what have you found?