#include"biblioNavale.h"
#include<stdlib.h>
#include<stdio.h>
#include<time.h>

/*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(verif(p1)==0||verif(p2)==0){
	printf("Joueur 1 :\n");
	initPlateau(p1);
	printf("Joueur 2 :\n");
	initPlateau(p2);
	}

	/*Affichage*/
	for (int i = 0; i < 60; i++)
	{printf("-");}
	printf("\n");
	afficheduo(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=jouerJoueur(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");
			afficheduo(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");
			afficheduo(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");
			afficheduo(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");
			afficheduo(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");
			afficheduo(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");
			afficheduo(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=jouerJoueur(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");
			afficheduo(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");
			afficheduo(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");
			afficheduo(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");
			afficheduo(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");
			afficheduo(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");
			afficheduo(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;
}