132 lines
3.5 KiB
C
132 lines
3.5 KiB
C
/* biblioNavale.h */
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <time.h>
|
|
|
|
void ajoutNavireAleatoire(char tab[][10], char b, int t) {
|
|
int direction = rand() % 2; // 0 for horizontal, 1 for vertical
|
|
int x, y;
|
|
int place = 0;
|
|
|
|
while (!place) {
|
|
if (direction == 0) { // Horizontal
|
|
x = rand() % (10 - t + 1);
|
|
y = rand() % 10;
|
|
place = 1;
|
|
for (int i = 0; i < t; i++) {
|
|
if (tab[y][x + i] != ' ') {
|
|
place = 0;
|
|
break;
|
|
}
|
|
}
|
|
} else { // Vertical
|
|
x = rand() % 10;
|
|
y = rand() % (10 - t + 1);
|
|
place = 1;
|
|
for (int i = 0; i < t; i++) {
|
|
if (tab[y + i][x] != ' ') {
|
|
place = 0;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (place) {
|
|
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 ;
|
|
}
|
|
|
|
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++) {
|
|
printf("%c ", t[i][j]);
|
|
}
|
|
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++) {
|
|
printf("%c ", t[i][j]);
|
|
}
|
|
printf(" %2d ", i + 1);
|
|
for (int j = 0; j < 10; j++) {
|
|
printf("%c ", p[i][j]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
}
|
|
|
|
void initPlateau(char plat[][10]) {
|
|
for (int i = 0; i < 10; i++) {
|
|
for (int j = 0; j < 10; j++) {
|
|
plat[i][j] = ' ';
|
|
}
|
|
}
|
|
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 input[3];
|
|
scanf("%2s", input);
|
|
|
|
x = input[0] - 'A';
|
|
y = input[1] - '1';
|
|
if (x < 0 || x >= 10 || y < 0 || y >= 10) {
|
|
return -3; // Hors plateau
|
|
}
|
|
|
|
char target = adv[y][x];
|
|
switch (target) {
|
|
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";
|
|
}
|
|
}
|