2 registered members (AndrewAMD, TipmyPip),
12,709
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: turn based game
[Re: metal]
#317138
03/29/10 14:23
03/29/10 14:23
|
Joined: Dec 2008
Posts: 1,660 North America
Redeemer
Serious User
|
Serious User
Joined: Dec 2008
Posts: 1,660
North America
|
if(turn==false){change board piece to X;turn =true;} else{change it to O; turn=false;} Better way: The best way to determine who has won is to use a 2 dimensional array that carries an index for every space on the board. You can fill up this array as the game is played out. At the end of every turn just perform some checks to see if that player has "connected" three of his own tiles in a row, perhaps like this:
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.
}
}
Just call the end_of_turn() function at the end of every player's turn. Remember to pass the player number to the function when you call it. 
|
|
|
|