This is what I have and having some problems with the checkPosn function. can anyone help me out. When I run the program and pick the posn it runs "posn already taken, please choose another. What I am doing wrong?

Melissa

#include <iostream>
using namespace std;
class ticTac
{
      private:
             int counter;
             char posn[9];
             char player;
      public:
             void nextPlayer();
             char getPlayer();
             void newBoard();
             void startGame();
             char checkWinner();
             void checkPosn(int spot);
             void currentBoard();
};

int main()
{
    char ch;
    ticTac Toe;
    do{
           Toe.newBoard();
           Toe.startGame();
           cout << "Would you like to play again (Enter 'y')? ";
           cin >> ch;
           }while (ch == 'y');
           
    return 0;    
}
void ticTac::newBoard()
{
          
     char posndef[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
     int i;
     counter = 0;
     player = 'x';
     for (i=0; i<9; i++) posn[i]=posndef[i];
     currentBoard();
     return;
     
};
void ticTac::currentBoard()
{
     cout << "\n\n" <<endl;
     cout << "\n\n" <<endl;
     cout << "\n\n\t\t" <<posn [0]<< "   | " <<posn [1]<< "  | " <<posn [2]<<endl;
     cout << " \t\t    |    |   " <<endl;
     cout << " \t\t ___|____|___ " <<endl;
     cout << "\n\n\t\t" <<posn [3]<< "   | " <<posn [4]<< "  | " <<posn [5]<<endl;
     cout << " \t\t    |    |   " <<endl;
     cout << " \t\t ___|____|___ " <<endl;
     cout << "\n\n\t\t" <<posn [6]<< "   | " <<posn [7]<< "  | " <<posn [8]<<endl;
     cout << " \t\t    |    |   " <<endl;
     cout << " \t\t    |    |   " <<endl;
     cout << "\n\n" <<endl;
     
};
void ticTac::startGame()
{
     int spot;
     char blank = ' ';
  
     cout << "Player " << getPlayer() <<" will go first and be the letter 'x'" <<endl;
        
     do {
         currentBoard();              // display current board   
              
         cout << "\n\n Player " << getPlayer() <<" choose a posn." <<endl;
         cin >> spot;
         
         do {
             checkPosn(spot);
             }while (posn[spot] != 'x' && posn[spot] != 'o');
             cout << "Nice move." <<endl;
         
         currentBoard();              // display current board   
          
         nextPlayer();  
         }while ( checkWinner() == blank );
              
};
     
char ticTac::checkWinner()
{
     char Winner = ' ';
     
     // Check if X wins   
     if (posn[1] == 'x' && posn[2] == 'x' && posn[3] == 'x') Winner = 'x';
     if (posn[4] == 'x' && posn[5] == 'x' && posn[6] == 'x') Winner = 'x';
     if (posn[7] == 'x' && posn[8] == 'x' && posn[9] == 'x') Winner = 'x';
     if (posn[1] == 'x' && posn[4] == 'x' && posn[7] == 'x') Winner = 'x';
     if (posn[2] == 'x' && posn[5] == 'x' && posn[8] == 'x') Winner = 'x';
     if (posn[3] == 'x' && posn[6] == 'x' && posn[9] == 'x') Winner = 'x';
     if (posn[1] == 'x' && posn[5] == 'x' && posn[9] == 'x') Winner = 'x';
     if (posn[3] == 'x' && posn[5] == 'x' && posn[7] == 'x') Winner = 'x';
     if (Winner == 'x' )  
       {cout << "Player1 wins the game." <<endl; return Winner; };
       
     // Check if O wins     
     if (posn[1] == 'o' && posn[2] == 'o' && posn[3] == 'o') Winner = 'o';
     if (posn[4] == 'o' && posn[5] == 'o' && posn[6] == 'o') Winner = 'o';
     if (posn[7] == 'o' && posn[8] == 'o' && posn[9] == 'o') Winner = 'o';
     if (posn[1] == 'o' && posn[4] == 'o' && posn[7] == 'o') Winner = 'o';
     if (posn[2] == 'o' && posn[5] == 'o' && posn[8] == 'o') Winner = 'o';
     if (posn[3] == 'o' && posn[6] == 'o' && posn[9] == 'o') Winner = 'o';
     if (posn[1] == 'o' && posn[5] == 'o' && posn[9] == 'o') Winner = 'o';
     if (posn[3] == 'o' && posn[5] == 'o' && posn[7] == 'o') Winner = 'o';
     if (Winner == 'o' )
       {cout << "Player2 wins the game." <<endl; return Winner; };
       
     // check for Tie
     
     return Winner;        
};
     
void ticTac::checkPosn(int spot)
{
          
     switch (spot)
     {
     case 1: (posn[1] == 'x' || posn[1] =='o'); cout << break;
     case 2: (posn[2] == 'x' || posn[2] =='o'); cout << break;
     case 3: (posn[3] == 'x' || posn[3] =='o'); cout << break;
     case 4: (posn[4] == 'x' || posn[4] =='o'); cout << break;
     case 5: (posn[5] == 'x' || posn[5] =='o'); cout << break;
     case 6: (posn[6] == 'x' || posn[6] =='o'); cout << break;
     case 7: (posn[7] == 'x' || posn[7] =='o'); cout << break;
     case 8: (posn[8] == 'x' || posn[8] =='o'); cout << break;
     case 9: (posn[9] == 'x' || posn[9] =='o'); cout << break;
     }
     cout << "That posn is already taken, please choose another." <<endl;
     counter++;
     
     return;
        
};
void ticTac::nextPlayer()
{
     if (player == 'x')
         player = 'o';
     else player = 'x';
     return;
};
         
char ticTac::getPlayer()
{
     return player;
};

You're forgetting that C/C++ arrays start at 0, not 1. So you're overrunning the array boundaries when you reference array[9], because that is in fact the 10th element.

You might do better to create a 2D array instead of a single dimension, as that is the shape of a Tic-Tac-Toe board...

[edit] Loops might also save you some time coding ;-)

