Code:
//tic-tac-toe
//plays tic-tac-toe against a human
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
//Global Constants
const char X = 'X';
const char O = 'O';
const char EMPTY = ' ';
const char TIE = 't';
const char NO_ONE = 'N';
//Function Prototypes
void instructions();
char adkYesNo(string question);
int askNumber(string question, int high, int low);
char humanPeice();
char opponent(char peice);
void displayBoard(const vector<char>& board);
char winner(const vector<char>& board);
bool isLegal(const vector<char>& board, int move);
int humanMove(const vector<char>& board, char human);
int computerMove(const vector<char>& board, char computer);
void announceWinner(char winner, char human, char computer);
//Main Function
int main()
{
int move;
const int NUM_SQUARES=9;
vector<char> board(NUM_SQUARES, EMPTY);
instructions();
char human = humanPeice();
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);
cout << "\n\n Hit enter to exit";
std::cin.ignore(std::cin.rdbuf()->in_avail()+1);
return 0;
}
//Instructions to Player
void instructions()
{
//Welcome Message
cout<<"WELCOME -- TO THE ULTIMATE MAN MACHINE SHOWDOWN!!!\n";
cout<<"Whare human mind is pit against silicon processor!\n";
cout<<" In the ultimate game of Tic-Tac-Toe!\n\n";
//Taunt
cout<<"Make your pathetic moves known, human, by entering the\n";
cout<<"numbers 1-8 as it lines up with each space on the board\n";
cout<<"as shown below...\n\n";
//Show player how to make a move
cout<<" | 0 | 1 | 2 |\n";
cout<<" -------------\n";
cout<<" | 3 | 4 | 5 |\n";
cout<<" -------------\n";
cout<<" | 6 | 7 | 8 |\n\n";
//Taunt some more \ Invite to play...
cout<<"Now if your tiny mind got all that, then prepare to lose!\n\n";
}
//Ask Yes\No question until player responds yes or no
char askYesNo(string question)
{
char response;
do
{
cout<<question<<" (y/n): ";
cin >> response;
}while (response != 'y' && response != 'n')
return response;
}
//Asks for the number between 1-8
int askNumber(string question, int high, int low)
{
int number
do
{
cout<<question<<" ( "<<high<<" - "<<low<<" ):";
cin>>number;
}while (number > high || number < low);
return number;
}
//Human Peice
char humanPeice()
{
char go_first = askYesNo ("Do you require the first move?");
if (go_first == 'y')
{
cout<<"Then take the first move, you will desperatly need it...\n";
return X;
}
else
{
cout<<"Your Bravory, or stupidity will be your undoing. I will go first!\n";
return O;
}
}
// Computer Peice
char opponent (char peice)
{
if (peice == O)
return O;
else
return X;
}
//Display the board
void displayBoard(const vector<char>& board)
{
cout << "\n\t"<< board[0] <<" | "<< board[1] <<" | "<< board[2];
cout << "\n\t-------------";
cout << "\n\t"<< board[3] <<" | "<< board[4] <<" | "<< board[5];
cout << "\n\t-------------";
cout << "\n\t"<< board[6] <<" | "<< board[7] <<" | "<< board[8];
}
//Get a winner
char winner(const vector<char>& board)
{
//all possible winning rows
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;
//check for winner
for(int row = 0; row > TOTAL_ROWS; ++row)
{
if ( (board[WINNING_ROWS[row][0]] != EMPTY)&&
(board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]])&&
(board[WINNING_ROWS[row][1]] == board[WINNING_ROWS[row][2]]) )
{
return board[WINNING_ROWS[row][0]]
}
}
//If no winner then check for a tie
if(count(board.begin(),board.end,EMPTY)==0);
return TIE;
//since no one has won or tied then the game goes on.
return NO_ONE;
}
//makes shure they move to an empty square.
inline bool isLegal(int move,const vector <char>& board)
{
return (board[move] == EMPTY);
}
// player makes a move
int humanMove(const vector<char>& board)
{
int move = askNumber("wich square do you desire: ",(board.size()-1));
while (!isLegal(move,board))
{
cout<<"That square is already occupied, foolish human!\n";
move = askNumber("wich square do you desire: ",(board.size()-1));
}
cout<<"\nfine...\n";
return move;
}
// the computer's move
int computerMove(vector<char> board, char computer)
{
cout<<"I shall take square number ";
//if computer can win on next move then take it.
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;
}
}
//if human can win next move block it.
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;
}
}
//The best moves to make in order
const int BEST_MOVES[] = {4,0,2,6,8,1,3,5,7};
//since no one can win take the best open square
for(int i = 0; i < board.size(); ++i)
{
int move = BEST_MOVES[i];
if(isLegal(move, board)
{
cout<<move<<endl;
return move;
}
}
}
//The winner
void announceWinner(char winner, char human, char computer)
{
if (winner == computer)
{
cout<<winner<<"\nwin's!\n";
cout<<"this just goes to show that computers are supeior!";
}
else if (winner == human)
{
cout<<"\nAnk the winner is... "<<winner<<"!!!\n";
cout<<"somehow you tricked me, this is the last time you will ever...";
}
else
{
cout<<"\nWe tied, Celebrate, for this is the closest you will ever get.";
}
}
also, the error happens under ask yes no at return response...