Code:
#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 askYesNo(string question);
int askNumber(string question, int high, int low = 0);
char humanPiece();
char opponent(char piece);
void displayBoard( vector<char>& board);
char winner( vector<char>& board);
bool isLegal( vector<char>& board, int move);
int humanMove( vector<char>& board, char human);
int computerMove(vector<char> board, char computer);
void announceWinner(char winner, char computer, char human);
// main function
int main()
{
int move;
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);
system("pause");
return 0;
}
// functions
void instructions()
{
cout << " Get a sum of 15 with atleast 3 squares first " << endl;
cout << " 4 | 9 | 2\n";
cout << " ---------\n";
cout << " 3 | 5 | 7\n";
cout << " ---------\n";
cout << " 8 | 1 | 6\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+1 || number < low);
return number;
}
char humanPiece()
{
char go_first = askYesNo("Would you like to go first?");
if (go_first == 'y')
{
cout << "\nYou go first." <<endl;
return X;
}
else
{
cout << "\n Then I'll start." <<endl;
return O;
}
}
char opponent(char piece)
{
if (piece == X)
return O;
else
return X;
}
void displayBoard( vector<char>& board)
{
cout << "012345678" <<endl;
cout << board[0] << board[1] << board[2]<< board[3] << board[4] << board[5]<< board[6] << board[7] << board[8];
cout << "\n\n";
}
char winner( vector<char>& board)
{
// all possible winning rows
int WINNING_ROWS[8][3] ={
{4,9,2},
{3,5,7},
{8,1,6},
{4,3,8},
{9,5,1},
{2,7,6},
{4,5,6},
{2,5,8}};
int TOTAL_ROWS = 8;
// if any winning row has three values that are the same (and not EMPTY),
// then we have a 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]];
}
}
// since nobody has won, check for a tie (no empty squares left)
if (count(board.begin(), board.end(), EMPTY) == 0)
return TIE;
// since nobody has won and it isn't a tie, the game ain't over
return NO_ONE;
}
bool isLegal(int move, vector<char>& board)
{
return (board[move] == EMPTY);
}
int humanMove( vector<char>& board, char human)
{
int move = askNumber("Where will you move?", (board.size() - 1));
while (!isLegal(move, board))
{
cout << "\nSquare is already used" << endl;
move = askNumber("Where will you move?", (board.size() - 1));
}
return move;
}
int computerMove(vector<char> board, char computer)
{
cout << "I'll take number: ";
// if computer can win on next move, make that 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;
}
// done checking this move, undo it
board[move] = EMPTY;
}
}
// if human can win on next move, block that 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;
}
// done checking this move, undo it
board[move] = EMPTY;
}
}
// the best moves to make, in order
int BEST_MOVES[] = {5,4,2,8,6,9,3,7,1};
// since no one can win on next move, pick 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;
}
}
}
void announceWinner(char winner, char computer, char human)
{
if (winner == computer)
{
cout << "You lost!" << endl;
}
else if (winner == human)
{
cout <<"You won." <<endl;
}
else
{
cout << "Tie." <<endl;
}
}
