commit 3410f2c60e52f1f31fb1e638a529008a3ea3aed8 Author: santic-zombie Date: Mon Feb 21 21:31:59 2022 +0300 first commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..c457b82 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# tic_tac_toe + +Simple tic_tac_toe game for linux terminal \ No newline at end of file diff --git a/src/game.c b/src/game.c new file mode 100644 index 0000000..e19ae7e --- /dev/null +++ b/src/game.c @@ -0,0 +1,179 @@ +#include +#include + +struct player { + const unsigned char X; + const unsigned char O; +}; + +char field[5][5] = { '1', '|', '2', '|', '3', + '-', '+', '-', '+', '-', + '4', '|', '5', '|', '6', + '-', '+', '-', '+', '-', + '7', '|', '8', '|', '9' }; + +// сlear screen +void clrscr(void) +{ + printf("\033[2J"); /* Clear the entire screen. */ + printf("\033[0;0f"); /* Move cursor to the top left hand corner */ +} + +// clear stdin +void flush_input(void) { + char c; + while ( scanf("%c", &c) == 1 && c != '\n' ); +} + +// checking players turn input +char turn_inp(char M[5][5], unsigned char player) { + char ch; + char N[9]; + int err, c = 0; + int i, j, g = 0; + + for (i = 0; i < 5; i++) { + for (j = 0; j < 5; j++) { + if (i%2 == 0 && j%2 == 0 ) { + N[g] = M[i][j]; + g++; + } + } + } + + printf("%c, your turn: ", player); + for (;;) { + err = 0; + ch = getchar(); + flush_input(); + c = ch - 48; + if (isdigit(ch)) { + for (g = 1; g < 10; g++) { + if (c == g && ch != N[g-1] ) { + printf("Input error! %c, repeat your turn: ", player); + err = 1; + } + } + if (err == 0) {return ch;} + } else { + printf("Input error! %c, repeat your turn: ", player); + } + } +} + +// checking victory conditions +int victory(char M[5][5], int free_turns) { + int i, j, g = 0; + char N[9]; + for (i = 0; i < 5; i++) { + for (j = 0; j < 5; j++) { + if (i%2 == 0 && j%2 == 0 ) { + N[g] = M[i][j]; + g++; + } + } + } + if ((N[0] == N[1] && N[1] == N[2]) || + (N[3] == N[4] && N[4] == N[5]) || + (N[6] == N[7] && N[7] == N[8]) || + (N[0] == N[3] && N[3] == N[6]) || + (N[1] == N[4] && N[4] == N[7]) || + (N[2] == N[5] && N[5] == N[8]) || + (N[0] == N[4] && N[4] == N[8]) || + (N[2] == N[4] && N[4] == N[6])) { + return 1; + } + + if (free_turns == 0) + return 2; + + return 0; +} + +// change gamefield +void field_change(char M[5][5], unsigned char turn, unsigned char player) { + int i, j = 0; + for (i = 0; i < 5; i++) { + for (j = 0; j < 5; j++) { + if (M[i][j] == turn ) { + M[i][j] = player; + } + } + } +} + +// print gamefield +void print_field(char M[5][5]) { + int i,j = 0; + for (i = 0; i < 5; i++) { + for (j = 0; j < 5; j++) { + if (j < 4) { + if (M[i][j] == 'X') { + printf("\x1b[31m%c\x1b[0m ", M[i][j]); + } else if (M[i][j] == 'O') { + printf("\x1b[32m%c\x1b[0m ", M[i][j]); + } else { + printf("%c ", M[i][j]); + } + } else { + if (M[i][j] == 'X') { + printf("\x1b[31m%c\x1b[0m \n", M[i][j]); + } else if (M[i][j] == 'O') { + printf("\x1b[32m%c\x1b[0m \n", M[i][j]); + } else { + printf("%c \n", M[i][j]); + } + } + } + } +} + + +int main(void) { + + char turn; + struct player p = { 'X', 'O' }; + int z = 1; + int free_turns = 9; + int win = victory(field, free_turns); + + do { + print_field(field); + printf("\n"); + + if (z == 1) { + turn = turn_inp(field, p.X); + field_change(field, turn, p.X); + free_turns--; + z = 0; + } else { + turn = turn_inp(field, p.O); + field_change(field, turn, p.O); + free_turns--; + z = 1; + } + + win = victory(field, free_turns); + clrscr(); + + } while (win == 0); + + if (win == 1) { + print_field(field); + printf("\n\n"); + if (z == 0) + printf("Player \x1b[5;31m%c\x1b[0m wins!\n", p.X); + else + printf("Player \x1b[5;32m%c\x1b[0m wins!\n", p.O); + } + + if (win == 2) { + print_field(field); + printf("\n"); + const char go[] ="GAME OVER. Tie!"; + printf("\x1b[5;32m%s\x1b[0m\n", go); + } + + return 0; + +}