#include <iostream>
using namespace std;
int main() {
//constanting variabels
const int rows = 3, cols = 3;
int CHOSEN_ROW, CHOSEN_COLUMN, count = 0;
bool wasputted = false, isXwin = false, isOwin = false;
char matrix[rows][cols]{
'*','*','*',
'*','*','*',
'*','*','*'
};
//showing the matrix
for (size_t i = 0; i < rows; i++)
{
for (size_t j = 0; j < cols; j++)
{
cout << matrix[i][j] << "\t";
}
cout << endl;
}
//main game part
for (size_t i = 0; i < 9; i++)
{
//X putting while get free space
while (wasputted == false) {
cout << "Choose the row to put X(1-3) : ";
cin >> CHOSEN_ROW;
cout << "\nChoose the colunm X(1-3) : ";
cin >> CHOSEN_COLUMN;
//if chosen place is free
if (matrix[CHOSEN_ROW - 1][CHOSEN_COLUMN - 1] == '*') {
matrix[CHOSEN_ROW - 1][CHOSEN_COLUMN - 1] = 'X';
wasputted = true;
count++;
}
//if chosen place is used
else {
cout << "This place was used" << endl;
if (count == 9) {
break;
}
else {
continue;
}
}
}
//zero the bool variabel
wasputted = false;
//showing the matrix
for (size_t i = 0; i < rows; i++)
{
for (size_t j = 0; j < cols; j++)
{
cout << matrix[i][j] << "\t";
}
cout << endl;
}
//O putting while get free space
while (wasputted == false) {
cout << "Choose the row to put O(1-3) : ";
cin >> CHOSEN_ROW;
cout << "\nChoose the colunm O(1-3) : ";
cin >> CHOSEN_COLUMN;
//if chosen place is free
if (matrix[CHOSEN_ROW - 1][CHOSEN_COLUMN - 1] == '*') {
matrix[CHOSEN_ROW - 1][CHOSEN_COLUMN - 1] = 'O';
wasputted = true;
count = count + 1;
}
//if chosen place is used
else {
cout << "This place was used" << endl;
if (count == 9) {
break;
}
}
}
if (count == 9) {
break;
}
wasputted = false;
//showing the matrix
for (size_t i = 0; i < rows; i++)
{
for (size_t j = 0; j < cols; j++)
{
cout << matrix[i][j] << "\t";
}
cout << endl;
}
//checking matrix for free places
for (size_t i = 0; i < rows; i++)
{
for (size_t j = 0; j < cols; j++)
{
if (matrix[i][j] == '*')
count = count + 1;
else {}
}
}
}
}
#include<iostream>
#include<array>
#include<iomanip>
#include<Windows.h>
using namespace std;
int main()
{
srand(unsigned(time(0)));
array<array<char, 3>, 3> places;
for (auto s = 0; s < 9; s++)
{
places[s / 3][s - ((s / 3) * 3)] = '*';
}
bool end = 0; unsigned u1, u2; short f = 1; char g = 'a';
while (!end)
{
for (const auto& c : places)
{
for (const auto& x : c)
{
cout << setw(7) << x;
}
cout << endl;
}
cout << endl;
bool k = 0;
do
{
cout << "Enter coordinates:" << endl;
cout << "X: "; cin >> u1;
cout << "Y: "; cin >> u2;
if (!(u1 < 0 || u2 < 0 || u1>2 || u2>2))
{
if (f == 1 && places[u1][u2] == 'O')
{
k = 1; cout << "invalid input";
}
if (f == 0 && places[u1][u2] == 'X')
{
k = 1; cout << "invalid input";
}
}
else
{
k = 1; cout << "invalid input"; u1 = 0; u2 = 0;
}
Sleep(750);
system("cls");
} while (k);
if (f == 1)
{
places[u1][u2] = 'X';
f = 0;
}
else
{
places[u1][u2] = 'O';
f = 1;
}
if (g > 'e')
{
unsigned cr = 0, zer = 0;
for (const auto& c : places)
{
for (const auto& x : c)
{
if (x == 'O')
{
zer++;
}
if (x == 'X')
{
cr++;
}
}
if (cr == 3 || zer == 3)
{
end = 1;
break;
}
else
{
cr = 0; zer = 0;
}
}
for (auto s = 0; s < 3; s++)
{
for (auto x = 0; x < 3; x++)
{
if (places[x][s] == 'O')
{
zer++;
}
if (places[x][s] == 'X')
{
cr++;
}
}
if (cr == 3 || zer == 3)
{
end = 1;
break;
}
else
{
cr = 0; zer = 0;
}
}
for (auto s = 0; s < 3; s++)
{
if (places[s][s] == 'X')
{
cr++;
}
if (places[s][s] == 'O')
{
zer++;
}
}
if (cr == 3 || zer == 3)
{
end = 1;
break;
}
else
{
cr = 0; zer = 0;
}
for (auto s = 0; s < 3; s++)
{
if (places[s][2 - s] == 'X')
{
cr++;
}
if (places[s][2 - s] == 'O')
{
zer++;
}
}
if (cr == 3 || zer == 3)
{
end = 1;
break;
}
else
{
cr = 0; zer = 0;
}
}
if (g == 'i')
{
cout << "withdraw";
return 0;
}
system("cls");
g++;
}
system("cls");
for (const auto& c : places)
{
for (const auto& x : c)
{
cout << setw(7) << x;
}
cout << endl;
}
Sleep(1000);
system("cls");
cout << "Game over";
return 0;
}