TicTacToe C++

Hi,
I have problem when I run the program and select the symbol and then select the spot to move into. tell me this spot already taken please if some one could help me. below my functions

///////////////////////// functions.cpp
<char AskForSymbol()
{
char usym;
cout << "Are ready to start??" << endl
<<"Please select your symblo (X Or O)" << endl;
cin >> usym;
return toupper(usym);
}



char ComputerSymblo(char player)
{
if (player == 'X')
{
return 'O';
}
else {
return 'X';
}

}


int AskPlayerMove()
{
int move;
bool isValid = false;
while (!isValid) {
cout << "Enter the number of spot you want to move into:"<< endl;
cin >> move;
if (move < 1 || move > 9) {
cout << "Invalid move " << move << " Number must be between 1 - 9" << endl;
}
else
{
isValid = true;
}
}
return move ;


}

int CalcComputerMove(char board[], char comp)
{
char plyr = comp == 'O' ? 'X' : 'O';

//Row Wins (0 1 2) or (3 4 5) or (6 7 8)
for (int i = 0; i < 9; i += 3) {
if ((board[i] == comp && board[i + 1] == comp) || (board[i] == comp && board[i + 2] == comp) || (board[i + 1] == comp && board[i + 2] == comp))
{
if (board[i] != comp && board[i] != plyr)
return static_cast<int>(board[i]) - 49;
else if (board[i + 1] != comp && board[i + 1] != plyr)
return static_cast<int>(board[i + 1]) - 49;
else if (board[i + 2] != comp && board[i + 2] != plyr)
return static_cast<int>(board[i + 2]) - 49;
}
}
//Column Wins (0 3 6) or (1 4 7) or (2 5 8)
for (int i = 0; i < 3; i++) {
if ((board[i] == comp && board[i + 3] == comp) || (board[i] == comp && board[i + 6] == comp) || (board[i + 3] == comp && board[i + 6] == comp))
{
if (board[i] != comp && board[i] != plyr)
return static_cast<int>(board[i]) - 49;
else if (board[i + 3] != comp && board[i + 3] != plyr)
return static_cast<int>(board[i + 3]) - 49;
else if (board[i + 6] != comp && board[i + 6] != plyr)
return static_cast<int>(board[i + 6]) - 49;
}
}
// Diagonally Wins(0 4 8) or (2 4 6)
if ((board[0] == comp && board[4] == comp) || (board[0] == comp && board[8] == comp) || (board[4] == comp && board[8] == comp))
{
if (board[0] == '1')
return 0;
else if (board[4] == '5')
return 4;
else if (board[8] == '9')
return 8;
}
if ((board[2] == comp && board[4] == comp) || (board[2] == comp && board[6] == comp) || (board[4] == comp && board[6] == comp))
{
if (board[2] == '3')
return 2;
else if (board[4] == '5')
return 4;
else if (board[6] == '7')
return 6;
}


//Check if Player Can Win
//Row Wins (0 1 2) or (3 4 5) or (6 7 8)
for (int i = 0; i < 9; i += 3) {
if ((board[i] == plyr && board[i + 1] == plyr) || (board[i] == plyr && board[i + 2] == plyr) || (board[i + 1] == plyr && board[i + 2] == plyr))
{
if (board[i] != plyr && board[i] != comp)
return static_cast<int>(board[i]) - 49;
else if (board[i + 1] != plyr && board[i + 1] != comp)
return static_cast<int>(board[i + 1]) - 49;
else if (board[i + 2] != plyr && board[i + 2] != comp)
return static_cast<int>(board[i + 2]) - 49;
}
}
//Column Wins (0 3 6) or (1 4 7) or (2 5 8)
for (int i = 0; i < 3; i++) {
if ((board[i] == plyr && board[i + 3] == plyr) || (board[i] == plyr && board[i + 6] == plyr) || (board[i + 3] == plyr && board[i + 6] == plyr))
{
if (board[i] != plyr && board[i] != comp)
return static_cast<int>(board[i]) - 49;
else if (board[i + 3] != plyr && board[i + 3] != comp)
return static_cast<int>(board[i + 3]) - 49;
else if (board[i + 6] != plyr && board[i + 6] != comp)
return static_cast<int>(board[i + 6]) - 49;
}
}
// Diagonally Wins(0 4 8) or (2 4 6)
if ((board[0] == plyr && board[4] == plyr) || (board[0] == plyr && board[8] == plyr) || (board[4] == plyr && board[8] == plyr))
{
if (board[0] == '1')
return 0;
else if (board[4] == '5')
return 4;
else if (board[8] == '9')
return 8;
}
else if ((board[2] == plyr && board[4] == plyr) || (board[2] == plyr && board[6] == plyr) || (board[4] == plyr && board[6] == plyr))
{
if (board[2] == '3')
return 2;
else if (board[4] == '5')
return 4;
else if (board[6] == '7')
return 6;
}


//All the rest best moves
if (board[4] == '5')
return 4;
else if (board[0] == '1')
return 0;
else if (board[2] == '3')
return 2;
else if (board[6] == '7')
return 6;
else if (board[8] == '9')
return 8;
else if (board[1] == '2')
return 1;
else if (board[3] == '4')
return 3;
else if (board[5] == '6')
return 5;
else if (board[7] == '8')
return 7;

return -1;
}



