turn based game

Posted By: bilby11

turn based game - 03/29/10 12:03

gid day all,

I am trying to make a Tic Tac Toe (or checkers) game for my little brother which is turn based game so that I can play with him. Can anyone point in the right direction of a turn based script for Tic Tac Toe &/or checkers, or simular. I have been about to get my board developed, and is using mouse_ent with ent_morph to change board pieces into either X or O, but I have not been able to work out how to change from one player to the other player. Also, if someone can explain how to c-scan or c-trace or whatever to work out if someone has won, that would be very helpful (ie three X in a row).

Thank you in advance
Bilby11
Posted By: metal

Re: turn based game - 03/29/10 12:32

you could use something like
BOOL turn=false; //false => Player1 has to set his marker; true for Player2
if you click at the board you can chekc the value of "turn":
if(turn==false){change board piece to X;turn =true;}
else{change it to O; turn=false;}
Posted By: Redeemer

Re: turn based game - 03/29/10 14:23

Quote:
if(turn==false){change board piece to X;turn =true;}
else{change it to O; turn=false;}

Better way:
Code:
turn = (turn==false);



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:

Code:
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. wink
Posted By: bilby11

Re: turn based game - 03/30/10 09:34

Gid day;

Thank you Metal, I have been able to get the turn about working.

Thank you Redeemer for your scoring snipet, however I have been getting a Syntex error: Can't convert EQ:FIXED:POINTER:LONG.
<if (game_board[row][0] == player;

Can you help me out? What have I been doing wrong?

Thank you in advance
Bilby11
© 2024 lite-C Forums