SAE partie 2
This commit is contained in:
parent
c279bdc6cb
commit
4019c6178c
272
biblioNavale2.c
272
biblioNavale2.c
@ -1,145 +1,131 @@
|
||||
void ajoutNavireAleatoireDeux(char tab[][10],char b,int t);
|
||||
int verifDeux(char tab[][10]);
|
||||
void afficheDeux(char t[][10])
|
||||
void afficheduoDeux(char t[][10],char p[][10]){
|
||||
printf("_|");
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
printf("%d|",i);
|
||||
}
|
||||
printf(" ");
|
||||
printf("_|");
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
printf("%d|",i);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
printf("A|")
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
if(t[i][1]==0){
|
||||
printf(" |");
|
||||
}
|
||||
if(t[i][1]==1){
|
||||
printf("X|");
|
||||
}
|
||||
if(t[i][1]==2){
|
||||
printf(".|");
|
||||
}
|
||||
}
|
||||
printf(" ");
|
||||
printf("A|")
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
if(t[i][1]==0){
|
||||
printf(" |");
|
||||
}
|
||||
if(t[i][1]==1){
|
||||
printf("X|");
|
||||
}
|
||||
if(t[i][1]==2){
|
||||
printf(".|");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
printf("B|")
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
if(t[i][2]==0){
|
||||
printf(" |");
|
||||
}
|
||||
if(t[i][2]==1){
|
||||
printf("X|");
|
||||
}
|
||||
if(t[i][2]==2){
|
||||
printf(".|");
|
||||
}
|
||||
}
|
||||
printf(" ");
|
||||
printf("B|")
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
if(t[i][2]==0){
|
||||
printf(" |");
|
||||
}
|
||||
if(t[i][2]==1){
|
||||
printf("X|");
|
||||
}
|
||||
if(t[i][2]==2){
|
||||
printf(".|");
|
||||
}
|
||||
}
|
||||
};
|
||||
void initPlateauDeux(char plat[][10]);
|
||||
int jouerJoueurDeux(char adv[][10]);
|
||||
/* 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 valid = 0;
|
||||
|
||||
char* navireDeux(int i){
|
||||
if(i==1){char navire[11];
|
||||
navire[0]='P';
|
||||
navire[1]='o';
|
||||
navire[2]='r';
|
||||
navire[3]='t';
|
||||
navire[4]='e';
|
||||
navire[5]='-';
|
||||
navire[6]='A';
|
||||
navire[7]='v';
|
||||
navire[8]='i';
|
||||
navire[9]='o';
|
||||
navire[10]='n';
|
||||
navire[11]='\0';
|
||||
return navire;}
|
||||
if(i==2){char navire [8];
|
||||
navire[0]='C';
|
||||
navire[1]='r';
|
||||
navire[2]='o';
|
||||
navire[3]='i';
|
||||
navire[4]='s';
|
||||
navire[5]='e';
|
||||
navire[6]='u';
|
||||
navire[7]='r';
|
||||
navire[8]='\0';
|
||||
return navire;}
|
||||
if(i==3){char navire[10];
|
||||
navire[0]='S';
|
||||
navire[1]='o';
|
||||
navire[2]='u';
|
||||
navire[3]='s';
|
||||
navire[4]='-';
|
||||
navire[5]='M';
|
||||
navire[6]='a';
|
||||
navire[7]='r';
|
||||
navire[8]='i';
|
||||
navire[9]='n';
|
||||
navire[10]='\0';
|
||||
return navire;}
|
||||
if(i==4){char navire[10];
|
||||
navire[0]='M';
|
||||
navire[1]='o';
|
||||
navire[2]='u';
|
||||
navire[3]='s';
|
||||
navire[4]='-';
|
||||
navire[5]='S';
|
||||
navire[6]='a';
|
||||
navire[7]='r';
|
||||
navire[8]='i';
|
||||
navire[9]='n';
|
||||
navire[10]='\0';
|
||||
return navire;}
|
||||
if(i==5){char navire[10];
|
||||
navire[0]='T';
|
||||
navire[1]='o';
|
||||
navire[2]='r';
|
||||
navire[3]='p';
|
||||
navire[4]='i';
|
||||
navire[5]='l';
|
||||
navire[6]='l';
|
||||
navire[7]='e';
|
||||
navire[8]='u';
|
||||
navire[9]='r';
|
||||
navire[10]='\0';
|
||||
return navire;}
|
||||
if(i>5){return NULL;}
|
||||
if(i<1){return NULL;}
|
||||
};
|
||||
while (!valid) {
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user