Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,291 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 937 people online right now. Registration is fast and FREE... Join Now!




Tic Tac Toe..Problem switching users

 
Reply to this topicStart new topic

Tic Tac Toe..Problem switching users

Decipil
3 Dec, 2008 - 11:11 AM
Post #1

New D.I.C Head
*

Joined: 19 Nov, 2008
Posts: 41


My Contributions
Ok I got almost all the code done I just can't seem to figure out how to switch the users, any ideas.

Here is my code...

CODE


#include <iostream>

using namespace std;

class TicTacToe{
         private:
                char theBoard [3][3];
         public:
                TicTacToe(void);
                void playOneGame(void);
                void switchPlayer(char &);
                void showBoard(void);
                void postMove(int, int, char);
                char determineWinner(void);
};


int main (void)
{
        //test the class by playing one game
        TicTacToe Game1;
        Game1.playOneGame();
        
}

void TicTacToe::playOneGame(void){
      //start a game and play until someone wins or a draw occurs...
      const int MaxMoves = 9;
      char currentPlayer = 'O';
      int row = 0;
      int clmn = 0;
      char theWinner = ' ';
      int nmbrOfMoves = 0; //keep track of the number of moves max is 9

      do {
            switchPlayer(currentPlayer); //change player from x to o or vice versa                
            showBoard();            

            cout << "\n\nPlayer " << currentPlayer << endl; //get the players move

            postMove(row, clmn, currentPlayer); //post the move to the board      
            theWinner = determineWinner();  //see if anyone won the game
            nmbrOfMoves++;  //keep track of the number of moves

      } while ((theWinner == 'D') && (nmbrOfMoves < MaxMoves));

       showBoard(); //show the ending board

       if (theWinner != 'D')  //declare a winner
            cout << "\n\nThe Winner is player " << theWinner << endl;
       else
            cout << "\n\nThe Game was a Draw";
}

TicTacToe::TicTacToe(void)
{
          //intialize the array contents
    int i, j;

    for (i = 0; i < 3; i++)
        for ( j = 0; j < 3; j++) theBoard[i][j] = ' ';
        
}

void TicTacToe::switchPlayer(char &currentPlayer)
{
         //switches the current player

}

void TicTacToe::showBoard()
{
        //displays the board
    void showBoard(void);
    {
        int t;
        for(t = 0; t < 3; t++)
        {
            printf (" %c | %c | %c ",theBoard[t][0],
                    theBoard[t][1], theBoard[t][2]);
            if (t != 2) printf("\n---|---|---\n");
        }
        printf ("\n");
    }
}

void TicTacToe::postMove(int row, int col, char value)
{
       //gets the users move and posts it to the board
    int x, y;

     printf("Enter Row (1,2,3),Column (1,2,3) for your move: ");
    scanf_s("%d%*c%d", &x, &y);

    x--, y--;

    if (theBoard [x][y] != ' ')
    {
        printf ("Invalid Move, Try Again.\n");
        postMove(1, 3, x);
    }
    else theBoard [x][y] = 'O';

}

char TicTacToe::determineWinner(void){
      //analyzes the board to see if there is a winner
      //returns a X, O indicating the winner
      //if the game is a draw then D is returned

      //check the rows
      for (int i = 0; i < 3; i++){
            if (theBoard[i][0] == theBoard[i][1]
                 && theBoard[i][1] == theBoard[i][2]
                 && theBoard[i][0] != ' '){
                 return theBoard[i][0];
            }
      }

      //check the clmns
      for (int i = 0; i < 3; i++){
             if (theBoard[0][i] == theBoard[1][i]
                   && theBoard[1][i] == theBoard[2][i]
                   && theBoard[0][i] != ' '){
                   return theBoard[0][i];
             }
       }

       //check the diagnals
       if (theBoard[0][0] == theBoard[1][1]
              && theBoard[1][1] == theBoard[2][2]
              && theBoard[0][0] != ' ') {
              return theBoard[0][0];
       }

       if (theBoard[2][0] == theBoard[1][1]
               && theBoard[1][1] == theBoard[0][2]
               && theBoard[2][0] != ' ') {
               return theBoard[2][0];
       }

       return 'D';
}




