#include <graphics.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#define SIZE 3
void drawGrid(int grid[SIZE][SIZE], int revealed[SIZE][SIZE]) {
int x, y;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
x = j * 100 + 50; // Posición X
y = i * 100 + 50; // Posición Y
if (revealed[i][j]) {
setfillstyle(SOLID_FILL, grid[i][j]);
floodfill(x + 1, y + 1, WHITE);
} else {
rectangle(x, y, x + 100, y + 100);
}
if (revealed[i][j]) {
char num[2];
sprintf(num, "%d", (i * SIZE + j + 1));
outtextxy(x + 40, y + 40, num);
}
}
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Cambia la ruta según tu instalación
srand(time(NULL)); // Inicializa la semilla para números aleatorios
int grid[SIZE][SIZE];
int revealed[SIZE][SIZE] = {0}; // 0 = oculto, 1 = revelado
int attempts = 3;
int pairsFound = 0;
// Generar colores aleatorios para cada número
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
grid[i][j] = (rand() % 15) + 1; // Colores de 1 a 15
}
}
while (attempts > 0) {
drawGrid(grid, revealed);
int firstChoice, secondChoice;
printf("Elige dos números (1-9): ");
scanf("%d %d", &firstChoice,&secondChoice);
// Convertir a índices de matriz
int firstRow = (firstChoice - 1) / SIZE;
int firstCol = (firstChoice - 1) % SIZE;
int secondRow = (secondChoice - 1) / SIZE;
int secondCol = (secondChoice - 1) % SIZE;
// Revelar elecciones
revealed[firstRow][firstCol] = 1;
revealed[secondRow][secondCol] = 1;
drawGrid(grid, revealed);
delay(1000); // Esperar un segundo para mostrar las elecciones
// Comprobar si son iguales
if (grid[firstRow][firstCol] == grid[secondRow][secondCol]) {
pairsFound++;
printf("¡Encontraste un par!\n");
} else] = 0; // Ocultar de nuevo
revealed[secondRow][secondCol] = 0; // Ocultar de nuevo
printf("No son iguales. Intenta de nuevo.\n");
attempts--;
}
if (pairsFound == 4) {
printf("¡Ganaste! Encontraste todos los pares.\n");
break;
}
}
if (attempts == 0) {
printf("Perdiste. Intenta de nuevo.\n");
}
getch();
closegraph();
return 0;
}