122 lines
2.7 KiB
C
122 lines
2.7 KiB
C
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
void print_pole(char pole[3][3]);
|
||
|
char is_game_won(char pole[3][3]);
|
||
|
void read_date(int* y, int*x);
|
||
|
int main(){
|
||
|
char pole[3][3];
|
||
|
|
||
|
for (size_t y = 0; y < 3; y++)//заполняем по дефолку пробелами
|
||
|
{
|
||
|
for (size_t x = 0; x < 3; x++)
|
||
|
{
|
||
|
pole[y][x] = ' ';
|
||
|
}
|
||
|
|
||
|
}
|
||
|
printf("Welcome!!!\n");
|
||
|
while (1)
|
||
|
{
|
||
|
if (is_game_won(pole) == 'o')//безконечный цыул пока один из игроков не выиграет
|
||
|
{
|
||
|
printf("Won first player\n");
|
||
|
break;
|
||
|
}else if (is_game_won(pole) == 'x')
|
||
|
{
|
||
|
printf("Won second player\n");
|
||
|
break;
|
||
|
}
|
||
|
print_pole(pole);
|
||
|
printf("First player: (row column)\n");
|
||
|
int x =0;
|
||
|
int y =0;
|
||
|
read_date(&y,&x);
|
||
|
pole[y][x] = 'o';
|
||
|
print_pole(pole);
|
||
|
if (is_game_won(pole) == 'o')//проверка на победу
|
||
|
{
|
||
|
printf("Won first player\n");
|
||
|
break;
|
||
|
}else if (is_game_won(pole) == 'x')
|
||
|
{
|
||
|
printf("Won second player (row column ) \n");
|
||
|
break;
|
||
|
}
|
||
|
printf("Second player: \n");
|
||
|
read_date(&y,&x);
|
||
|
pole[y][x] = 'x';
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
void print_pole(char pole[3][3]){//выводит поле спомощью фор
|
||
|
printf (" 1 2 3\n");
|
||
|
for (size_t y = 0; y < 3; y++)
|
||
|
{
|
||
|
printf("%ld ", y+1);
|
||
|
for (size_t x = 0; x < 3; x++)
|
||
|
{
|
||
|
printf("%c|", pole[y][x]);
|
||
|
}
|
||
|
printf("\n");
|
||
|
printf(" -----\n");
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
void read_date(int* y, int*x){//считует даные и проверяет их
|
||
|
scanf("%d %d", y, x);
|
||
|
*x = *x -1;
|
||
|
*y = *y -1;
|
||
|
while (*x>2 || *y >2)
|
||
|
{
|
||
|
printf("Wrong input\n");
|
||
|
scanf("%d %d", y, x);
|
||
|
*x = *x -1;
|
||
|
*y = *y -1;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
char is_game_won(char pole[3][3]){//проверяет победу
|
||
|
for (size_t y = 0; y < 3; y++)
|
||
|
{
|
||
|
if (pole[y][0] == pole[y][1] && pole[y][1]==pole[y][2])
|
||
|
{
|
||
|
if (pole[y][0] != ' ')
|
||
|
{
|
||
|
return pole[y][0];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
for (size_t x = 0; x< 3; x++)
|
||
|
{
|
||
|
if (pole[0][x] == pole[1][x]&&pole[1][x]==pole[2][x])
|
||
|
{
|
||
|
if (pole[0][x] != ' ')
|
||
|
{
|
||
|
return pole[0][x];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
if (pole[0][0] == pole[1][1]&&pole[1][1]==pole[2][2])
|
||
|
{
|
||
|
if (pole[0][0] != ' ')
|
||
|
{
|
||
|
return pole[0][0];
|
||
|
}
|
||
|
}
|
||
|
if (pole[2][0]==pole[1][1]&&pole[1][1]==pole[0][2])
|
||
|
{
|
||
|
if (pole[2][0] != ' ')
|
||
|
{
|
||
|
return pole[2][0];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return ' ';
|
||
|
}
|