User is offlineProfile CardPM
+Quote Post


JackOfAllTrades
RE: Tic Tac Toe..Problem Switching Users
3 Dec, 2008 - 11:38 AM
Post #2

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 3,055



Thanked: 270 times
Dream Kudos: 50
Expert In: Nothing. Well, nothing relevant here anyway. ;)

My Contributions
cpp
void TicTacToe::switchPlayer(char &currentPlayer)
{
currentPlayer = currentPlayer == 'X' ? 'O' : 'X';
}


This post has been edited by JackOfAllTrades: 3 Dec, 2008 - 11:38 AM
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Tic Tac Toe..Problem Switching Users
3 Dec, 2008 - 11:40 AM
Post #3

//
Group Icon

Joined: 1 Dec, 2005
Posts: 2,453



Thanked: 50 times
Dream Kudos: 525
My Contributions
Since you keep track of the number of moves taken, when the number of moves is even set the currentPlayer char to 'X' and when odd set the char to 'O'.
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Tic Tac Toe..Problem Switching Users
3 Dec, 2008 - 11:47 AM
Post #4

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 3,055



Thanked: 270 times
Dream Kudos: 50
Expert In: Nothing. Well, nothing relevant here anyway. ;)

My Contributions
Or, for ASCII table fun...
cpp
void changeplayer(char &currPlayer)
{
currPlayer += ('X' - 'O') * (currPlayer == 'X' ? -1 : 1);
}

User is offlineProfile CardPM
+Quote Post

Decipil
RE: Tic Tac Toe..Problem Switching Users
3 Dec, 2008 - 01:10 PM
Post #5

New D.I.C Head
*

Joined: 19 Nov, 2008
Posts: 41


My Contributions
Thank you very much, that is much appreciated....one more question.

I added that line and now it switches the part that says player 'X' or 'O', but when I play a turn it just puts a 'O', not quite sure what I need to place after that to make it work right. I'll post my code again just incase.

CODE


#include <iostream>

using namespace std;

class TicTacToe{
         private:
                char theBoard [3][3];
         public:
                TicTacToe(void);
                void playOneGame(void);
                void switchPlayer(char &);
                void showBoard(void);
                void postMove(int, int, char);
                char determineWinner(void);
};


int main (void)
{
        //test the class by playing one game
        TicTacToe Game1;
        Game1.playOneGame();
        
}

void TicTacToe::playOneGame(void){
      //start a game and play until someone wins or a draw occurs...
      const int MaxMoves = 9;
      char currentPlayer = 'O';
      int row = 0;
      int clmn = 0;
      char theWinner = ' ';
      int nmbrOfMoves = 0; //keep track of the number of moves max is 9

      do {
            switchPlayer(currentPlayer); //change player from x to o or vice versa                
            showBoard();            

            cout << "\n\nPlayer " << currentPlayer << endl; //get the players move

            postMove(row, clmn, currentPlayer); //post the move to the board      
            theWinner = determineWinner();  //see if anyone won the game
            nmbrOfMoves++;  //keep track of the number of moves

      } while ((theWinner == 'D') && (nmbrOfMoves < MaxMoves));

       showBoard(); //show the ending board

       if (theWinner != 'D')  //declare a winner
            cout << "\n\nThe Winner is player " << theWinner << endl;
       else
            cout << "\n\nThe Game was a Draw";
}

TicTacToe::TicTacToe(void)
{
          //intialize the array contents
    int i, j;

    for (i = 0; i < 3; i++)
        for ( j = 0; j < 3; j++) theBoard[i][j] = ' ';
        
}

