var game_board[3][3]; // this is your game board
var winner = 0; // the winner of this game. 0=nobody, 1=player1, 2=player2, etc.
function check_across( row, player )
{
// check if the player wins
if( game_board[row][0] == player )
if( game_board[row][1] == player )
if( game_board[row][2] == player )
{
// the player who placed all these identical pieces in a row wins!
winner = player;
}
}
function check_down( column, player )
{
// check if the player wins
if( game_board[0][column] == player )
if( game_board[1][column] == player )
if( game_board[2][column] == player )
{
// the player who placed all these identical pieces in a column wins!
winner = player;
}
}
function check_diagonals( player )
{
// check if the player wins down and right
if( game_board[0][0] == player )
if( game_board[1][1] == player )
if( game_board[2][2] == player )
{
// the player who placed all these identical pieces down and right wins!
winner = player;
}
// check if the player wins down and left
if( game_board[0][2] == player )
if( game_board[1][1] == player )
if( game_board[2][0] == player )
{
// the player who placed all these identical pieces down and left wins!
winner = player;
}
}
function end_of_turn( player )
{
var c;
// this function checks to see if a player won the game
for( c=0; c<3; c++ )
{
check_across( c, player );
check_down( c, player );
}
check_diagonals( player );
// did this player win?
if( winner == player )
{
// celebrate his winning, restart the game, etc. etc.
}
}