#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 5
#define MAX_ATTEMPTS 5
void initializeBoard(char board[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = '~'; // Agua
}
}
}
void printBoard(char board[SIZE][SIZE]) {
printf(" 0 1 2 3 4\n");
for (int i = 0; i < SIZE; i++) {
printf("%d ", i);
for (int j = 0; j < SIZE; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
}
int main() {
char board[SIZE][SIZE];
int shipRow, shipCol;
int guessRow, guessCol;
int attempts = 0;
int hit = 0;
srand(time(0));
shipRow = rand() % SIZE;
shipCol = rand() % SIZE;
initializeBoard(board);
while (attempts < MAX_ATTEMPTS && hit == 0) {
printBoard(board);
printf("Adivina la fila y columna del barco (ejemplo: 1 2): ");
scanf("%d %d", &guessRow, &guessCol);
if (guessRow == shipRow && guessCol == shipCol) {
printf("¡Tocado y hundido!\n");
hit = 1;
} else {
printf("Fallaste. Intenta de nuevo.\n");
board[guessRow][guessCol] = 'X'; // Marca el intento fallido
attempts++;
}
}
if (hit == 0) {
printf("Has agotado tus intentos. El barco estaba en %d, %d.\n", shipRow, shipCol);
}
return 0;
}