void TicTacToe::switchPlayer(char &currentPlayer)
{
         //switches the current player
    
    currentPlayer = currentPlayer = 'O' ? 'X' : 'O';
        
    

}

void TicTacToe::showBoard()
{
        //displays the board
    void showBoard(void);
    {
        int t;
        for(t = 0; t < 3; t++)
        {
            printf (" %c | %c | %c ",theBoard[t][0],
                    theBoard[t][1], theBoard[t][2]);
            if (t != 2) printf("\n---|---|---\n");
        }
        printf ("\n");
    }
}

void TicTacToe::postMove(int row, int col, char value)
{
       //gets the users move and posts it to the board
    int x, y;

     printf("Enter Row (1,2,3),Column (1,2,3) for your move: ");
    scanf_s("%d%*c%d", &x, &y);

    x--, y--;

    if (theBoard [x][y] != ' ')
    {
        printf ("Invalid Move, Try Again.\n");
        postMove(1, 3, x);
    }
    else theBoard [x][y] = 'O';

}

char TicTacToe::determineWinner(void){
      //analyzes the board to see if there is a winner
      //returns a X, O indicating the winner
      //if the game is a draw then D is returned

      //check the rows
      for (int i = 0; i < 3; i++){
            if (theBoard[i][0] == theBoard[i][1]
                 && theBoard[i][1] == theBoard[i][2]
                 && theBoard[i][0] != ' '){
                 return theBoard[i][0];
            }
      }

      //check the clmns
      for (int i = 0; i < 3; i++){
             if (theBoard[0][i] == theBoard[1][i]
                   && theBoard[1][i] == theBoard[2][i]
                   && theBoard[0][i] != ' '){
                   return theBoard[0][i];
             }
       }

       //check the diagnals
       if (theBoard[0][0] == theBoard[1][1]
              && theBoard[1][1] == theBoard[2][2]
              && theBoard[0][0] != ' ') {
              return theBoard[0][0];
       }

       if (theBoard[2][0] == theBoard[1][1]
               && theBoard[1][1] == theBoard[0][2]
               && theBoard[2][0] != ' ') {
               return theBoard[2][0];
       }

       return 'D';
}


User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Tic Tac Toe..Problem Switching Users
3 Dec, 2008 - 01:16 PM
Post #6

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 3,055



Thanked: 270 times
Dream Kudos: 50
Expert In: Nothing. Well, nothing relevant here anyway. ;)

My Contributions
Bad copy/paste:
cpp
currentPlayer = currentPlayer = 'O' ? 'X' : 'O';

should be
cpp
currentPlayer = currentPlayer == 'O' ? 'X' : 'O';

User is offlineProfile CardPM
+Quote Post

Decipil
RE: Tic Tac Toe..Problem Switching Users
3 Dec, 2008 - 01:19 PM
Post #7

New D.I.C Head
*

Joined: 19 Nov, 2008
Posts: 41


My Contributions
Oh yeah that was my bad, but It still only places a 'O' on the board when I play a turn.
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Tic Tac Toe..Problem Switching Users
3 Dec, 2008 - 01:23 PM
Post #8

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 3,055



Thanked: 270 times
Dream Kudos: 50
Expert In: Nothing. Well, nothing relevant here anyway. ;)

My Contributions
Hmmm...this could be why:
cpp
if (theBoard [x][y] != ' ')
{
printf ("Invalid Move, Try Again.\n");
postMove(1, 3, x);
}
else
theBoard [x][y] = 'O';

User is offlineProfile CardPM
+Quote Post

Decipil
RE: Tic Tac Toe..Problem Switching Users
3 Dec, 2008 - 01:34 PM
Post #9

New D.I.C Head
*

Joined: 19 Nov, 2008
Posts: 41


My Contributions
Ok, I have added some code to the switch user part and it works....at first. the problem isn't with the turns anymore it is now that it is displaying either the wrong persons turn or it is not displaying it at all.