Yes, you start using 0-8 but jump to 1-9. I'm not clear, in Checkposn what "cout << break" is supposed to do, but there's it's not going to get you past the cout line you're always seeing, right? It just stops the 'cascade' of the switch statement. Did you want that complaint cout to be the default for the switch?

You also will save a few steps - just check if its not a blank and then check what letter's there. Likewise, you can just ask if the 3 'win' positions are the same non-blank and, if so, say that letter wins.

switch (spot)
     {
     case 1: (posn[1] == 'x' || posn[1] =='o'); cout << break;
     case 2: (posn[2] == 'x' || posn[2] =='o'); cout << break;
     case 3: (posn[3] == 'x' || posn[3] =='o'); cout << break;
     case 4: (posn[4] == 'x' || posn[4] =='o'); cout << break;
     case 5: (posn[5] == 'x' || posn[5] =='o'); cout << break;
     case 6: (posn[6] == 'x' || posn[6] =='o'); cout << break;
     case 7: (posn[7] == 'x' || posn[7] =='o'); cout << break;
     case 8: (posn[8] == 'x' || posn[8] =='o'); cout << break;
     case 9: (posn[9] == 'x' || posn[9] =='o'); cout << break;
     }

This can be condensed to the following:

if (posn[spot] == 'x' || posn[spot] == 'o') {
    // spot is taken
}
else {
    // spot is empty
}

It would also be a good idea to validate the position given in the parameter...

There wasn't suposed to the cout << in the switch statement before the break. I forgot to delete that part. So, you are saying that I use an if statement instead of the switch statement.

Melissa

This TicTacToe game is my first and it has to be simple because I am just learning C++. I was able to fix the checkposn funtion, but now it won't go to the next player or input the 'x' or 'o'.

Here is what I have so far:

#include <iostream>
using namespace std;
class ticTac
{
      private:
             int counter;
             char posn[9];
             char player;
      public:
             void nextPlayer();
             char getPlayer();
             void newBoard();
             void startGame();
             char checkWinner();
             void checkPosn(int spot);
             void currentBoard();
};
 
