From 5c429aa46d6f8cc080471231cad8f6b7a8ddb4f0 Mon Sep 17 00:00:00 2001 From: Nazar Sendetskyi Date: Mon, 20 Jun 2022 05:43:18 +0000 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D0=BB(=D0=B0)=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 game.c diff --git a/game.c b/game.c new file mode 100644 index 0000000..73142ea --- /dev/null +++ b/game.c @@ -0,0 +1,122 @@ +#include +#include + +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 ' '; +} \ No newline at end of file