#include <iostream>
#include <stdexcept>
#include <bits/stdc++.h>
using namespace std;
#define BOARDSIZE 3
/*
| | | | | ==> presenceStat is 2*4 => one row for each player
| | | | | ==> 1st col - row, 2nd-col, 3rd-principal diagnal, 4th - secondary diagnal
*/
class TicTacToe
{
private:
int player;
vector< vector < int > > presenceStat;
vector< vector < char > > board;
public:
TicTacToe() : player(1), presenceStat(2, vector<int>(4, 0)),
board(BOARDSIZE, vector<char>(BOARDSIZE, ' ')) {}
void printPresenceStat()
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
cout << presenceStat[i][j] << " ";
}
cout << endl;
}
}
void makeMove(int row, int col, int player)
{
cout << "makeMove " << row << " " << col << " " << player << endl;
// try
// {
if (row < 0 || row >= BOARDSIZE || col < 0 || col >= BOARDSIZE || board[row][col] != ' ')
{
// throw runtime_exception("Invalid move");
}
int rowIdx = player-1;
bool isWin = false;
board[row][col] = rowIdx == 0 ? 'X' : 'O';
((presenceStat[rowIdx][0] |= (1 << row)) != 7) ? : isWin = true ;
((presenceStat[rowIdx][1] |= (1 << col)) != 7) ? : isWin = true ;
if (row == col)
((presenceStat[rowIdx][2] |= (1 << row)) != 7) ? : isWin = true ;
if (row + col == BOARDSIZE-1)
((presenceStat[rowIdx][3] |= (1 << row)) != 7) ? : isWin = true ;
printPresenceStat();
//check for win
if (isWin)
cout << player << " won the match" << endl;
player = 3-player;
/* }
catch(exception& e)
{
cout e.what() << endl;
} */
}
};
int main() {
TicTacToe* pGame = new TicTacToe();
pGame->makeMove(0, 0, 1);
pGame->makeMove(0, 1, 2);
pGame->makeMove(1, 0, 1);
pGame->makeMove(0, 2, 2);
pGame->makeMove(2, 0, 1);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RkZXhjZXB0PgojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIEJPQVJEU0laRSAzCgovKgp8I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