#include <stdio.h> #include <stdlib.h>
struct coord { int f; int c; }; typedef struct coord coord;
int hay_ganador(char tab[][3]);
int main(void) { char tab[][3] = { {'X', ' ', 'O'}, {'X', 'O', ' '}, {'X', 'O', 'X'} }; int estado; if ((estado = hay_ganador(tab)) >= 0) printf("Ganador en solucion %d\n", estado); else puts("No hay ganador"); return EXIT_SUCCESS; }
int hay_ganador(char tab[][3]) { static coord const sol[][3] = { {{0, 0}, {0, 1}, {0, 2}}, {{1, 0}, {1, 1}, {1, 2}}, {{2, 0}, {2, 1}, {2, 2}}, {{0, 0}, {1, 0}, {2, 0}}, {{0, 1}, {1, 1}, {2, 1}}, {{0, 2}, {1, 2}, {2, 2}}, {{0, 0}, {1, 1}, {2, 2}}, {{0, 2}, {1, 1}, {2, 0}} }; static size_t const num_sols = sizeof sol / sizeof sol[0]; size_t i; size_t j; char prev; for (i = 0; i < num_sols; i++) if ((prev = tab[sol[i][0].f][sol[i][0].c]) != ' '){ for (j = 1; j < 3 && tab[sol[i][j].f][sol[i][j].c] == prev; j++) ; if (j == 3) return i; } return -1; } |