Skip to content

Instantly share code, notes, and snippets.

@vinzdef
Created May 16, 2015 00:07
Show Gist options
  • Save vinzdef/803185003ab1725a619d to your computer and use it in GitHub Desktop.
Save vinzdef/803185003ab1725a619d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
const char X = 'X', O = 'O', EMPTY = ' ', TIE = 'T', NO_ONE = 'N';
using namespace std;
void displayInstructions();
bool getConfirm(string question);
int getMove(string question, int high, int low);
char humanPiece();
void displayBoard(const vector<char> &board);
char winner(const vector<char>& board);
bool isLegal(const vector<char> &board, int move);
int humansMove(const vector<char> &board, char human);
int computersMove(vector<char> board, char computer);
void announceWinner(char winner, char computer, char human);
int main()
{
int move;
vector<char> board (9, EMPTY);
displayInstructions();
char human = humanPiece();
char computer;
char turn = X;
if (human == X)
computer = O;
else computer = X;
displayBoard(board);
while (winner(board) == NO_ONE){
if (turn == computer){
move = computersMove(board, computer);
board[move] = computer;
}
else{
move = humansMove(board, human);
board[move] = human;
}
displayBoard(board);
if (turn == computer) turn = human; else turn = computer;
}
announceWinner(winner(board), computer, human);
return 0;
}
void displayInstructions(){
cout << "Welcome to Tic-Tac-Toe.\n";
cout << "Make your move known by entering a number, 0 - 8. The number\n";
cout << " 0 | 1 | 2\n";
cout << " ----------\n";
cout << " 3 | 4 | 5\n";
cout << " ----------\n";
cout << " 6 | 7 | 8\n\n";
}
bool getConfirm(string question){
string response;
do{
cout << question << " [y/n] ";
cin >> response;
} while (response != "y" && response != "n");
if (response == "y")
return true;
return false;
}
int getMove(string question, int high, int low){
int number;
cout << question << " ( " << low << " - " << high << " ): ";
cin >> number;
while (number > high || number < low){
cout << "Sorry, out of range. Retry: ";
cin >> number;
}
return number;
}
char humanPiece(){
if (getConfirm("Do you want to go first? \n"))
return X;
return O;
}
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];
cout << "\n\n";
}
char winner(const vector<char> &board){
const int WINNING_MOVES[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}};
for (int i = 0; i < 8; ++i)
if (board[WINNING_MOVES[i][0]] != EMPTY)
if ((board[WINNING_MOVES[i][0]] == board[WINNING_MOVES[i][1]]) && (board[WINNING_MOVES[i][1]] == board[WINNING_MOVES[i][2]]))
return board[WINNING_MOVES[i][0]];
if (count(board.begin(), board.end(), EMPTY) == 0)
return TIE;
return NO_ONE;
}
int humansMove(const vector<char> &board, char human){
int move = getMove("\nWhat's your next move?", board.size()-1, 0);
while (board[move] != EMPTY)
move = getMove("\nThat's already occupied, choose an another cell.", board.size()-1, 0);
return move;
}
int computersMove(vector<char> board, char computer){
cout << "\nMy Turn\n";
//Check for a winning move
for (int i = 0; i < board.size(); ++i){
if (board[i] == EMPTY){
board[i] = computer;
if (winner(board) == computer)
return i;
board[i] = EMPTY;
}
}
char human;
if (computer == X) human = O; else human = X;
//Check for an opponent's possibility of win
for (int i = 0; i < board.size(); ++i){
if (board[i] == EMPTY){
board[i] = human;
if (winner(board) == human)
return i;
board[i] = EMPTY;
}
}
const int BEST_MOVES[] = {4, 0, 2, 6, 8, 1, 3, 5, 7};
//Else take the best available cell
for (int i = 0; i < 9; ++i)
if (board[BEST_MOVES[i]] == EMPTY)
return BEST_MOVES[i];
}
void announceWinner(char winner, char computer, char human)
{
if (winner == computer){
cout << "I won!\n";
cout << "As I predicted, human, I am triumphant once more - proof\n";
cout << "that computers are superior to humans in all regards.\n";
}
else if (winner == human){
cout << "You won...\n";
cout << "No, no! It cannot be! Somehow you tricked me, human.\n";
cout << "But never again! I, the computer, so swear it!\n";
}
else{
cout << "It's a tie.\n";
cout << "You were most lucky, human, and somehow managed to tie me.\n";
cout << "Celebrate... for this is the best you will ever achieve.\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment