/* biblioNavale.h */ #include <stdlib.h> #include <stdio.h> #include <time.h> #include <string.h> void ajoutNavireAleatoire(char tab[][10], char b, int t) { int direction = rand() % 2; // 0 pour horizontale, 1 pour verticale int x, y; int valid = 0; // 1 si la position est valide sinon 0 while (!valid) { if (direction == 0) { x = rand() % (10 - t + 1); y = rand() % 10; valid = 1; for (int i = 0; i < t; i++) { if (tab[y][x + i] != ' ') { valid = 0; break; } } } else { x = rand() % 10; y = rand() % (10 - t + 1); valid = 1; for (int i = 0; i < t; i++) { if (tab[y + i][x] != ' ') { valid = 0; break; } } } if (valid) { for (int i = 0; i < t; i++) { if (direction == 0) { tab[y][x + i] = b; } else { tab[y + i][x] = b; } } } } } int verif(char tab[][10]) { int nbcases = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (tab[i][j] != ' ') { nbcases++; } } } return nbcases == 17 ? 1 : 0; } void affiche(char t[][10]) { printf(" A B C D E F G H I J\n"); for (int i = 0; i < 10; i++) { printf("%2d ", i + 1); for (int j = 0; j < 10; j++) { if (t[i][j] == ' ' || t[i][j] == '.' || t[i][j] == 'X') { printf("%c ", t[i][j]); } else { printf(" "); } } printf("\n"); } } void afficheduo(char t[][10], char p[][10]) { printf(" A B C D E F G H I J A B C D E F G H I J\n"); for (int i = 0; i < 10; i++) { printf("%2d ", i + 1); for (int j = 0; j < 10; j++) { if (t[i][j] == ' ' || t[i][j] == '.' || t[i][j] == 'X') { printf("%c ", t[i][j]); } else { printf(" "); } } printf(" %2d ", i + 1); for (int j = 0; j < 10; j++) { if (p[i][j] == ' ' || p[i][j] == '.' || p[i][j] == 'X') { printf("%c ", p[i][j]); } else { printf(" "); } } printf("\n"); } } void initPlateau(char plat[][10]) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { plat[i][j] = ' '; } } printf("Voulez-vous placer les navires manuellement (M) ou aléatoirement (A) ? "); char choix; scanf(" %c", &choix); if (choix == 'm' || choix == 'M') { int tailles[] = {5, 4, 3, 3, 2}; char symboles[] = {'A', 'C', 'S', 'M', 'T'}; for (int n = 0; n < 5; n++) { int valid = 0; while (!valid) { char direction; int x, y; printf("Placer le navire %c de taille %d\n", symboles[n], tailles[n]); printf("Horizontal (h) ou Vertical (v) ? "); scanf(" %c", &direction); printf("Entrez les coordonnées (ex: A5): "); char coordonnees[3]; scanf("%2s", coordonnees); x = coordonnees[0] - 'A'; y = coordonnees[1] - '1'; if (x < 0 || x >= 10 || y < 0 || y >= 10) { printf("Coordonnées hors plateau, essayez à nouveau.\n"); continue; } valid = 1; if (direction == 'h' || direction == 'H') { if (x + tailles[n] > 10) { printf("Le navire dépasse à droite, essayez à nouveau.\n"); valid = 0; continue; } for (int i = 0; i < tailles[n]; i++) { if (plat[y][x + i] != ' ') { printf("Le navire entre en collision, essayez à nouveau.\n"); valid = 0; break; } } if (valid) { for (int i = 0; i < tailles[n]; i++) { plat[y][x + i] = symboles[n]; } } } else if (direction == 'v' || direction == 'V') { if (y + tailles[n] > 10) { printf("Le navire dépasse en bas, essayez à nouveau.\n"); valid = 0; continue; } for (int i = 0; i < tailles[n]; i++) { if (plat[y + i][x] != ' ') { printf("Le navire entre en collision, essayez à nouveau.\n"); valid = 0; break; } } if (valid) { for (int i = 0; i < tailles[n]; i++) { plat[y + i][x] = symboles[n]; } } } else { printf("Direction invalide, essayez à nouveau.\n"); valid = 0; } } } } else { ajoutNavireAleatoire(plat, 'A', 5); // Porte-Avion ajoutNavireAleatoire(plat, 'C', 4); // Croiseur ajoutNavireAleatoire(plat, 'S', 3); // Sous-Marin ajoutNavireAleatoire(plat, 'M', 3); // Mous-Sarin ajoutNavireAleatoire(plat, 'T', 2); // Torpilleur } } int jouerJoueur(char adv[][10]) { int x, y; printf("Entrez les coordonnées (ex: A5): "); char coordonnees[3]; scanf("%2s", coordonnees); x = coordonnees[0] - 'A'; y = coordonnees[1] - '1'; if (x < 0 || x >= 10 || y < 0 || y >= 10) { return -3; // Hors plateau } char cible = adv[y][x]; switch (cible) { case ' ': adv[y][x] = '.'; return -1; // À l'eau case 'A': adv[y][x] = 'X'; return 0; // Porte-Avion case 'C': adv[y][x] = 'X'; return 1; // Croiseur case 'S': adv[y][x] = 'X'; return 2; // Sous-Marin case 'M': adv[y][x] = 'X'; return 3; // Mous-Sarin case 'T': adv[y][x] = 'X'; return 4; // Torpilleur case '.': case 'X': return -2; // Case déjà touchée default: return -2; // Caractère non reconnu } } char* navire(int i) { switch (i) { case 1: return "Porte-Avion"; case 2: return "Croiseur"; case 3: return "Sous-Marin"; case 4: return "Mous-Sarin"; case 5: return "Torpilleur"; default: return "Inconnu"; } }