Here is the code...

CODE


#include <iostream>

using namespace std;

class TicTacToe{
         private:
                char theBoard [3][3];
         public:
                TicTacToe(void);
                void playOneGame(void);
                void switchPlayer(char &);
                void showBoard(void);
                void postMove(int, int, char);
                char determineWinner(void);
};


int main (void)
{
        //test the class by playing one game
        TicTacToe Game1;
        Game1.playOneGame();
        
}

void TicTacToe::playOneGame(void){
      //start a game and play until someone wins or a draw occurs...
      const int MaxMoves = 9;
      char currentPlayer = 'O';
      int row = 0;
      int clmn = 0;
      char theWinner = ' ';
      int nmbrOfMoves = 0; //keep track of the number of moves max is 9

      do {
            switchPlayer(currentPlayer); //change player from x to o or vice versa                
            showBoard();            

            cout << "\n\nPlayer " << currentPlayer << endl; //get the players move

            postMove(row, clmn, currentPlayer); //post the move to the board      
            theWinner = determineWinner();  //see if anyone won the game
            nmbrOfMoves++;  //keep track of the number of moves

      } while ((theWinner == 'D') && (nmbrOfMoves < MaxMoves));

       showBoard(); //show the ending board

       if (theWinner != 'D')  //declare a winner
            cout << "\n\nThe Winner is player " << theWinner << endl;
       else
            cout << "\n\nThe Game was a Draw";
}

TicTacToe::TicTacToe(void)
{
          //intialize the array contents
    int i, j;

    for (i = 0; i < 3; i++)
        for ( j = 0; j < 3; j++) theBoard[i][j] = ' ';
        
}

void TicTacToe::switchPlayer(char &currentPlayer)
{
         //switches the current player
    currentPlayer = currentPlayer == 'O' ? 'X' : 'O';

        void showBoard(void);
    {
        int t;
        for(t = 0; t < 3; t++)
        {
            printf (" %c | %c | %c ",theBoard[t][0],
                    theBoard[t][1], theBoard[t][2]);
            if (t != 2) printf("\n---|---|---\n");
        }
        printf ("\n");
    }
    

        int x, y;

     printf("Enter Row (1,2,3),Column (1,2,3) for your move: ");
    scanf_s("%d%*c%d", &x, &y);

    x--, y--;

    if (theBoard [x][y] != ' ')
    {
        printf ("Invalid Move, Try Again.\n");
        postMove(1, 3, x);
    }
    else theBoard [x][y] = 'O';
        
    

}

void TicTacToe::showBoard()
{
        //displays the board
    void showBoard(void);
    {
        int t;
        for(t = 0; t < 3; t++)
        {
            printf (" %c | %c | %c ",theBoard[t][0],
                    theBoard[t][1], theBoard[t][2]);
            if (t != 2) printf("\n---|---|---\n");
        }
        printf ("\n");
    }
}

void TicTacToe::postMove(int row, int col, char value)
{
       //gets the users move and posts it to the board
    int x, y;

     printf("Enter Row (1,2,3),Column (1,2,3) for your move: ");
    scanf_s("%d%*c%d", &x, &y);

    x--, y--;

    if (theBoard [x][y] != ' ')
    {
        printf ("Invalid Move, Try Again.\n");
        postMove(1, 3, x);
    }
    else theBoard [x][y] = 'X';

}

