   Switching players in TicTacToe
  I am trying to make a simple, text TTT game.  So far I can select a square and assign it an ''X'' ...it also makes sure the square hasn''t been taken already.So now I want the ''O'' player to play as well.  I am trying to think up the simplest way to decide who''s turn it is.  What I''ve come up with (in my head) is a simple counter that gets incremented at the end of end each turn.  If its not divisible by 2, its ''X'' turn, if it is div by 2, then its ''O'' turn.  And I would have to make it so picking a taken square doesn''t trigger a player switch.Is this a fair approach or is there a smarter approach for this kind of(and other) simple turn-based games?Thanks 
  Just make each board position into a char variable. All you have to do is have a while loop and have it continually ask them a position until it is available. If the char variable isn't the position num, then have them repick. If it is open, you set the position to either O or X depending on the player.Hope you understand! : )    [edited by - RuntimeFailure on August 2, 2003 7:19:28 PM];  I'd say that's a fair way of doing it.  To make it clearer, you could always use some enum constants.  To demonstrate:// Just a program to demonstrate what I'm rambling on about. =)#include <iostream>using std::cout;int main(){int turnCounter;enum PlayerTurn { PLAYERONETURN, PLAYERTWOTURN };for (turnCounter = 0; turnCounter < 10; turnCounter++){switch (turnCounter % 2){case PLAYERONETURN:// Player 1 code goes here.cout << "Player 1's turn.\n";break;case PLAYERTWOTURN:// Player 2 code goes here.cout << "Player 2's turn.\n";break;default:// Good practice to always have a default case.break;}}return 0;}   Hope that's what you're askin for. =)--hellz   [edited by - hellz on August 2, 2003 7:28:34 PM];  I would use a bool for the turns and XOR it with 1 after each turn. ;  RuntimeFailure:  No, I am looking for a way, within my code to determine who''s turn it is.  I''ve got all the other stuff you mentioned up to that point.  But I think I got it now...just read hellz''s reply.hellz:  Thanks for the "%2" idea your code.  Forgot I could do that.  I substituded it for the check I was using and it switches players perfectly now.  If I used an ENUM for the players, wouldn''t that mean duplicating some of the code unnecessarily?  It seems like it would be much more readable than what I did though.  I simply went thru and coded one full turn and at the end had it determine whether an X or an O should go to the square.  I am just happy it works at all.  This is my first attempt at making a game.  Now I just have to have it determine a win so it''s actually a game.  Thanks guys. ;  2 player is the easy way out of programming TicTacToe. To give you a real challenge do it one player . I am a newbie at C++ and still can''t get mine to work...although a few hours ago I found a way ...just haven''t put it together yte ;quote:Original post by logic monkey  hellz:  Thanks for the "%2" idea your code.  Forgot I could do that.  I substituded it for the check I was using and it switches players perfectly now.You roughly touched on the idea yourself, so don''t thank me. quote:Original post by logic monkey  If I used an ENUM for the players, wouldn''t that mean duplicating some of the code unnecessarily?  It seems like it would be much more readable than what I did though.  I simply went thru and coded one full turn and at the end had it determine whether an X or an O should go to the square.There''s a chance that it does duplicate some code unnecessarily, but I''ve always tended to lean towards readable (and manageable) code, as opposed to lots of "magic" values everywhere. quote:Original post by logic monkey  I am just happy it works at all.  This is my first attempt at making a game.  Now I just have to have it determine a win so it''s actually a game.  Thanks guys.   Glad to be of help, and good luck! --hellz;quote:Original post by DarkWhoppy  2 player is the easy way out of programming TicTacToe. To give you a real challenge do it one player . I am a newbie at C++ and still can''t get mine to work...although a few hours ago I found a way ...just haven''t put it together yte  Yeah, that is the next plan.  Eventually, after I finish a complete 2 player version, I would like to have the second player be the computer.  Programming AI for it doesn''t seem too hard, but its still a little ahead of where I''m am.  My next step will be determining when a player has won and to restart the game.Your post over in the AI forum is actually why I even decided to try to make the game today.  I wanted to test myself on the reading I''ve been doing and tic-tac-toe seemed to hit the spot.  If you get your one-player version done, I hope you''ll post the code either here or there.  I approached the game a little differently, but it''d be good to have something to check my work against. ;quote:Original post by logic monkey  I am trying to make a simple, text TTT game.  So far I can select a square and assign it an ''X'' ...it also makes sure the square hasn''t been taken already.So now I want the ''O'' player to play as well.  I am trying to think up the simplest way to decide who''s turn it is.  What I''ve come up with (in my head) is a simple counter that gets incremented at the end of end each turn.  If its not divisible by 2, its ''X'' turn, if it is div by 2, then its ''O'' turn.  And I would have to make it so picking a taken square doesn''t trigger a player switch.Is this a fair approach or is there a smarter approach for this kind of(and other) simple turn-based games?Thanks   Just use a boolean if(playerOnesTurn){  playerOnesTurn = false;  //player one move stuff}else{  playerOnesTurn = true;  //player two move stuff}  simple ;  This is my 1 or 2 player ttt game: #include <stdio.h>#include <conio.h>#include <windows.h>int turn = 1, move = 0, won = 0, players = 0; // Sets turn to 1, move to 0, and done to 0char restart;char name1[25], name2[25];char board[9]; // The tic-tac-toe Boardvoid Playersname(){if (players == '1'){printf("\n");printf("What is the player's name: ");scanf("%s", name1);system("cls");}else if (players == '2'){printf("\n");printf("What is 1st player's name: ");scanf("%s", name1);printf("\nWhat is 2nd player's name: ");scanf("%s", name2);system("cls");}}void BoardSetup(){int n;for(n=0;n<9;n++) {board[n]= '1' + n;}}void DrawBoard(){printf("\n\n");printf(" %c | %c | %c \n", board[0], board[1], board[2]);printf(" --------- \n");printf(" %c | %c | %c \n", board[3], board[4], board[5]);printf(" --------- \n");printf(" %c | %c | %c \n", board[6], board[7], board[8]);printf("\n\n");}void AImove (){move = (rand()%9+1);move--;if((board[move]!='X')&&(board[move]!='O')){board[move]='O';turn=1;}system("cls");}void Playersturn(){if(turn == 1){printf("It is %s's turn: ", name1);scanf("%d", &move);move--;if((board[move]!='X')&&(board[move]!='O')){board[move]='X';turn=2;}else{printf("Invalid Move\n");}system("cls");} //ifelse {if (players == '2') {printf("It is %s's turn: ", name2);scanf("%d", &move);move--;if((board[move]!='X')&&(board[move]!='O')){board[move]='O';turn=1;}else {printf("Invalid move\n");}system("cls");}else{printf("The Computer is thinking!!!");AImove ();turn = 1;}} //esle } //Playerturnvoid Wincheck(){if ((board[0] == 'X' && board[1] == 'X' && board[2] == 'X') ||(board[3] == 'X' && board[4] == 'X' && board[5] == 'X') ||(board[6] == 'X' && board[7] == 'X' && board[8] == 'X') || (board[0] == 'X' && board[3] == 'X' && board[6] == 'X') || (board[1] == 'X' && board[4] == 'X' && board[7] == 'X') || (board[2] == 'X' && board[5] == 'X' && board[8] == 'X') || (board[0] == 'X' && board[4] == 'X' && board[8] == 'X') || (board[2] == 'X' && board[4] == 'X' && board[6] == 'X')){printf("%s wins!!!\n", name1);won = 1;}if ((board[0] == 'O' && board[1] == 'O' && board[2] == 'O') ||(board[3] == 'O' && board[4] == 'O' && board[5] == 'O') ||(board[6] == 'O' && board[7] == 'O' && board[8] == 'O') || (board[0] == 'O' && board[3] == 'O' && board[6] == 'O') || (board[1] == 'O' && board[4] == 'O' && board[7] == 'O') || (board[2] == 'O' && board[5] == 'O' && board[8] == 'O') || (board[0] == 'O' && board[4] == 'O' && board[8] == 'O') || (board[2] == 'O' && board[4] == 'O' && board[6] == 'O'))if (players == '1'){printf("Computer has won!!!");won = 1;}else{printf("%s wins!!!\n", name2);won = 1;}}void Playgame () {    do{    DrawBoard();    Playersturn();    Wincheck();} // playgame loop    while (won == 0);}int main(){    printf("\n");    printf("Welcome to Tic-Tac-Toe!!!\n\n");printf("How many players (1) or (2): ");scanf ("%c", &players);system("cls");Playersname();do {BoardSetup();Playgame();printf("\n");printf("Do you want to play again?");restart=getch();won = 0;system("cls"); }while( toupper(restart) == 'Y');return 0; }// Int main()The AI isn't smart... it just uses rand() to find a space.  This should give you an idea on what you can do...  (friend) WHAT!!!!..... Are you crazy??? (me) Yes I am. [edited by - librab103 on August 3, 2003 1:18:21 PM]