#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
void Print_now (std::vector<std::vector<char>>& square)
{
for (int i = 0; i != 3; ++i)
{
for (int j = 0; j != 3; ++j)
{
std::cout << square[i][j] << " ";
}
std::cout << "\n";
}
}
bool draw (std::vector<std::vector<bool>>& is_there)
{
for (int i = 0; i != 3; ++i)
{
for (int j = 0; j != 3; ++j)
{
if (!is_there[i][j])
{
return false;
}
}
}
return true;
}
bool Vruyr_wins (std::vector<std::vector<char>>& square)
{
int count = 0;
for (int i = 0; i != 3; ++i)
{
for (int j = 0; j != 3; ++j)
{
if (square[i][j] == 'O')
{
++count;
}
}
if (count == 3)
{
return true;
}
}
count = 0;
for (int i = 0; i != 3; ++i)
{
for (int j = 0; j != 3; ++j)
{
if (square[j][i] == 'O')
{
++count;
}
}
if (count == 3)
{
return true;
}
}
return false;
}
bool Player_wins (std::vector<std::vector<char>>& square)
{
int count = 0;
for (int i = 0; i != 3; ++i)
{
for (int j = 0; j != 3; ++j)
{
if (square[i][j] == 'X')
{
++count;
}
}
if (count == 3)
{
return true;
}
}
count = 0;
for (int i = 0; i != 3; ++i)
{
for (int j = 0; j != 3; ++j)
{
if (square[j][i] == 'X')
{
++count;
}
}
if (count == 3)
{
return true;
}
}
return false;
}
bool there_is_possibility_to_lose (std::vector<std::vector<char>>& square, std::vector<std::vector<bool>>& is_there)
{
int count = 0;
for (int i = 0; i != 3; ++i)
{
for (int j = 0; j != 3; ++j)
{
if (square[i][j] == 'X')
{
++count;
}
}
if(count == 2)
{
for(int j = 0; j != 3; ++j)
{
if ((square[i][j] != 'X') && (square[i][j] != 'O'))
{
square[i][j] = 'O';
return true;
}
}
}
}
count = 0;
for (int i = 0; i != 3; ++i)
{
for (int j = 0; j != 3; ++j)
{
if (square[j][i] == 'X')
{
++count;
}
}
if(count == 2)
{
for(int j = 0; j != 3; ++j)
{
if ((square[j][i] != 'X') && (square[j][i] != 'O'))
{
square[j][i] = 'O';
return true;
}
}
return true;
}
}
return false;
}
void Vruyrs_turn (std::vector<std::vector<char>>& square, std::vector<std::vector<bool>>& is_there)
{
if (there_is_possibility_to_lose(square, is_there))
{
return;
}
int com = rand()%9;
if (!is_there[(com-1)/3][(com-1)%3])
{
is_there[(com-1)/3][(com-1)%3] = true;
square[(com-1)/3][(com-1)%3] = 'O';
return;
}
else
{
Vruyrs_turn(square, is_there);
}
}
void Players_turn (std::vector<std::vector<char>>& square, std::vector<std::vector<bool>>& is_there)
{
int com;
std::cin >> com;
square[(com-1)/3][(com-1)%3] = 'X';
is_there[(com-1)/3][(com-1)%3] = true;
}
void start (std::vector<std::vector<char>>& square, std::vector<std::vector<bool>>& is_there)
{
if(draw(is_there))
{
std::cout << "It's Draw, try one more time";
return;
}
std::cout << "It's Your turn" << "\n";
Players_turn (square, is_there);
Print_now (square);
if(Player_wins(square))
{
std::cout << "You WIN!!!!";
return;
}
std::cout << "Vruyr's turn" << "\n";
Vruyrs_turn (square, is_there);
Print_now (square);
if(Vruyr_wins(square))
{
std::cout << "You Lose";
return;
}
start (square, is_there);
}
int main()
{
srand(time(nullptr));
std::vector<std::vector<char>> square (3, std::vector<char>(3, '*'));
std::vector<std::vector<bool>> is_there (3, std::vector<bool>(3, false));
std::cout << "Hi, You are playing with Vruyr" << "\n";
start (square, is_there);
}