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;
};