I'm writing a tic-tac-toe program for a programing course that I'm enrolled in and everything has been smooth sailing so far except one lil thing. My problem is I cant seem to figure out why after a winner has been determined the program proceeds to close out instead of displaying the ending code I wrote. Keep in mind Iv been learning C++ for about a month now so I'm sure the problem is simple and obvious but any help would be appreciated. Here is my code tell me whatcha think:
// Below are the constants for the game
const char X = 'X';
const char O = 'O';
const char EMPTY = ' ';
const char TIE = 'T';
const char NO_ONE = 'N';
// Here are the functions that are used for the program
void instructions();
char askYesNo(string question);
int askNumber(string question, int high, int low = 0);
char humanPiece();
char opponent(char piece);
void displayBoard(const vector<char>* const pBoard);
char winner(const vector<char>* const pBoard);
bool isLegal(const vector<char>* const pBoard, int move);
int humanMove(const vector<char>* const pBoard, char human);
int computerMove(vector<char> board, char computer);
void announceWinner(char winner, char computer, char human);
int main()
{
int move;
const int NUM_SQUARES = 9;
vector<char> board(NUM_SQUARES, EMPTY);
// Here is the first function used followed by the start instructions.
void instructions()
{
cout << "Are you ready for some Tic-Tac-Toe action?\n";
cout << "To make your move you will enter a number 0 - 8. The number\n";
cout << "you enter corrisponds to a postion on the board as follows:\n";
cout << "0 will be on the top left and then from left to right the\n";
cout << "numbers follow in a desending order\n";
int askNumber(string question, int high, int low)
{
int number;
do
{
cout << question << " (" << low << " - " << high << "): ";
cin >> number;
} while (number > high || number < low);
return number;
}
char humanPiece()
{
char go_first = askYesNo("Would you like to go first, or are you feeling lucky?");
if (go_first == 'y')
{
cout << "\nNot feeling lucky I guess, By all means take the first move.\n";
return X;
}
else
{
cout << "\nHa ha ha, Thats was a mistake.\n";
return O;
}
}
// Below is how the program notices that all spaces are taken and a tie is announced
if (count(pBoard->begin(), pBoard->end(), EMPTY) == 0)
return TIE;
int humanMove(const vector<char>* const pBoard, char human)
{
int move = askNumber("Your turn.", (pBoard->size() - 1));
while (!isLegal(move, pBoard))
{
cout << "\nLook again that space is already taken.\n";
move = askNumber("Where will you move?", (pBoard->size() - 1));
}
cout << "Ok ok, not bad.\n";
return move;
}
int computerMove(vector<char> board, char computer)
{
// Here is the code for when the computer see a final winning move
for(int move = 0; move < board.size(); ++move)
{
if (isLegal(move, &board))
{
board[move] = computer;
if (winner(&board) == computer)
{
cout << move << endl;
return move;
}
board[move] = EMPTY;
}
}
// Here is the code for when the computer sees it needs to block its opponet's next move.
char human = opponent(computer);
for(int move = 0; move < board.size(); ++move)
{
if (isLegal(move, &board))
{
board[move] = human;
if (winner(&board) == human)
{
cout << move << endl;
return move;
}
board[move] = EMPTY;
}
}
// Here are the best moves the comp can make
const int BEST_MOVES[] = {4, 0, 2, 6, 8, 1, 3, 5, 7};
// If no one can win here is where the comp will make the next best move.
for(int i = 0; i < board.size(); ++i)
{
int move = BEST_MOVES[i];
if (isLegal(move, &board))
{
cout << move << endl;
return move;
}
}
}
// Finally this is the code for when a winner is determined
void announceWinner(char winner, char computer, char human)
{
if (winner == computer)
{
cout << "Man o man, looks like someone needs to step up their game!!\n";
}
else if (winner == human)
{
cout << winner << "'s won!\n";
cout << "What! You suck stupid human.\n";
}