char TicTacToe::determineWinner(void){
      //analyzes the board to see if there is a winner
      //returns a X, O indicating the winner
      //if the game is a draw then D is returned

      //check the rows
      for (int i = 0; i < 3; i++){
            if (theBoard[i][0] == theBoard[i][1]
                 && theBoard[i][1] == theBoard[i][2]
                 && theBoard[i][0] != ' '){
                 return theBoard[i][0];
            }
      }

      //check the clmns
      for (int i = 0; i < 3; i++){
             if (theBoard[0][i] == theBoard[1][i]
                   && theBoard[1][i] == theBoard[2][i]
                   && theBoard[0][i] != ' '){
                   return theBoard[0][i];
             }
       }

       //check the diagnals
       if (theBoard[0][0] == theBoard[1][1]
              && theBoard[1][1] == theBoard[2][2]
              && theBoard[0][0] != ' ') {
              return theBoard[0][0];
       }

       if (theBoard[2][0] == theBoard[1][1]
               && theBoard[1][1] == theBoard[0][2]
               && theBoard[2][0] != ' ') {
               return theBoard[2][0];
       }

       return 'D';
}

User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Tic Tac Toe..Problem Switching Users
3 Dec, 2008 - 01:51 PM
Post #10

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 3,055



Thanked: 270 times
Dream Kudos: 50
Expert In: Nothing. Well, nothing relevant here anyway. ;)

My Contributions
Why do you have functions defined inside of methods? You are too busy cutting and pasting code randomly and it's completely screwing you up. Think about what you're doing.
User is offlineProfile CardPM
+Quote Post

Decipil
RE: Tic Tac Toe..Problem Switching Users
3 Dec, 2008 - 02:05 PM
Post #11

New D.I.C Head
*

Joined: 19 Nov, 2008
Posts: 41


My Contributions
Ok, well I wasnt cutting and pasting randomly I just simply took to much of the code, but now I have fixed that.
User is offlineProfile CardPM
+Quote Post

Decipil
RE: Tic Tac Toe..Problem Switching Users
3 Dec, 2008 - 02:47 PM
Post #12

New D.I.C Head
*

Joined: 19 Nov, 2008
Posts: 41


My Contributions
Well here is my problem, it shows that the player is switching but when a turn is taken it only shows an 'X', I have tried many things to fix this and some work but with other problems attached.

Here is the code..

CODE


#include <iostream>

using namespace std;

class TicTacToe{
         private:
                char theBoard [3][3];
         public:
                TicTacToe(void);
                void playOneGame(void);
                void switchPlayer(char &);
                void showBoard(void);
                void postMove(int, int, char);
                char determineWinner(void);
};


int main (void)
{
        //test the class by playing one game
        TicTacToe Game1;
        Game1.playOneGame();
        
}

void TicTacToe::playOneGame(void){
      //start a game and play until someone wins or a draw occurs...
      const int MaxMoves = 9;
      char currentPlayer = 'O';
      int row = 0;
      int clmn = 0;
      char theWinner = ' ';
      int nmbrOfMoves = 0; //keep track of the number of moves max is 9

      do {
            switchPlayer(currentPlayer); //change player from x to o or vice versa                
            showBoard();            

            cout << "\n\nPlayer " << currentPlayer << endl; //get the players move

            postMove(row, clmn, currentPlayer); //post the move to the board      
            theWinner = determineWinner();  //see if anyone won the game
            nmbrOfMoves++;  //keep track of the number of moves

      } while ((theWinner == 'D') && (nmbrOfMoves < MaxMoves));

       showBoard(); //show the ending board

       if (theWinner != 'D')  //declare a winner
            cout << "\n\nThe Winner is player " << theWinner << endl;
       else
            cout << "\n\nThe Game was a Draw";
}

TicTacToe::TicTacToe(void)
{
          //intialize the array contents
    int i, j;

    for (i = 0; i < 3; i++)
        for ( j = 0; j < 3; j++) theBoard[i][j] = ' ';
        
}

void TicTacToe::switchPlayer(char &currentPlayer)
{
         //switches the current player
                 //change player from x to o or vice versa                
    showBoard();
    currentPlayer = currentPlayer == 'O' ? 'X' : 'O';

    
}

void TicTacToe::showBoard()
{
        //displays the board
    
    {
        int t;
        for(t = 0; t < 3; t++)
        {
            printf (" %c | %c | %c ",theBoard[t][0],
                    theBoard[t][1], theBoard[t][2]);
            if (t != 2) printf("\n---|---|---\n");
        }
        printf ("\n");
    }
}

