// This is a personal academic project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
#include <iostream>
#include<time.h>
using namespace std;
const int rows = 3;
const int cols = 3;
int field[rows][cols];
int x, y, turn;
string man = "человек", compucter = "компьютер";
void user_computer();
void user_user();
void computer_computer();
bool checkVictory();
void printField();
int main()
{
turn = 0;
x = y = 0;
setlocale(LC_ALL, "Russian");
srand(time(NULL));
int select = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
field[i][j] = 0;
}
}
bool winner = 0;
int swit = 0;
while (1)
{
if (swit == 9)
break;
cout << "режим игры:" << endl;
cout << "1)компьютер - компьютер" << endl;
cout << "2)компьютер - человек" << endl;
cout << "3)человек - человек" << endl;
swit = 0;
select = 0;
while (select == 0)
{
if (!(cin >> select))
{
cin.clear();
cin.ignore(65535, '\n');
}
}
switch (select)
{
case 1:
computer_computer();
main();
break;
case 2:
user_computer();
main();
break;
case 3:
user_user();
main();
break;
default:
cout << "неверный режим\nдля выхода введите 9" << endl;
cin >> swit;
break;
}
}
return 0;
}
void user_computer()
{
do
{
cout << "Ваш ход: ";
if (turn % 2 == 0)
{
cout << compucter;
do
{
x = 1 + rand() % 3;
y = 1 + rand() % 3;
} while (field[x - 1][y - 1] != 0);
field[x - 1][y - 1] = 1;
}
else
{
cout << man;
do
{
cout << endl << (turn == 0 ? man : compucter) << ", Введите свой ход - \n";
cout << endl << "Строка: ";
cin >> x;
cout << endl << "Столбец: ";
cin >> y;
if ((x >= 1 && x <= 3) && (y >= 1 && y <= 3) && (field[x - 1][y - 1] == 0))
break;
} while (true);
field[x - 1][y - 1] = 2;
}
printField();
if (checkVictory() == true)
break;
turn++;
} while (turn <= 9);
cout << endl << (turn % 2 == 0 ? compucter : man) << " Победил!\n\n";
}
void user_user()
{
do
{
cout << "Следующий ход: ";
if (turn % 2 == 0)
{
cout << man;
do
{
cout << endl << man << ", Введите свой ход - \n";
cout << endl << "Строка: ";
cin >> x;
cout << endl << "Столбец: ";
cin >> y;
if (
(x >= 1 && x <= 3)
&&
(y >= 1 && y <= 3)
&&
(field[x - 1][y - 1] == 0)
)
break;
} while (true);
field[x - 1][y - 1] = 1;
}
else
{
cout << man;
do
{
cout << endl << man << ", Введите свой ход - \n";
cout << endl << "Строка: ";
cin >> x;
cout << endl << "Столбец: ";
cin >> y;
if ((x >= 1 && x <= 3) && (y >= 1 && y <= 3) && (field[x - 1][y - 1] == 0))
break;
} while (true);
field[x - 1][y - 1] = 2;
}
printField();
if (checkVictory() == true)
break;
turn++;
} while (turn <= 9);
cout << endl << man << " Победил!\n\n";
}
void computer_computer()
{
do
{
cout << "Следующий ход: ";
if (turn % 2 == 0)
{
cout << compucter;
do
{
x = 1 + rand() % 3;
y = 1 + rand() % 3;
} while (field[x - 1][y - 1] != 0);
field[x - 1][y - 1] = 1;
}
else
{
cout << compucter;
do
{
x = 1 + rand() % 3;
y = 1 + rand() % 3;
} while (field[x - 1][y - 1] != 0);
field[x - 1][y - 1] = 2;
}
printField();
if (checkVictory() == true)
break;
turn++;
} while (turn <= 9);
cout << endl << compucter << " Победил!\n\n";
}
void printField()
{
cout << endl << " -----------------" << endl;
for (size_t i = 0; i < rows; i++)
{
for (size_t j = 0; j < cols; j++)
{
cout << "| " << (field[i][j] == 1 ? "X" : field[i][j] == 2 ? "O" : " ") << " ";
}
cout << "|" << endl << "|-----------------|" << endl;
}
}
bool checkVictory()
{
// проверка победы по строкам (по горизонтали)
for (size_t row = 0; row < rows; row++)
if (field[row][0] == field[row][1] && field[row][1] == field[row][2] && field[row][2] != 0)
return true;
// проверка победы по столбцам (по вертикали)
for (size_t column = 0; column < cols; column++)
if (field[0][column] == field[1][column] && field[1][column] == field[2][column] && field[2][column] != 0)
return true;
// проверка победы на главной диагонали
if (field[0][0] == field[1][1] && field[1][1] == field[2][2] && field[1][1] != 0)
return true;
// проверка победы на обратной диагонали
if (field[0][2] == field[1][1] && field[1][1] == field[2][0] && field[1][1] != 0)
return true;
return false;
}