SAE partie 2

This commit is contained in:
Derqsi BILAL 2025-01-17 17:28:06 +01:00
parent 9feb9b4361
commit d4f88f6ac9

@ -2,35 +2,36 @@
#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 for horizontal, 1 for vertical
int direction = rand() % 2; // 0 pour horizontale, 1 pour verticale
int x, y;
int place = 0;
int valid = 0; // 1 si la position est valide sinon 0
while (!place) {
if (direction == 0) { // Horizontal
while (!valid) {
if (direction == 0) {
x = rand() % (10 - t + 1);
y = rand() % 10;
place = 1;
valid = 1;
for (int i = 0; i < t; i++) {
if (tab[y][x + i] != ' ') {
place = 0;
valid = 0;
break;
}
}
} else { // Vertical
} else {
x = rand() % 10;
y = rand() % (10 - t + 1);
place = 1;
valid = 1;
for (int i = 0; i < t; i++) {
if (tab[y + i][x] != ' ') {
place = 0;
valid = 0;
break;
}
}
}
if (place) {
if (valid) {
for (int i = 0; i < t; i++) {
if (direction == 0) {
tab[y][x + i] = b;
@ -51,7 +52,7 @@ int verif(char tab[][10]) {
}
}
}
return nbcases == 17 ;
return nbcases == 17 ? 1 : 0;
}
void affiche(char t[][10]) {
@ -59,7 +60,11 @@ void affiche(char t[][10]) {
for (int i = 0; i < 10; i++) {
printf("%2d ", i + 1);
for (int j = 0; j < 10; j++) {
printf("%c ", t[i][j]);
if (t[i][j] == ' ' || t[i][j] == '.' || t[i][j] == 'X') {
printf("%c ", t[i][j]);
} else {
printf(" ");
}
}
printf("\n");
}
@ -70,11 +75,19 @@ void afficheduo(char t[][10], char p[][10]) {
for (int i = 0; i < 10; i++) {
printf("%2d ", i + 1);
for (int j = 0; j < 10; j++) {
printf("%c ", t[i][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++) {
printf("%c ", p[i][j]);
if (p[i][j] == ' ' || p[i][j] == '.' || p[i][j] == 'X') {
printf("%c ", p[i][j]);
} else {
printf(" ");
}
}
printf("\n");
}
@ -86,27 +99,100 @@ void initPlateau(char plat[][10]) {
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
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 input[3];
scanf("%2s", input);
char coordonnees[3];
scanf("%2s", coordonnees);
x = input[0] - 'A';
y = input[1] - '1';
x = coordonnees[0] - 'A';
y = coordonnees[1] - '1';
if (x < 0 || x >= 10 || y < 0 || y >= 10) {
return -3; // Hors plateau
}
char target = adv[y][x];
switch (target) {
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