void TicTacToe::postMove(int row, int col, char value)
{
       //gets the users move and posts it to the board
    int x, y;

     printf("Enter Row (1,2,3),Column (1,2,3) for your move: ");
    scanf_s("%d%*c%d", &x, &y);

    x--, y--;

    if (theBoard [x][y] != ' ')
    {
        printf ("Invalid Move, Try Again.\n");
        postMove(1, 3, x);
    }
    else theBoard [x][y] = 'X';
    
}


char TicTacToe::determineWinner(void){
      //analyzes the board to see if there is a winner
      //returns a X, O indicating the winner
      //if the game is a draw then D is returned

      //check the rows
      for (int i = 0; i < 3; i++){
            if (theBoard[i][0] == theBoard[i][1]
                 && theBoard[i][1] == theBoard[i][2]
                 && theBoard[i][0] != ' '){
                 return theBoard[i][0];
            }
      }

      //check the clmns
      for (int i = 0; i < 3; i++){
             if (theBoard[0][i] == theBoard[1][i]
                   && theBoard[1][i] == theBoard[2][i]
                   && theBoard[0][i] != ' '){
                   return theBoard[0][i];
             }
       }

       //check the diagnals
       if (theBoard[0][0] == theBoard[1][1]
              && theBoard[1][1] == theBoard[2][2]
              && theBoard[0][0] != ' ') {
              return theBoard[0][0];
       }

       if (theBoard[2][0] == theBoard[1][1]
               && theBoard[1][1] == theBoard[0][2]
               && theBoard[2][0] != ' ') {
               return theBoard[2][0];
       }

       return 'D';
}


Thank your for the help.
User is offlineProfile CardPM
+Quote Post

reaper7861
RE: Tic Tac Toe..Problem Switching Users
4 Dec, 2008 - 12:43 PM
Post #13

New D.I.C Head
*

Joined: 17 Nov, 2008
Posts: 30

Hope this helps you. :rockon:

CODE


void TicTacToe::switchPlayer(char &currentPlayer)
{
        if(currentPlayer == 'O')//the if statements to switch every other move
         currentPlayer = 'X';
        else
            if(currentPlayer == 'X')
                currentPlayer = 'O';

}


void TicTacToe::postMove(int row, int col, char value)
{
       //gets the users move and posts it to the board
    int x, y;

     printf("Enter Row (1,2,3),Column (1,2,3) for your move: ");
    scanf_s("%d%*c%d", &x, &y);

    x--, y--;

    if (theBoard [x][y] != ' ')
    {
        printf ("Invalid Move, Try Again.\n");
        postMove(1, 3, x);
    }
    else theBoard [x][y] = value;//this needed to be value to change back and forth

}




User is offlineProfile CardPM
+Quote Post

Decipil
RE: Tic Tac Toe..Problem Switching Users
4 Dec, 2008 - 12:49 PM
Post #14

New D.I.C Head
*

Joined: 19 Nov, 2008
Posts: 41


My Contributions
You are a Freaking genius, that worked perfect, thanks a lot. And you keep Rockin!
User is offlineProfile CardPM
+Quote Post

reaper7861
RE: Tic Tac Toe..Problem Switching Users
4 Dec, 2008 - 12:52 PM
Post #15

New D.I.C Head
*

Joined: 17 Nov, 2008
Posts: 30

Thanks bro and talk to you later, dont forget to pass on the help to those in need.
User is offlineProfile CardPM
+Quote Post

Decipil
RE: Tic Tac Toe..Problem Switching Users
4 Dec, 2008 - 12:54 PM
Post #16

New D.I.C Head
*

Joined: 19 Nov, 2008
Posts: 41


My Contributions
Will do Broham.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 03:58PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month