SAE partie 2

This commit is contained in:
Derqsi BILAL 2025-01-17 17:43:29 +01:00
parent dae2e696e4
commit 874da7df5f

479
MaBatailleNavale.c Normal file

@ -0,0 +1,479 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
/* biblioNavale2.h */
void ajoutNavireAleatoireDeux(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 verifDeux(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 afficheDeux(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 afficheduoDeux(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 initPlateauDeux(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 {
ajoutNavireAleatoireDeux(plat, 'A', 5); // Porte-Avion
ajoutNavireAleatoireDeux(plat, 'C', 4); // Croiseur
ajoutNavireAleatoireDeux(plat, 'S', 3); // Sous-Marin
ajoutNavireAleatoireDeux(plat, 'M', 3); // Mous-Sarin
ajoutNavireAleatoireDeux(plat, 'T', 2); // Torpilleur
}
}
int jouerJoueurDeux(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* navireDeux(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";
}
}
/*commande pour compiler :"gcc BatailleNavale.c -o BatailleNavale.o biblioNavale.o
"*/
int main(int argc, char const *argv[]){
/*création de toutes les variables utiles au programme et rand pour le
placement aléatoire des bateaux.*/
srand(time(NULL));
char p1 [10][10];
char p2 [10][10];
int victoirep1=5;
int victoirep2=5;
int a1=5;
int a2=5;
int c1=4;
int c2=4;
int s1=3;
int s2=3;
int m1=3;
int m2=3;
int t1=2;
int t2=2;
int tour=0;
/*Tout d'abord on va initialiser les plateaux des 2 joueurs.
On fait une boucle qui permet de recommencer l'initialisation si la
verification renvoi une erreur*/
while( verifDeux(p1)==0|| verifDeux(p2)==0){
printf("Joueur 1 :\n");
initPlateauDeux(p1);
printf("Joueur 2 :\n");
initPlateauDeux(p2);
}
/*Affichage*/
for (int i = 0; i < 60; i++)
{printf("-");}
printf("\n");
afficheduoDeux(p1,p2);
printf("La partie peut commencer !\n");
/*Affichage*/
/*Boucle de la partie entière*/
while(victoirep1!=0 && victoirep2!=0){
if(victoirep1!=0 && victoirep2!=0){
tour=0;
}
/*Boucle du tour de joueur 1*/
while(tour<1){
if(victoirep2==0){break;}
printf("Au tour du joueur 1 :\n");
int tir=jouerJoueurDeux(p1);
/*si le tir a rencontré une erreur*/
if(tir==-2||tir==-3){printf("veuillez tirer dans le plateau :)\n");}
/*si le tir ne touche aucun bateau*/
if(tir==-1){
for (int i = 0; i < 60; i++){
printf("-");
}
printf("\n");
afficheduoDeux(p1,p2);
printf("À l'eau !\n");
tour=tour+1;
}
/*si le tir a touché le porte avion*/
if(tir==0 && a1>=0){
for (int i = 0; i < 60; i++){
printf("-");
}
printf("\n");
afficheduoDeux(p1,p2);
printf("Touché\n");
a1=a1-1;
}
if (tir==0&&a1<=0){
victoirep1=victoirep1-1;
printf("Coulé !\n");
if(victoirep1==0){break;}
}
/*si le tir a touché le croiseur*/
if(tir==1 && c1>=0){
for (int i = 0; i < 60; i++){
printf("-");
}
printf("\n");
afficheduoDeux(p1,p2);
printf("Touché\n");
c1=c1-1;
}
if (tir==1 && c1<=0){
printf("Coulé !\n");
victoirep1=victoirep1-1;
if(victoirep1==0){break;}
}
/*si le tir a touché le sous marin*/
if(tir==2 && s1>=0){
for (int i = 0; i < 60; i++){
printf("-");
}
printf("\n");
afficheduoDeux(p1,p2);
printf("Touché\n");
s1=s1-1;
}
if (tir==2 && s1<=0){
printf("Coulé !\n");
victoirep1=victoirep1-1;
if(victoirep1==0){break;}
}
/*si le tir a touché le mous sarin*/
if(tir==3 && m1>=0){
for (int i = 0; i < 60; i++){
printf("-");
}
printf("\n");
afficheduoDeux(p1,p2);
printf("Touché\n");
m1=m1-1;
}
if (tir==3 && m1<=0){
printf("Coulé !\n");
victoirep1=victoirep1-1;
if(victoirep1==0){break;}
}
/*si le tir a touché le torpilleur*/
if(tir==4 && t1>=0){
for (int i = 0; i < 60; i++){
printf("-");
}
printf("\n");
afficheduoDeux(p1,p2);
printf("Touché\n");
t1=t1-1;
}
if (tir==4 && t1<=0){
printf("Coulé !\n");
victoirep1=victoirep1-1;
if(victoirep1==0){break;}
}
}
if(victoirep1!=0 && victoirep2!=0){
tour=0;
}
/*Boucle du tour de joueur 2*/
while(tour<1){
if(victoirep1==0){break;}
printf("Au tour du joueur 2 :\n");
int tir=jouerJoueurDeux(p2);
/*si le tir a rencontré une erreur*/
if(tir==-2||tir==-3){printf("veuillez tirer dans le plateau :)\n");}
/*si le tir ne touche aucun bateau*/
if(tir==-1){
for (int i = 0; i < 60; i++){
printf("-");
}
printf("\n");
afficheduoDeux(p1,p2);
printf("À l'eau !\n");
tour=tour+1;
}
/*si le tir a touché le porte avion*/
if(tir==0 && a2>=0){
for (int i = 0; i < 60; i++){
printf("-");
}
printf("\n");
afficheduoDeux(p1,p2);
printf("Touché\n");
a2=a2-1;
}
if (tir==0 && a2<=0){
printf("Coulé !\n");
victoirep2=victoirep2-1;
if(victoirep2==0){break;}
}
/*si le tir a touché le croiseur*/
if(tir==1 && c2>=0){
for (int i = 0; i < 60; i++){
printf("-");
}
printf("\n");
afficheduoDeux(p1,p2);
printf("Touché\n");
c2=c2-1;
}
if (tir==1 && c2<=0){
printf("Coulé !\n");
victoirep2=victoirep2-1;
if(victoirep2==0){break;}
}
/*si le tir a touché le sous marin*/
if(tir==2 && s2>=0){
for (int i = 0; i < 60; i++){
printf("-");
}
printf("\n");
afficheduoDeux(p1,p2);
printf("Touché\n");
s2=s2-1;
}
if (tir==2 && s1<=0){
printf("Coulé !\n");
victoirep2=victoirep2-1;
if(victoirep2==0){break;}
}
/*si le tir a touché le mous sarin*/
if(tir==3 && m2>=0){
for (int i = 0; i < 60; i++){
printf("-");
}
printf("\n");
afficheduoDeux(p1,p2);
printf("Touché\n");
m2=m2-1;
}
if (tir==3 && m2<=0){
printf("Coulé !\n");
victoirep2=victoirep2-1;
if(victoirep2==0){break;}
}
/*si le tir a touché le torpilleur*/
if(tir==4 && t2>=0){
for (int i = 0; i < 60; i++){
printf("-");
}
printf("\n");
afficheduoDeux(p1,p2);
printf("Touché\n");
t2=t2-1;
}
if (tir==4 && t2<=0){
printf("Coulé !\n");
victoirep2=victoirep2-1;
if(victoirep2==0){break;}
}
}
}
/*fin de la boucle de la partie entière donc il y a désormais un vainqueur
on affiche qui a remporté la partie avec les conditions suivantes*/
if(victoirep1==0){
printf("Joueur 1 a gagné la partie !\nBien joué !\n");
}
if(victoirep2==0){
printf("Joueur 2 a gagné la partie !\nBien joué !\n");
}
return 0;
}