char WhoWon(char board[])
{

if (board[0] == board[1] && board[1] == board[2])
{
if (board[0] == 'X')
return 'X';
else
return 'O';
}
else if (board[3] == board[4] && board[4] == board[5])
if (board[3] == 'X')
return 'X';
else
return 'O';
else if (board[6] == board[7] && board[7] == board[8])
{
if (board[6] == 'X')
return 'X';
else
return 'O';
}
else if (board[0] == board[3] && board[3] == board[6])
{
if (board[0] == 'X')

return 'X';
else
return 'O';

}
else if (board[1] == board[4] && board[4] == board[7])
{
if (board[1] == 'X')
return 'X';
else
return 'O';
}
else if (board[2] == board[5] && board[5] == board[8])
{
if (board[2] == 'X')
return 'X';
else
return 'O';
}
else if (board[0] == board[4] && board[4] == board[8])
{
if (board[0] == 'X')
return 'X';
else
return 'O';
}
else if (board[2] == board[4] && board[2] == board[6])
{
if (board[2] == 'X')
return 'X';
else
return 'O';
}
return 0;
}


void AnnounceWinner(char winner, char player, char computer, int numPlys)
{

if (player == 1)
{
cout << " You the Winner Congratulations!!" << endl;
}
else if (computer == 1)
{
cout << "Computer the Winner!!" << endl;
}
else
{
cout << "It's a tie game!" << endl;
}

cout << "The numbers of play = " << numPlys << endl;
}


void DrawBoard(char board[])
{
cout << "\n\n\tTic Tac Toe\n\n";


cout << endl;

cout << " | | " << endl;
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;

cout << " | | " << endl << endl;

}

void RestBord(char board[])
{
board[0]= '1';
board[1]= '2';
board[2]= '3';
board[3]= '4';
board[4]= '5';
board[5]= '6';
board[6]= '7';
board[7]= '8';
board[8]= '9';


}
////////////////////////////driver.cpp

int main()
{
int pMove = 1;
int numplys, cMove;
char ans, pplayer, cplayer, winner;
char board[SIZE] = { '1','2','3','4','5','6','7','8','9' };

bool found = false;
WriteHeader();
WriteInstructions();

//Application Loop
do
{
RestBord(board);
numplys = 0;
pplayer = AskForSymbol();
//Validate choice
if (pplayer != 'X' && pplayer != 'O') {
cout << "Your symbol " << pplayer << " is invalid, must be (X or O)" << endl;
} else {
cplayer = ComputerSymblo(pplayer);

cout << "Great your enter valid" << endl
<< "let's Begin" << endl;

cout << "Your symbol: " << pplayer << endl;
cout << "The computer sympol: " << cplayer << endl;
DrawBoard(board);
//Game Loop
do
{
pMove = AskPlayerMove();
board[pMove] = pplayer;
//Check if Move is Valid
if (board[pMove] != 'X' && board[pMove] != 'O')
{
numplys++;
cMove = CalcComputerMove(board, cplayer);

board[cMove] = cplayer;
//check if board is full if not full then
DrawBoard(board);
winner = WhoWon(board);
if (winner != 0) {
break;
}

}

else
{
//Move in already taken
cout << "The Space " << pMove << " is Already taken by " << board[pMove] << " try another.\n";
}
} while (true);
AnnounceWinner(winner, pplayer, cplayer, numplys);

}


cout << "Do you want to play again";
cin >> ans;


} while (ans == 'y');

cout << << endl;




return 0;
}

>
Last edited on
missing headers, post a compilable example.
Last edited on
Topic archived. No new replies allowed.