Skip to content

Instantly share code, notes, and snippets.

@satyajeetkrjha
Created July 5, 2022 23:14
Show Gist options
  • Save satyajeetkrjha/b3a7dcdd8b5408a3de28162896f370b0 to your computer and use it in GitHub Desktop.
Save satyajeetkrjha/b3a7dcdd8b5408a3de28162896f370b0 to your computer and use it in GitHub Desktop.
Object Oriented Tic Tac Toe
#include <array>
#include <deque>
#include <iostream>
#include <optional>
#include <vector>
using namespace std;
class PlayingPiece {
public:
enum PieceType { X, O };
PlayingPiece() : pieceType(std::nullopt) {}
PlayingPiece(PieceType pieceType) : pieceType(pieceType) {}
std::optional<PieceType> get_pieceType() { return pieceType; }
char mark() {
if (!pieceType)
return ' ';
else if (pieceType == O)
return 'O';
else
return 'X';
}
bool operator!=(const PlayingPiece &P) const {
return pieceType != P.pieceType;
}
private:
std::optional<PieceType> pieceType;
};
class PlayingPieceX : public PlayingPiece {
public:
PlayingPieceX() : PlayingPiece(X) {}
};
class PlayingPieceO : public PlayingPiece {
public:
PlayingPieceO() : PlayingPiece(O){};
};
class Player {
string name;
PlayingPiece playingPiece;
public:
Player(string name, PlayingPiece playingPiece)
: name(name), playingPiece(playingPiece) {}
string getName() { return name; }
PlayingPiece getplayingpiece() { return this->playingPiece; }
void setPLayingPiece(PlayingPiece playingPiece1) {
this->playingPiece = playingPiece1;
}
};
class Board {
public:
bool addPiece(int row, int column, PlayingPiece playingPiece) {
// you could not place a piece over there
if (board[row][column].get_pieceType()) {
return false;
}
board[row][column] = playingPiece;
return true;
}
vector<pair<int, int>> getfreeCells() {
vector<pair<int, int>> freeCells;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (!board[i][j].get_pieceType()) {
// cout << "pushed "<< endl;
freeCells.push_back({i, j});
}
}
}
return freeCells;
}
void printBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << board[i][j].mark() << " ";
}
cout << "|";
}
cout << endl;
}
bool checkWinner(int row, int column, PlayingPiece playingPiece) {
bool rowmatch, columnmatch, diagonalmatch, antidiagonalmatch;
rowmatch = columnmatch = diagonalmatch = antidiagonalmatch = true;
// stuck here
for (int i = 0; i < 3; i++) {
// stuck here
if (!board[row][i].get_pieceType() || board[row][i] != playingPiece) {
rowmatch = false ;
}
}
for(int i=0;i<3;i++) {
if(board[i][column].get_pieceType()|| board[i][column]!= playingPiece) {
columnmatch = false;
}
}
//need to check diagonals
for(int i=0, j=0; i<3;i++,j++) {
if (board[i][j].get_pieceType() || board[i][j] != playingPiece) {
diagonalmatch = false;
}
}
//need to check anti-diagonals
for(int i=0, j=2; i<3;i++,j--) {
if (board[i][j].get_pieceType() || board[i][j]!=playingPiece) {
antidiagonalmatch = false;
}
}
return rowmatch || columnmatch || diagonalmatch || antidiagonalmatch ;
}
private:
std::array<std::array<PlayingPiece, 3>, 3>
board; // a 2d array of 3 cross 3 of type playingPiece
};
class TicToeGame {
Board gameboard;
deque<Player> players;
public:
void initializeGame() {
PlayingPieceX crosspiece;
PlayingPieceO noughtspiece;
Player p1("PLAYER JACK", crosspiece);
Player p2("PLAYER TOM", noughtspiece);
players.push_back(p1);
players.push_back(p2);
}
string startGame() {
bool flag = true;
while (flag) {
Player playerplayingnow = players.front();
players.pop_front();
vector<pair<int, int>> freecells = gameboard.getfreeCells();
if (freecells.size() == 0) {
return "tie";
}
cout << "Player :" << playerplayingnow.getName() << " "
<< "Enter Row and Column" << endl;
int row, column;
cin >> row >> column;
bool addedsucessfully =
gameboard.addPiece(row, column, playerplayingnow.getplayingpiece());
if (!addedsucessfully) {
cout << "Incorrect position" << endl;
players.push_front(playerplayingnow);
continue;
}
players.push_back(playerplayingnow);
bool iswinner = gameboard.checkWinner(row, column,
playerplayingnow.getplayingpiece());
if (iswinner) {
return playerplayingnow.getName();
}
}
}
};
int main() {
TicToeGame ticToeGame ;
ticToeGame.initializeGame();
cout <<ticToeGame.startGame() << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment