Skip to content

Instantly share code, notes, and snippets.

@GnsP
Created November 16, 2015 17:41
Show Gist options
  • Save GnsP/f4565549b84b604db17c to your computer and use it in GitHub Desktop.
Save GnsP/f4565549b84b604db17c to your computer and use it in GitHub Desktop.
TicTacToe game written during my 1st year at NITR
#include <iostream>
#include <cctype>
using namespace std;
class board{
public:
board(){
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
mat[i][j] = ' '; // initially all cells are blank space
}
bool place(int row, int col, char sign){
if(sign != 'x' && sign != 'o'){
cout << sign << " is invalid" << endl;
return false; // return unsuccessful move
}
if(mat[row][col] != ' '){
cout << "Can't place " << sign << " at (" << row << "," << col << ")" << endl;
cout << "It already holds " << mat[row][col] << endl;
return false;
}
else{
mat[row][col] = sign;
displayBoard();
}
return true; // return success
}
void displayBoard(){
cout << endl;
cout <<'+';
for(int i=0; i<3; i++) cout << "---+";
cout << endl;
for(int i=0; i<3; i++){
cout << '|';
for(int j=0; j<3; j++){
cout << " " << mat[i][j] << " |";
}
cout << endl;
cout <<'+';
for(int i=0; i<3; i++) cout << "---+";
cout << endl;
}
cout << endl;
}
char checkWin(){
for(int i=0; i<3; i++){
if( mat[i][0] != ' ' && mat[i][0] == mat[i][1] && mat[i][0] == mat[i][2]) // check rows
return mat[i][0];
if( mat[0][i] != ' ' && mat[0][i] == mat[1][i] && mat[0][i] == mat[2][i]) // check cols
return mat[0][i];
}
if(mat[0][0] != ' ' && mat[0][0] == mat[1][1] && mat[0][0] == mat[2][2]) // check left diagonal
return mat[0][0];
if(mat[0][2] != ' ' && mat[0][2] == mat[1][1] && mat[0][2] == mat[2][0]) // check right diagonal
return mat[0][0];
for(int i=0; i<3; i++) // check if the board is full or not. If full and no win, then it's a draw.
for(int j=0; j<3; j++)
if(mat[i][j] == ' ')
return ' '; // return game running
return 'd'; // return draw
}
private:
char mat[3][3];
};
class game{
public:
game(){
cout << "RULES :" << endl;
cout << "1. Player-1 always moves first and gets to choose between o and x at the beginning." << endl;
cout << "2. Player-2 moves after player-1." << endl;
do{
cout << endl << "Player-1 choose your sign (x or o) :";
cin >> playerOneSign;
playerOneSign = tolower(playerOneSign);
} while(playerOneSign != 'x' && playerOneSign != 'o');
if(playerOneSign == 'x') playerTwoSign = 'o';
else playerTwoSign = 'x';
}
void run(){
int row, col;
int turn = 1;
char sign = playerOneSign;
cout << endl << "Game Begins ... " << endl << endl;
while(gameBoard.checkWin() == ' '){ // while game is running
do{
cout << "Player-" << turn << " move (enter <row><space><col>) :" ;
cin >> row >> col ;
} while(!gameBoard.place(row, col, sign));
if(turn == 1){
turn = 2;
sign = playerTwoSign;
}
else{
turn = 1;
sign = playerOneSign;
}
}
cout << endl << "---- GAME OVER ----" << endl;
char winner = gameBoard.checkWin();
if(winner == 'd'){
cout << endl << "it's a DRAW." << endl << endl;
return;
}
if(winner == playerOneSign)
cout << endl << "Player-1 WINS, Congrats." << endl;
else
cout << endl << "Player-1 WINS, Congrats." << endl;
}
private:
board gameBoard;
char playerOneSign;
char playerTwoSign;
};
int main(){
game myGame;
myGame.run();
return 0;
}
/* SAMPLE OUTPUT
RULES :
1. Player-1 always moves first and gets to choose between o and x at the beginning.
2. Player-2 moves after player-1.
Player-1 choose your sign (x or o) :x
Game Begins ...
Player-1 move (enter <row><space><col>) :0 0
+---+---+---+
| x | | |
+---+---+---+
| | | |
+---+---+---+
| | | |
+---+---+---+
Player-2 move (enter <row><space><col>) :0 0
Can't place o at (0,0)
It already holds x
Player-2 move (enter <row><space><col>) :0 1
+---+---+---+
| x | o | |
+---+---+---+
| | | |
+---+---+---+
| | | |
+---+---+---+
Player-1 move (enter <row><space><col>) :1 1
+---+---+---+
| x | o | |
+---+---+---+
| | x | |
+---+---+---+
| | | |
+---+---+---+
Player-2 move (enter <row><space><col>) :2 2
+---+---+---+
| x | o | |
+---+---+---+
| | x | |
+---+---+---+
| | | o |
+---+---+---+
Player-1 move (enter <row><space><col>) :2 0
+---+---+---+
| x | o | |
+---+---+---+
| | x | |
+---+---+---+
| x | | o |
+---+---+---+
Player-2 move (enter <row><space><col>) :1 0
+---+---+---+
| x | o | |
+---+---+---+
| o | x | |
+---+---+---+
| x | | o |
+---+---+---+
Player-1 move (enter <row><space><col>) :2 1
+---+---+---+
| x | o | |
+---+---+---+
| o | x | |
+---+---+---+
| x | x | o |
+---+---+---+
Player-2 move (enter <row><space><col>) :0 2
+---+---+---+
| x | o | o |
+---+---+---+
| o | x | |
+---+---+---+
| x | x | o |
+---+---+---+
Player-1 move (enter <row><space><col>) :1 2
+---+---+---+
| x | o | o |
+---+---+---+
| o | x | x |
+---+---+---+
| x | x | o |
+---+---+---+
---- GAME OVER ----
it's a DRAW.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment