first commit
This commit is contained in:
		
							
								
								
									
										3
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
				
			|||||||
 | 
					# tic_tac_toe
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Simple tic_tac_toe game for linux terminal
 | 
				
			||||||
							
								
								
									
										179
									
								
								src/game.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										179
									
								
								src/game.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,179 @@
 | 
				
			|||||||
 | 
					#include <stdio.h>
 | 
				
			||||||
 | 
					#include <ctype.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user