Tic Tac Toe
Author |
Message |
Sway
|
Posted: Wed Jan 18, 2006 8:46 pm Post subject: Tic Tac Toe |
|
|
Alright, I'm making a tic tac toe game for my turing summative, but i just can't seem to get the game actually working. I'm going to use a 2d array, I've got the 3 by 3 board set up and I have all of the graphics done. I'm completely stuck as to where I go from here though, clearly a turing newbie... can anyone give me a hint as to where to take it next? |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Cervantes

|
Posted: Wed Jan 18, 2006 9:07 pm Post subject: (No subject) |
|
|
You'll need to get input. Mouse.ButtonWait would be the way to go. |
|
|
|
|
 |
zylum

|
Posted: Wed Jan 18, 2006 9:14 pm Post subject: (No subject) |
|
|
ok so you have your 2d array... you need to initialize it to some value, probably zero. then you need to designate a number for player one and player two. id suggest numbers 1 and -1. these numbers will make things a little easier later on.
next you need a variable that keeps track of which players turn it is. initialize it to 1. everytime a player makes a move, multiply this variable by -1 so that it indicates the next player.
input will most likely come from the mouse so you will need vars to store those values.
other useful procedures to program are one to check if there is a winner and one to draw the board. these will help reduce clutter in your main loop.
now you can create your main loop. it should get input from the mouse. if it is clicked in one of the empty cells then change the value of that cell to the value of your 'player' variable. next draw the board. after each turn call your function that checks whether theres a winner. then you change your player variable by multiplying by -1.
the hardest part will probably creating the function that will determine if there is a winner so i have included code for you:
code: | fcn winner : int
var ret := 0
var h, v, d1, d2 : int
for i : 1 .. 3
r := 0
c := 0
d1 := 0
d2 := 0
for j : 1 .. 3
r += board (i, j)
c += board (j, i)
d1 += board (j, j)
d2 += board (4 - j, j)
end for
ret += r div 3 + c div 3 + d1 div 3 + d2 div 3
end for
result sign (ret)
end winner |
the function has variable 'ret' which is the return value. it will be eighther a player number if there is a winner or 0 if there is no winner. the four variables h, v, d1, d2 keep track of the rows cols and diagonals. the function loops through each of these and adds the value in the cells in these rows/cols/diags. if there is indeed a winner, then one of these values will be +- 3. so after each loop we sum up all these vars div 3. so if it is not +- 3 it will result in a zero otherwise it will be a player number.
anyways i hope that gets you started... if you have any questions along the way ask away  |
|
|
|
|
 |
|
|