Trouble with Tic-Tac-Toe

Hi everyone

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:

// COP3014 Summer 2010


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

// 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);

instructions();
char human = humanPiece();
char computer = opponent(human);
char turn = X;
displayBoard(&board);

while (winner(&board) == NO_ONE)
{
if (turn == human)
{
move = humanMove(&board, human);
board[move] = human;
}
else
{
move = computerMove(board, computer);
board[move] = computer;
}
displayBoard(&board);
turn = opponent(turn);
}

announceWinner(winner(&board), computer, human);

return 0;
}

// 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";


cout << "Get ready, Here we go!.\n\n";
}

char askYesNo(string question)
{
char response;
do
{
cout << question << " (y/n): ";
cin >> response;
} while (response != 'y' && response != 'n');

return response;
}

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

char opponent(char piece)
{
if (piece == X)
return O;
else
return X;
}

void displayBoard(const vector<char>* const pBoard)
{
cout << "\n\t" << (*pBoard)[0] << " | " << (*pBoard)[1] << " | " << (*pBoard)[2];
cout << "\n\t" << "---------";
cout << "\n\t" << (*pBoard)[3] << " | " << (*pBoard)[4] << " | " << (*pBoard)[5];
cout << "\n\t" << "---------";
cout << "\n\t" << (*pBoard)[6] << " | " << (*pBoard)[7] << " | " << (*pBoard)[8];
cout << "\n\n";
}

char winner(const vector<char>* const pBoard)
{
// Below I listed all of the possible winning row combinations.
const int WINNING_ROWS[8][3] = { {0, 1, 2},
{3, 4, 5},
{6, 7, 8},
{0, 3, 6},
{1, 4, 7},
{2, 5, 8},
{0, 4, 8},
{2, 4, 6} };
const int TOTAL_ROWS = 8;

for(int row = 0; row < TOTAL_ROWS; ++row)
{
if ( ((*pBoard)[WINNING_ROWS[row][0]] != EMPTY) &&
((*pBoard)[WINNING_ROWS[row][0]] == (*pBoard)[WINNING_ROWS[row][1]]) &&
((*pBoard)[WINNING_ROWS[row][1]] == (*pBoard)[WINNING_ROWS[row][2]]) )
{
return (*pBoard)[WINNING_ROWS[row][0]];
}
}

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

return NO_ONE;
}

inline bool isLegal(int move, const vector<char>* pBoard)
{
return ((*pBoard)[move] == EMPTY);
}

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

else
{
cout << "It's a tie.\n";
}
}

Thanks for any input.

Jason
Topic archived. No new replies allowed.