117 lines
2.5 KiB
C
117 lines
2.5 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
|
|
|
|
#if defined(_WIN32)
|
|
#define PLATFORM 0
|
|
#elif defined(_WIN64)
|
|
#define PLATFORM 0
|
|
#elif defined(__CYGWIN__)
|
|
#define PLATFORM 0
|
|
#elif defined(__linux__)
|
|
#define PLATFORM 1
|
|
#else
|
|
#define PLATFORM NULL
|
|
#endif
|
|
|
|
int kdoHraje = 0;
|
|
|
|
int vypisHru(char pole[][3], int vyherce) {
|
|
if (PLATFORM == 0) {
|
|
system("cls");
|
|
} else if (PLATFORM == 1) {
|
|
system("clear");
|
|
}
|
|
|
|
if (vyherce == 0) printf("Vyhralo o!\n");
|
|
else if (vyherce == 1) printf("Vyhralo x!\n");
|
|
else if (vyherce == 2) printf("Remiza!\n");
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
printf("[%c] ", pole[i][0]);
|
|
printf("[%c] ", pole[i][1]);
|
|
printf("[%c]\n", pole[i][2]);
|
|
}
|
|
|
|
if (vyherce == 0 || vyherce == 1 || vyherce == 2) return 0;
|
|
}
|
|
|
|
int checkniVyhru(char pole[][3]) {
|
|
// diagonalni check
|
|
if (pole[0][0] == 'o' && pole[1][1] == 'o' && pole[2][2] == 'o' || pole[0][2] == 'o' && pole[1][1] == 'o' && pole[2][0] == 'o') {
|
|
return 0;
|
|
}
|
|
|
|
if (pole[0][0] == 'x' && pole[1][1] == 'x' && pole[2][2] == 'x' || pole[0][2] == 'x' && pole[1][1] == 'x' && pole[2][0] == 'x') {
|
|
return 1;
|
|
}
|
|
|
|
// horizontalni check
|
|
for (int i = 0; i < 3; i++) {
|
|
if (pole[i][0] == 'o' && pole[i][1] == 'o' && pole[i][2] == 'o') {
|
|
return 0;
|
|
}
|
|
|
|
if (pole[i][0] == 'x' && pole[i][1] == 'x' && pole[i][2] == 'x') {
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
// vertikalni check
|
|
for (int i = 0; i < 3; i++) {
|
|
if (pole[0][i] == 'o' && pole[1][i] == 'o' && pole[2][i] == 'o') {
|
|
return 0;
|
|
}
|
|
|
|
if (pole[0][i] == 'x' && pole[1][i] == 'x' && pole[2][i] == 'x') {
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
// check na remizu
|
|
int zabrane = 0;
|
|
for (int i = 0; i < 3; i++) {
|
|
if (pole[i][0] == 'o' || pole[i][0] == 'x') zabrane++;
|
|
if (pole[i][1] == 'o' || pole[i][1] == 'x') zabrane++;
|
|
if (pole[i][1] == 'o' || pole[i][2] == 'x') zabrane++;
|
|
if (zabrane == 9) return 2;
|
|
}
|
|
}
|
|
|
|
int herniKolo(char pole[][3]) {
|
|
int rada, sloupec;
|
|
|
|
vypisHru(pole, checkniVyhru(pole));
|
|
|
|
scanf("%d", &rada);
|
|
scanf("%d", &sloupec);
|
|
|
|
if (kdoHraje == 0) {
|
|
if (pole[rada-1][sloupec-1] == ' ' && 1 <= rada <= 3 && 1 <= sloupec <= 3) {
|
|
pole[rada-1][sloupec-1] = 'o';
|
|
kdoHraje = 1;
|
|
}
|
|
} else if (kdoHraje == 1) {
|
|
if (pole[rada-1][sloupec-1] == ' ' && 1 <= rada <= 3 && 1 <= sloupec <= 3) {
|
|
pole[rada-1][sloupec-1] = 'x';
|
|
kdoHraje = 0;
|
|
}
|
|
}
|
|
|
|
checkniVyhru(pole);
|
|
|
|
if (vypisHru(pole, checkniVyhru(pole)) == 0) return 0;
|
|
}
|
|
|
|
int main() {
|
|
char pole[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
if (herniKolo(pole) == 0) return 0;
|
|
herniKolo(pole);
|
|
}
|
|
return 0;
|
|
}
|