Skip to content

Instantly share code, notes, and snippets.

@BlameOmar
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlameOmar/afaeb471c7a5e4107dc3 to your computer and use it in GitHub Desktop.
Save BlameOmar/afaeb471c7a5e4107dc3 to your computer and use it in GitHub Desktop.
TicTacToe (2010)
//Omar Evans
//TicTacToe Version 2
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#ifdef WIN32
#include <windows.h>
#define clear() system("cls")
#else
#include <unistd.h>
#define Sleep(X) usleep(1000 * X)
#define clear() system("clear")
#endif
using namespace std;
class TicTacToe {
private:
unsigned int mode, winner, p1Score, p2Score;
char board[3][3];
void menu();
void resetGame();
void display();
void p1Move();
void p2Move();
bool fill(int box, char mark);
int checkWinner();
public:
TicTacToe();
void run();
};
int main()
{
TicTacToe game;
cout << "TicTacToe 2.0" << endl << "Omar Evans" << endl << endl;
game.run();
return(0);
}
TicTacToe::TicTacToe()
{
srand((unsigned) time(NULL));
p1Score = 0;
p2Score = 0;
}
void TicTacToe::run()
{
menu();
while(mode) {
resetGame();
clear();
display();
while(!winner){
p1Move();
clear();
display();
if((winner = checkWinner())) continue;
p2Move();
clear();
display();
winner = checkWinner();
}
switch(winner) {
case 1:
p1Score++;
clear();
display();
cout << "Player 1 won!" << endl;
break;
case 2:
p2Score++;
clear();
display();
cout << "Player 2 won!" << endl;
break;
case 3:
cout << "It's a draw!" << endl;
break;
}
menu();
}
}
void TicTacToe::menu()
{
do {
cout << "Choose one of the following options:" << endl
<< "(1) Play against the computer" << endl
<< "(2) Play against another person" << endl
<< "(0) Quit" << endl
<< "Option: ";
cin >> mode;
} while(mode > 2);
}
void TicTacToe::resetGame()
{
int x, y;
winner = 0;
for(x = 0; x < 3; x++) {
for(y = 0; y < 3; y++) {
board[x][y] = ' ';
}
}
}
void TicTacToe::display()
{
int x, y;
cout << setfill('0')
<< "P1 " << setw(8) << p1Score
<< endl
<< "P2 " << setw(8) << p2Score
<< endl << endl;
for(x = 0; x < 3; x++) {
cout << setfill(' ')
<< 9 - 3 * x - 2 << setw(3) << "|"
<< 9 - 3 * x - 1 << setw(3) << "|"
<< 9 - 3 * x - 0 << endl;
for(y = 0; y < 3; y++) {
cout << setw(2) << board[x][y];
if(y < 2) cout << setw(2) << "|";
}
cout << endl;
if(x < 2) cout << setfill('_');
cout << setw(4) << "|" << setw(4) << "|" << setw(3) << "" << endl;
}
}
void TicTacToe::p1Move()
{
int box;
do {
cout << "Player 1: Choose an empty box: ";
cin >> box;
} while(!fill(box, 'X'));
}
void TicTacToe::p2Move()
{
int box;
do {
if(mode == 2) {
cout << "Player 2: Choose an empty box: ";
cin >> box;
} else {
box = rand() % 9 + 1;
}
} while(!fill(box, 'O'));
if(mode == 1) Sleep(500);
}
bool TicTacToe::fill(int box, char mark)
{
bool success = true;
int x, y;
if(1 <= box && box <= 9) {
x = 3 - (box + 2) / 3;
y = (box - 1) % 3;
if(board[x][y] == ' ') {
board[x][y] = mark;
} else {
if(mode != 1 || mark == 'X') cout << box << " is not an empty box." << endl;
success = false;
}
} else {
if(mode != 1 || mark == 'X') cout << box << " is not a valid box." << endl;
success = false;
}
return(success);
}
int TicTacToe::checkWinner()
{
int x,y, winner = 3; //Assumes a draw unless proven otherwise.
//If any box is empty, the game is assumed to continue, unless proven otherwise.
for(x = 0; x < 3 && winner == 3; x++) {
for(y = 0; y < 3 && winner == 3; y++) {
if(board[x][y] == ' ') winner = 0;
}
}
//Checks rows for a winner
for(x = 0; x < 3 && (winner != 1 && winner != 2); x++) {
if(board[x][0] == 'X' && board[x][0] == board[x][1] && board[x][1] == board[x][2])
winner = 1;
else if (board[x][0] == 'O' && board[x][0] == board[x][1] && board[x][1] == board[x][2])
winner = 2;
}
//Checks columns for a winner
for(y = 0; y < 3 && (winner != 1 && winner != 2); y++) {
if(board[0][y] == 'X' && board[0][y] == board[1][y] && board[1][y] == board[2][y])
winner = 1;
else if (board[0][y] == 'O' && board[0][y] == board[1][y] && board[1][y] == board[2][y])
winner = 2;
}
//Checks diagonals for a winner
if(winner != 1 && winner != 2) {
if(board[0][0] == 'X' && board[0][0] == board[1][1] && board[1][1] == board[2][2])
winner = 1;
else if(board[0][0] == 'O' && board[0][0] == board[1][1] && board[1][1] == board[2][2])
winner = 2;
else if(board[0][2] == 'X' && board[0][2] == board[1][1] && board[1][1] == board[2][0])
winner = 1;
else if(board[0][2] == 'O' && board[0][2] == board[1][1] && board[1][1] == board[2][0])
winner = 2;
}
return(winner);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment