#include <cstdlib>
#include <iostream>
using namespace std;
char matrix[10] = {'-','-','-','-','-','-','-','-','-'};
int DisplayMatrix()
{
cout << matrix[0] << " " << matrix[1] << " " <<matrix[2] << endl;
cout << matrix[3] << " " << matrix[4] << " " <<matrix[5] << endl;
cout << matrix[6] << " " << matrix[7] << " " <<matrix[8] << endl;
}
int CheckMatrix(char player)
{
//position 1
if (matrix[0] == player && matrix[1] == player && matrix[2]== player)
{
cout << "Win " << player ;
}
//position 2
if (matrix[3] == player && matrix[4] == player && matrix[5] == player)
{
cout << "Win " << player ;
}
//position 3
if (matrix[6] == player && matrix[7] == player && matrix[8] == player)
{
cout << "Win " << player ;
}
//position 4
if (matrix[0] == player && matrix[3] == player && matrix[6]== player)
{
cout << "Win " << player ;
}
//position 5
if (matrix[1] == player && matrix[4] == player && matrix[7] == player)
{
cout << "Win " << player ;
}
//position 6
if (matrix[2] == player && matrix[5] == player && matrix[8] == player)
{
cout << "Win " << player ;
}
//position 7
if (matrix[0] == player && matrix[4] == player && matrix[8] == player)
{
cout << "Win " << player ;
}
//position 8
if (matrix[2] == player && matrix[4] == player && matrix[6] == player)
{
cout << "Win " << player ;
}
}
int main(int argc, char *argv[])
{
int number = 0;
int idplayer = 0;
while (true)
{
DisplayMatrix();
CheckMatrix('X');
CheckMatrix('O');
if (idplayer == 0)
{
cout << "Input X ";
cin >> number;
for (int i = 0; i < 10; i++)
{
if (number == i && matrix[i] == '-') matrix[i] = 'X';
}
idplayer = 1;
}
if (idplayer == 1)
{
cout << "Input O ";
cin >> number;
for (int i = 0; i < 10; i++)
{
if (number == i && matrix[i] == '-') matrix[i] = 'O';
}
idplayer = 0;
}
}
}
|