int main()
{
    char ch;
    ticTac Toe;
    do{
           Toe.newBoard();         //Brings up the new board
           Toe.startGame();        //Starts the new game
           cout << "Would you like to play again (Enter 'y')? ";
           cin >> ch;
           }while (ch == 'y');
 
    return 0;    
}
//The new board before the game starts
void ticTac::newBoard()
{
 
     char posndef[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
     int i;
     counter = 0;
     player = 'x';
     for (i=0; i<9; i++) posn[i]=posndef[i];
     currentBoard();
     return;
 
};
//The board that each players play on
void ticTac::currentBoard()
{
     cout << "\n\n" <<endl;
     cout << "\n\n" <<endl;
     cout << "\n\n\t\t" <<posn [0]<< "   | " <<posn [1]<< "  | " <<posn [2]<<endl;
     cout << " \t\t    |    |   " <<endl;
     cout << " \t\t ___|____|___ " <<endl;
     cout << "\n\n\t\t" <<posn [3]<< "   | " <<posn [4]<< "  | " <<posn [5]<<endl;
     cout << " \t\t    |    |   " <<endl;
     cout << " \t\t ___|____|___ " <<endl;
     cout << "\n\n\t\t" <<posn [6]<< "   | " <<posn [7]<< "  | " <<posn [8]<<endl;
     cout << " \t\t    |    |   " <<endl;
     cout << " \t\t    |    |   " <<endl;
     cout << "\n\n" <<endl;
 
};
//Starts the game
void ticTac::startGame()
{
     int spot;
     char blank = ' ';
 
     cout << "Player " << getPlayer() <<" will go first and be the letter 'x'" <<endl;
 
     do {
         currentBoard();              // display current board   
 
         cout << "\n\n Player " << getPlayer() <<" choose a posn." <<endl;
         cin >> spot;
 
         do {
             checkPosn(spot);
             }while (posn[spot] != 'x' && posn[spot] != 'o');
             cout << "Nice move." <<endl;
 
         currentBoard();              // display current board   
 
         nextPlayer();  
         }while ( checkWinner() == blank );
 
         return;
 
};
//Checks to see who the winner will be     
char ticTac::checkWinner()
{
     char Winner = ' ';
 
     // Check if X wins   
     if (posn[1] == 'x' && posn[2] == 'x' && posn[3] == 'x') Winner = 'x';
     if (posn[4] == 'x' && posn[5] == 'x' && posn[6] == 'x') Winner = 'x';
     if (posn[7] == 'x' && posn[8] == 'x' && posn[9] == 'x') Winner = 'x';
     if (posn[1] == 'x' && posn[4] == 'x' && posn[7] == 'x') Winner = 'x';
     if (posn[2] == 'x' && posn[5] == 'x' && posn[8] == 'x') Winner = 'x';
     if (posn[3] == 'x' && posn[6] == 'x' && posn[9] == 'x') Winner = 'x';
     if (posn[1] == 'x' && posn[5] == 'x' && posn[9] == 'x') Winner = 'x';
     if (posn[3] == 'x' && posn[5] == 'x' && posn[7] == 'x') Winner = 'x';
     if (Winner == 'x' )  
       {cout << "Player1 wins the game." <<endl; return Winner; };
 
     // Check if O wins     
     if (posn[1] == 'o' && posn[2] == 'o' && posn[3] == 'o') Winner = 'o';
     if (posn[4] == 'o' && posn[5] == 'o' && posn[6] == 'o') Winner = 'o';
     if (posn[7] == 'o' && posn[8] == 'o' && posn[9] == 'o') Winner = 'o';
     if (posn[1] == 'o' && posn[4] == 'o' && posn[7] == 'o') Winner = 'o';
     if (posn[2] == 'o' && posn[5] == 'o' && posn[8] == 'o') Winner = 'o';
     if (posn[3] == 'o' && posn[6] == 'o' && posn[9] == 'o') Winner = 'o';
     if (posn[1] == 'o' && posn[5] == 'o' && posn[9] == 'o') Winner = 'o';
     if (posn[3] == 'o' && posn[5] == 'o' && posn[7] == 'o') Winner = 'o';
     if (Winner == 'o' )
       {cout << "Player2 wins the game." <<endl; return Winner; };
 
     // check for Tie
 
     return Winner;        
};
//Checks to see if the posn is taken     
void ticTac::checkPosn(int spot)
{
 
     if (posn[spot] == 'x' || posn[spot] == 'o') {
                  cout << "posn already taken, please choose another." <<endl;
                  }
                  else {
 
           };
 
     counter++;
 
     return;
 
};
//Tells the next player that it's thier turn
void ticTac::nextPlayer()
{
     if (player == 'x')
         player = 'o';
     else player = 'x';
     return;
};
//This switches between players         
char ticTac::getPlayer()
{
     return player;
};

Thanks for everyone that is helping me on this program. I am enjoying learning this language.

Melissa

By the way, I am using Bloodshed compiler for this program.

Melissa

>I was able to fix the checkposn funtion, but now it won't go to the next player or input the 'x' or 'o'.
That's because of 2 problems.

If you don't return anything from this function, how do you expect the other part of the code to know if the position is taken?

//Checks to see if the posn is taken     
void ticTac::checkPosn(int spot)
{
 
     if (posn[spot] == 'x' || posn[spot] == 'o') {
                  cout << "posn already taken, please choose another." <<endl;
                  }
                  else {
 
           };
 
     counter++;
 
     return;
};

I'd also advise against printing the error message in the function itself; the purpose of this function should be only to return the state of a given position. Let the function which invoked this function take care of the error messages.

Your second problem is here.

do {
             checkPosn(spot);
             }while (posn[spot] != 'x' && posn[spot] != 'o');

Just how do you expect 'posn' to contain a value when you never have any input here? Your while() loop is redundant too, because it checks the same thing that checkPosn does.

How about:

bool posTaken = true;
while (posTaken) {
    cout << "position is taken, please enter a valid space" << endl;
    cin >> spot; // not the prettiest thing to do,
                 // but this is just for demonstration
    posTaken = checkPosn(spot);
}

Thank you joeprogrammer,

I really glad that I can get help from this web site when I can't get it from the school.
Thanks for all the help.

Melissa

> I really glad that I can get help from this web site when I can't get it from the school.
No problem.

>Thanks for all the help.
You're welcome.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.