teac2006
Nouveau membre
Bonjour
je dois créer une petite bataille navale en c dans le cadre de mon DUT reseaux et télécommunication nous utilisons Code::Blocks, j'arrive a compiler mais je n'arrive pas a jouer, aidez moi svp.
le source:[cpp]
#include <stdio.h>
#include <tpinfo.h>
#include <stdlib.h>
#include <time.h>
int MAX_LIG=10;
int MIN_LIG=6;
int MAX_COL=10;
int MIN_COL=6;
void grandeur_matrice (int *nl,int *nc);
void remplit_matrice (int mat [][MAX_COL],int nl,int nc,int x);
void affiche_matrice(int mat [][MAX_COL],int nl,int nc);
int nombre_al (int max);
void intro_nbr_bateaux (int nl, int nc, int *nb_deux,int *nb_trois,int *nb_quatre,int *nb_cinq,int *nb_six);
int verif_place (int nb_deux, int nb_trois,int nb_quatre,int nb_cinq,int nb_six,int nl,int nc);
int verif_placement_bateau_2 (int mat[][MAX_COL],int nl, int nc, int*x,int*y, int *orientation);
void placement_bateau_2 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation);
int verif_contour (int mat[][MAX_COL],int x1,int y1,int x2,int y2 );
int verif_placement_bateau_3 (int mat[][MAX_COL],int nl, int nc, int*x,int*y, int *orientation);
void placement_bateau_3 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation);
int verif_placement_bateau_4 (int mat[][MAX_COL],int nl, int nc, int*x,int*y, int *orientation);
void placement_bateau_4 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation);
int verif_placement_bateau_5 (int mat[][MAX_COL],int nl, int nc, int*x,int*y, int *orientation);
void placement_bateau_5 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation);
int verif_placement_bateau_6 (int mat[][MAX_COL],int nl, int nc, int*x,int*y, int *orientation);
void placement_bateau_6 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation);
int bateau_coule_2 (int matjoueur[][MAX_COL], int mat2[][3],int nb_deux);
int bateau_coule_3 (int matjoueur[][MAX_COL], int mat3[][3],int nb_trois);
int bateau_coule_4 (int matjoueur[][MAX_COL], int mat4[][3],int nb_quatre);
int bateau_coule_5 (int matjoueur[][MAX_COL], int mat5[][3],int nb_cinq);
int bateau_coule_6 (int matjoueur[][MAX_COL], int mat6[][3],int nb_six);
void entree_ligne_colonne (int *x,int *y,int cpt_essais,int nl, int nc);
/****************************************************************************************/
/************************ Demande les dimensions de la matrice **************************/
/****************************************************************************************/
void grandeur_matrice (int *nl,int *nc)
{
printf ("Entrer un nombre de lignes compris entre %d et %d\n",MIN_LIG,MAX_LIG);
do
scanf ("%d",nl);
while (*nl<MIN_LIG||*nl>MAX_LIG);
printf ("Entrer un nombre de colonnes compris entre %d et %d\n",MIN_COL,MAX_COL);
do
scanf ("%d",nc);
while (*nc<MIN_COL||*nc>MAX_COL);
}
/****************************************************************************************/
/****************************** Remplit la matrice **************************************/
/****************************************************************************************/
void remplit_matrice (int mat [][MAX_COL],int nl,int nc,int x)
{
int i,j;
for (i=0;i<nl;i++)
{
for (j=0;j<nc;j++)
{
mat[j]=x;
}
}
}
/****************************************************************************************/
/********************************* Affiche la matrice *****************************************/
/****************************************************************************************/
void affiche_matrice(int mat [][MAX_COL],int nl,int nc)
{
int i,j;
printf (" ");
for (i=0;i<nc;i++)
{
printf ("%d ",i);
}
printf ("\n ");
for (i=0;i<nc;i++)
{
printf ("--");
}
printf ("\n");
for (i=0;i<nl;i++)
{
printf("%d|",i);
for (j=0;j<nc;j++)
{
if (mat[j]==0||mat[j]==2||mat[j]==3||mat[j]==4)
printf ("%d ",mat[j]);
else
printf(" ");
}
printf("|");
printf ("\n");
}
printf(" ");
for (i=0;i<nc;i++)
{
printf("--");
}
printf("\n");
}
/****************************************************************************************/
/********************************* Demande coordonnes ***********************************/
/****************************************************************************************/
void entree_ligne_colonne (int *x,int *y,int cpt_essais, int nl, int nc)
{
int ok;
do
{
printf("ligne,colonne (essai %d)\n",cpt_essais);
scanf("%d%d",x,y);
if (*x<nl&&*x>=0&&*y<nc&&*y>=0)
{
ok=1;
}
else
{
ok=0;
printf("erreur:ligne(0->%d) colonne(0->%d)\n",nl-1,nc-1);
}
}
while (ok!=1);
}
/****************************************************************************************/
/******************* Verifie si les bateaux selectionner entre sur la grille ************/
/****************************************************************************************/
//IN : nombre de 2,3,4 , nombre de lignes et colonnes
//RETURN : 1 ok, 0 ko
int verif_place (int nb_deux, int nb_trois,int nb_quatre,int nb_cinq,int nb_six,int nl,int nc)
{
if (nb_deux*2+nb_trois*3+nb_quatre*4>nl*nc/3)
{
return (0);
}
else
{
return (1);
}
}
/****************************************************************************************/
/************************ Selection de nombre de bateaux adverses ***********************/
/****************************************************************************************/
void intro_nbr_bateaux (int nl, int nc, int *nb_deux,int *nb_trois,int *nb_quatre,int *nb_cinq,int *nb_six)
{
int ok;
ok=1;
do
{
if(ok==0)
{
printf ("\ntrop de bateaux pour la taille de la matrice\n\n");
}
printf("Vous n'avez droit qu'a 6 bateaux adverses choisissez judicieusement\n\n");
printf ("Une Chaloupe est un navire de taille 2,\ncombien voulez-vous que l'ennemie en aie :\n");
scanf ("%d",nb_deux);
printf ("Un Escorteur est un navire de taille 3,\ncombien voulez-vous que l'ennemie en aie :\n");
scanf ("%d",nb_trois);
printf ("Un Dragueur de Mines est un navire de taille 4,\ncombien voulez-vous que l'ennemie en aie :\n");
scanf ("%d",nb_quatre);
printf ("Un Destroyeur est un navire de taille 5,\ncombien voulez-vous que l'ennemie en aie :\n");
scanf ("%d",nb_cinq);
printf ("Un Porte-Avions est un navire de taille 6,\ncombien voulez-vous que l'ennemie en aie :\n");
scanf ("%d",nb_six);
ok=verif_place(*nb_deux,*nb_trois,*nb_quatre,*nb_cinq,*nb_six,nl,nc);
}
while (ok==0);
}
/****************************************************************************************/
/************************* Verifie si des bateaux sont coules ***************************/
/****************************************************************************************/
//Return 1 : bateau coule Return 0 : rien.
int bateau_coule_2 (int matjoueur[][MAX_COL], int mat2[][3],int nb_deux) // batteau de 2 emplacements
{
int x,y,i,a;
a=0;
for(i=0;i<nb_deux;i++)
{
if (mat2[2]==1)
{
x=mat2[0];
y=mat2[1];
if (matjoueur[x][y]==2&&matjoueur[x-1][y]==2)
{
mat2[2]=0;//"efface" le bateau de la matrice ( ne passera plus dans les if )
a=1;
}
}
if (mat2[2]==2)
{
x=mat2[0];
y=mat2[1];
if (matjoueur[x][y]==2&&matjoueur[x][y+1]==2)
{
mat2[2]=0;//"efface le bateau de la matrice"
a=1;
}
}
}
if (a==0)
{
return (0);
}
else
return (1);
}
/******************************************************************************************/
int bateau_coule_3 (int matjoueur[][MAX_COL], int mat3[][3],int nb_trois) //bateau de 3 emplacements
{
int x,y,i,a;
a=0;
for(i=0;i<nb_trois;i++)
{
if (mat3[2]==1)
{
x=mat3[0];
y=mat3[1];
if (matjoueur[x][y]==3&&matjoueur[x-1][y]==3&&matjoueur[x-2][y]==3)
{
mat3[2]=0;//"efface le bateau de la matrice"
a=1;
}
}
}
}
if (a==0)
{
return (0);
}
else
return (1);
}
/*******************************************************************************************/
int bateau_coule_4 (int matjoueur[][MAX_COL], int mat4[][3],int nb_quatre) // bateau de 4 emplacements
{
int x,y,i,a;
a=0;
for(i=0;i<nb_quatre;i++)
{
if (mat4[2]==1)
{
x=mat4[0];
y=mat4[1];
if (matjoueur[x][y]==4&&matjoueur[x-1][y]==4&&matjoueur[x-2][y]==4&&matjoueur[x-3][y]==4)
{
mat4[2]=0;//"efface le bateau de la matrice"
a=1;
}
}
if (a==0)
{
return (0);
}
else
return (1);
}
/*************************************************************************************************/
int bateau_coule_5 (int matjoueur[][MAX_COL], int mat5[][3],int nb_cinq) // bateau de 5 emplacements
{
int x,y,i,a;
a=0;
for(i=0;i<nb_cinq;i++)
{
if (mat5[2]==1)
{
x=mat5[0];
y=mat5[1];
if (matjoueur[x][y]==5&&matjoueur[x-1][y]==5&&matjoueur[x-2][y]==5&&matjoueur[x-3][y]==5&&matjoueur[x-4][y]==5)
{
mat5[2]=0;//"efface le bateau de la matrice"
a=1;
}
}
}
if (a==0)
{
return (0);
}
else
return (1);
}
/*************************************************************************************************/
int bateau_coule_6 (int matjoueur[][MAX_COL], int mat6[][3],int nb_six) // bateau de 6 emplacements
{
int x,y,i,a;
a=0;
for(i=0;i<nb_six;i++)
{
if (mat6[2]==1)
{
x=mat6[0];
y=mat6[1];
if (matjoueur[x][y]==6&&matjoueur[x-1][y]==6&&matjoueur[x-2][y]==6&&matjoueur[x-3][y]==6&&matjoueur[x-4][y]==6&&matjoueur[x-5][y]==6)
{
mat6[2]=0;//"efface le bateau de la matrice"
a=1;
}
}
}
if (a==0)
{
return (0);
}
else
return (1);
}
/****************************************************************************************/
/******************************** Placement des bateaux *********************************/
/****************************************************************************************/
/****************************** Trouve un nombre aléatoire ******************************/
//IN : int max : maximum du nombre aléatoire
// Return : a :nombre aléatoire
int nombre_al (int max)
{
int a;
a=rand();
a=a%max+1;
return (a);
}
/** verifie que les elements d'un carre de la matrice est bien egal a zero avec le point superieur gauche et le point superieur droit.*/
// IN : Coordonees des 2 point : int x1,x2,y1,y2
// Return :1/0 : ok/ko
int verif_contour (int mat[][MAX_COL],int x1,int y1,int x2,int y2 )
{
int i,j,a;
a=1;
for (i=x1;i<=x2;i++)
{
if (a==0)
{
break;
}
for (j=y1;j<=y2;j++)
{
if (mat [j]==0)
{
a=1;
}
else
{
a=0;
break;
}
}
}
if (a==1)
{
return(1);
}
if (a==0)
{
return(0);
}
else
return(0);
}
/************* Trouve un point et verifie s'il peut y placer un bateau soit en vert ou horizontal ****/
//IN : int mat, int nl, int nc : matrice , nbre de lignes , nbre de colonnes
//OUT : int *x *y *orientation : point trouve ainsi que l'orientation du bateau
//RETURN : 1 : le bateau a reussi a etre place /0 : echec du placement
int verif_placement_bateau_2 (int mat[][MAX_COL],int nl, int nc, int*x,int*y,int *orientation)
{
int a;
*orientation=nombre_al(2);
*x=nombre_al(nl);
*y=nombre_al(nc);
*x=*x-1;
*y=*y-1;
//printf ("(%d,%d,%d) ",*x,*y,*orientation);
if (*orientation==1)//nord
{
if (*x==0)
{
return(0);
}
else if (*x==1&&*y==0)
{
if ((a=verif_contour(mat,0,0,2,1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==1&&*y==nc-1)
{
if ((a=verif_contour(mat,0,nc-2,2,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==1&&(*y!=0&&*y!=nc-1))
{
if ((a=verif_contour(mat,0,*y-1,2,*y+1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-3,0,nl-1,1))==1)
{
return(1);
}
else
return (0);
}
else if (*x==nl-1&&*y==nc-1)
{
if ((a=verif_contour(mat,nl-3,nc-2,nl-1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&(*y!=0&&*y!=nc-1))
{
if ((a=verif_contour(mat,nl-3,*y-1,nl-1,*y+1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==0&&(*x!=1&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-2,0,*x+1,1))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-2,*y-1,*x+1,*y+1))==1)
{
return(1);
}
else
return (0);
}
/// Orientation est
if (*orientation==2)
{
if (*y==nc-1)
{
return (0);
}
else if (*x==0&&*y==0)
{
if ((a=verif_contour(mat,0,0,1,2))==1)
{
return (1);
}
else
return (0);
}
else if (*x==0&&*y==nc-2)
{
if ((a=verif_contour(mat,0,nc-3,1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==nc-2&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,nc-3,*x+1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-2,0,nl-1,2))==1)
{
return(1);
}
else
return (0);
}
else if (*y==0&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,0,*x+1,2))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&(*y!=0&&*y!=nc-2))
{
if ((a=verif_contour(mat,nl-2,*y-1,nl-1,*y+2))==1)
{
return (1);
}
else
return (0);
}
else if (*x==0&&(*y!=0&&*y!=nc-2))
{
if ((a=verif_contour(mat,0,*y-1,1,*y+2))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-1,*y-1,*x+1,*y+2))==1)
{
return(1);
}
else
return (0);
}
else
return (0);
}
/*************************** Place les bateaux de taille 2 dans une matrice *************************/
//IN : int mat[][MAX_COL] : nom de la matrice , int nl,nc : nbre ligne et colonnes de la matrice
//OUT : int *x,*y,*orientation
void placement_bateau_2 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation)
{
int a;
do
{
a=verif_placement_bateau_2(mat,nl,nc,x,y,orientation);
}
while (a==0);
if (*orientation==1)
{
mat [*x][*y]=2;
mat [*x-1][*y]=2;
}
else
{
mat [*x][*y]=2;
mat [*x][*y+1]=2;
}
//printf(" ok\n");
}
/**************************** Verifie le placement de bateau de taille 3 *********************/
int verif_placement_bateau_3 (int mat[][MAX_COL],int nl, int nc, int*x,int*y,int *orientation)
{
int a;
*orientation=nombre_al(2);
*x=nombre_al(nl);
*y=nombre_al(nc);
*x=*x-1;
*y=*y-1;
//printf ("(%d,%d,%d) ",*x,*y,*orientation);
if (*orientation==1)//nord
{
if (*x==0||*x==1)
{
return(0);
}
else if (*x==2&&*y==0)
{
if ((a=verif_contour(mat,0,0,3,1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==2&&*y==nc-1)
{
if ((a=verif_contour(mat,0,nc-2,3,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==2&&(*y!=0&&*y!=nc-1))
{
if ((a=verif_contour(mat,0,*y-1,3,*y+1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-4,0,nl-1,1))==1)
{
return(1);
}
else
return (0);
}
else if (*x==nl-1&&*y==nc-1)
{
if ((a=verif_contour(mat,nl-4,nc-2,nl-1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==0&&(*x!=2&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-3,0,*x+1,1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==nc-1&&(*x!=2&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-3,nc-2,*x+1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-3,*y-1,*x+1,*y+1))==1)
{
return(1);
}
else
return (0);
}
/// Orientation est
if (*orientation==2)
{
if (*y==nc-1||*y==nc-2)
{
return (0);
}
else if (*x==0&&*y==0)
{
if ((a=verif_contour(mat,0,0,1,3))==1)
{
return (1);
}
else
return (0);
}
else if (*x==0&&*y==nc-3)
{
if ((a=verif_contour(mat,0,nc-4,1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==nc-3&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,nc-4,*x+1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-2,0,nl-1,3))==1)
{
return(1);
}
else
return (0);
}
else if (*x==nl-1&&*y==nc-3)
{
if ((a=verif_contour(mat,nl-2,nc-4,nl-1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==0&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,0,*x+1,3))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&(*y!=0&&*y!=nc-3))
{
if ((a=verif_contour(mat,nl-2,*y-1,nl-1,*y+3))==1)
{
return (1);
}
else
return (0);
}
else if (*x==0&&(*y!=0&&*y!=nc-3))
{
if ((a=verif_contour(mat,0,*y-1,1,*y+3))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-1,*y-1,*x+1,*y+3))==1)
{
return(1);
}
else
return (0);
}
else
return (0);
}
/***************************** Place les bateaux de taille 3 dans une matrice ********************/
//IN : int mat[][MAX_COL] : nom de la matrice , int nl,nc : nbre ligne et colonnes de la matrice
//OUT : int *x,*y,*orientation
void placement_bateau_3 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation)
{
int a;
do
{
a=verif_placement_bateau_3(mat,nl,nc,x,y,orientation);
}
while (a==0);
if (*orientation==1)
{
mat [*x][*y]=3;
mat [*x-1][*y]=3;
mat [*x-2][*y]=3;
}
else
{
mat [*x][*y]=3;
mat [*x][*y+1]=3;
mat [*x][*y+2]=3;
}
//printf(" ok\n");
}
/******************************* Verifie le placement de bateau de taille 4 *********************/
int verif_placement_bateau_4 (int mat[][MAX_COL],int nl, int nc, int*x,int*y,int *orientation)
{
int a;
*orientation=nombre_al(2);
*x=nombre_al(nl);
*y=nombre_al(nc);
*x=*x-1;
*y=*y-1;
//printf ("(%d,%d,%d) ",*x,*y,*orientation);
if (*orientation==1)//nord
{
if (*x==0||*x==1||*x==2)
{
return(0);
}
else if (*x==3&&*y==0)
{
if ((a=verif_contour(mat,0,0,4,1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==3&&*y==nc-1)
{
if ((a=verif_contour(mat,0,nc-2,4,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==3&&(*y!=0&&*y!=nc-1))
{
if ((a=verif_contour(mat,0,*y-1,4,*y+1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-5,0,nl-1,1))==1)
{
return(1);
}
else
return (0);
}
else if (*x==nl-1&&(*y!=0&&*y!=nc-1))
{
if ((a=verif_contour(mat,nl-5,*y-1,nl-1,*y+1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==0&&(*x!=3&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-4,0,*x+1,1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==nc-1&&(*x!=3&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-4,nc-2,*x+1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-4,*y-1,*x+1,*y+1))==1)
{
return(1);
}
else
return (0);
}
/// Orientation est
if (*orientation==2)
{
if (*y==nc-1||*y==nc-2||*y==nc-3)
{
return (0);
}
else if (*x==0&&*y==0)
{
if ((a=verif_contour(mat,0,0,1,4))==1)
{
return (1);
}
else
return (0);
}
else if (*x==0&&*y==nc-4)
{
if ((a=verif_contour(mat,0,nc-5,1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==nc-4&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,nc-5,*x+1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-2,0,nl-1,4))==1)
{
return(1);
}
else
return (0);
}
else if (*x==nl-1&&*y==nc-4)
{
if ((a=verif_contour(mat,nl-2,nc-5,nl-1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==0&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,0,*x+1,4))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&(*y!=0&&*y!=nc-4))
{
if ((a=verif_contour(mat,nl-2,*y-1,nl-1,*y+4))==1)
{
return (1);
}
else
return (0);
}
else if (*x==0&&(*y!=0&&*y!=nc-4))
{
if ((a=verif_contour(mat,0,*y-1,1,*y+4))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-1,*y-1,*x+1,*y+4))==1)
{
return(1);
}
else
return (0);
}
else
return (0);
}
/****************************** Place les bateaux de taille 4 dans une matrice **********************/
//IN : int mat[][MAX_COL] : nom de la matrice , int nl,nc : nbre ligne et colonnes de la matrice
// OUT : int *x,*y,*orientation
void placement_bateau_4 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation)
{
int a;
do
{
a=verif_placement_bateau_4(mat,nl,nc,x,y,orientation);
}
while (a==0);
if (*orientation==1)
{
mat [*x][*y]=4;
mat [*x-1][*y]=4;
mat [*x-2][*y]=4;
mat [*x-3][*y]=4;
}
else
{
mat [*x][*y]=4;
mat [*x][*y+1]=4;
mat [*x][*y+2]=4;
mat [*x][*y+3]=4;
}
//printf(" ok\n");
}
/***************************** Verifie placement bateau de taille 5 *********************/
int verif_placement_bateau_5 (int mat[][MAX_COL],int nl, int nc, int*x,int*y,int *orientation)
{
int a;
*orientation=nombre_al(2);
*x=nombre_al(nl);
*y=nombre_al(nc);
*x=*x-1;
*y=*y-1;
//printf ("(%d,%d,%d) ",*x,*y,*orientation);
if (*orientation==1)//nord
{
if (*x==0||*x==1||*x==2||*x==3)
{
return(0);
}
else if (*x==4&&*y==0)
{
if ((a=verif_contour(mat,0,0,5,1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==4&&*y==nc-1)
{
if ((a=verif_contour(mat,0,nc-2,5,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==4&&(*y!=0&&*y!=nc-1))
{
if ((a=verif_contour(mat,0,*y-1,5,*y+1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-6,0,nl-1,1))==1)
{
return(1);
}
else
return (0);
}
else if (*x==nl-1&&*y==nc-1)
{
if ((a=verif_contour(mat,nl-6,nc-2,nl-1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&(*y!=0&&*y!=nc-1))
{
if ((a=verif_contour(mat,nl-6,*y-1,nl-1,*y+1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==nc-1&&(*x!=4&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-5,nc-2,*x+1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-5,*y-1,*x+1,*y+1))==1)
{
return(1);
}
else
return (0);
}
/// Orientation est
if (*orientation==2)
{
if (*y==nc-1||*y==nc-2||*y==nc-3||*y==nc-4)
{
return (0);
}
else if (*x==0&&*y==0)
{
if ((a=verif_contour(mat,0,0,1,5))==1)
{
return (1);
}
else
return (0);
}
else if (*x==0&&*y==nc-5)
{
if ((a=verif_contour(mat,0,nc-5,1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==nc-5&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,nc-6,*x+1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-2,0,nl-1,5))==1)
{
return(1);
}
else
return (0);
}
else if (*x==nl-1&&*y==nc-5)
{
if ((a=verif_contour(mat,nl-2,nc-6,nl-1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==0&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,0,*x+1,5))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&(*y!=0&&*y!=nc-5))
{
if ((a=verif_contour(mat,nl-2,*y-1,nl-1,*y+5))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-1,*y-1,*x+1,*y+5))==1)
{
return(1);
}
else
return (0);
}
else
return (0);
}
/****************************** Place les bateaux de taille 5 dans une matrice **********************/
//IN : int mat[][MAX_COL] : nom de la matrice , int nl,nc : nbre ligne et colonnes de la matrice
// OUT : int *x,*y,*orientation
void placement_bateau_5 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation)
{
int a;
do
{
a=verif_placement_bateau_5(mat,nl,nc,x,y,orientation);
}
while (a==0);
if (*orientation==1)
{
mat [*x][*y]=5;
mat [*x-1][*y]=5;
mat [*x-2][*y]=5;
mat [*x-3][*y]=5;
mat [*x-4][*y]=5;
}
else
{
mat [*x][*y]=5;
mat [*x][*y+1]=5;
mat [*x][*y+2]=5;
mat [*x][*y+3]=5;
mat [*x][*y+4]=5;
}
//printf(" ok\n");
}
/***************************** Verifie placement bateau de taille 6 *********************/
int verif_placement_bateau_6 (int mat[][MAX_COL],int nl, int nc, int*x,int*y,int *orientation)
{
int a;
*orientation=nombre_al(2);
*x=nombre_al(nl);
*y=nombre_al(nc);
*x=*x-1;
*y=*y-1;
//printf ("(%d,%d,%d) ",*x,*y,*orientation);
if (*orientation==1)//nord
{
if (*x==0||*x==1||*x==2||*x==3||*x==4)
{
return(0);
}
else if (*x==5&&*y==0)
{
if ((a=verif_contour(mat,0,0,6,1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==5&&*y==nc-1)
{
if ((a=verif_contour(mat,0,nc-2,6,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-7,0,nl-1,1))==1)
{
return(1);
}
else
return (0);
}
else if (*x==nl-1&&*y==nc-1)
{
if ((a=verif_contour(mat,nl-7,nc-2,nl-1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&(*y!=0&&*y!=nc-1))
{
if ((a=verif_contour(mat,nl-7,*y-1,nl-1,*y+1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==0&&(*x!=5&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-6,0,*x+1,1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==nc-1&&(*x!=5&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-6,nc-2,*x+1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-6,*y-1,*x+1,*y+1))==1)
{
return(1);
}
else
return (0);
}
/// Orientation est
if (*orientation==2)
{
if (*y==nc-1||*y==nc-2||*y==nc-3||*y==nc-4||*y==nc-5)
{
return (0);
}
else if (*x==0&&*y==0)
{
if ((a=verif_contour(mat,0,0,1,6))==1)
{
return (1);
}
else
return (0);
}
else if (*y==nc-6&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,nc-7,*x+1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-2,0,nl-1,6))==1)
{
return(1);
}
else
return (0);
}
else if (*x==nl-1&&*y==nc-6)
{
if ((a=verif_contour(mat,nl-2,nc-7,nl-1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==0&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,0,*x+1,6))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&(*y!=0&&*y!=nc-6))
{
if ((a=verif_contour(mat,nl-2,*y-1,nl-1,*y+6))==1)
{
return (1);
}
else
return (0);
}
else if (*x==0&&(*y!=0&&*y!=nc-6))
{
if ((a=verif_contour(mat,0,*y-1,1,*y+6))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-1,*y-1,*x+1,*y+6))==1)
{
return(1);
}
else
return (0);
}
else
return (0);
}
/****************************** Place les bateaux de taille 6 dans une matrice **********************/
//IN : int mat[][MAX_COL] : nom de la matrice , int nl,nc : nbre ligne et colonnes de la matrice
// OUT : int *x,*y,*orientation
void placement_bateau_6 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation)
{
int a;
do
{
a=verif_placement_bateau_6(mat,nl,nc,x,y,orientation);
}
while (a==0);
if (*orientation==1)
{
mat [*x][*y]=6;
mat [*x-1][*y]=6;
mat [*x-2][*y]=6;
mat [*x-3][*y]=6;
mat [*x-4][*y]=6;
mat [*x-5][*y]=6;
}
else
{
mat [*x][*y]=6;
mat [*x][*y+1]=6;
mat [*x][*y+2]=6;
mat [*x][*y+3]=6;
mat [*x][*y+4]=6;
mat [*x][*y+5]=6;
}
//printf(" ok\n");
}
/****************************************************************************************/
/************************************** MAIN ********************************************/
/****************************************************************************************/
int main (void)
{
int matjoueur [MAX_LIG][MAX_COL];
int matordi [MAX_LIG][MAX_COL];
int mat2 [16][3];
int mat3 [11][3];
int mat4 [8][3];
int mat5 [6] [3];
int mat6 [5] [3];
int x,y,orientation;
int nl, nc ;
int nb_deux, nb_trois, nb_quatre, nb_cinq, nb_six;
int i,j,k;
int cpt_essais,cpt2,cpt3,cpt4,cpt5,cpt6;
int choix;
int fini=0;
i=1;
srand(time(NULL));
while (i==1)
{
system("cls");
printf ("******************************************************************\n");
printf ("************************* COMBAT NAVAL ***************************\n");
printf ("******************************************************************\n");
printf ("1 Jouer\n2 Regles\n3 Quiter\nVeuillez entrer un choix:");
scanf ("%d",&choix);
if (choix==1)
{
do
{
system("cls");
grandeur_matrice(&nl, &nc);
intro_nbr_bateaux (nl, nc, &nb_deux, &nb_trois, &nb_quatre, &nb_cinq, &nb_six);
remplit_matrice (matordi,nl, nc,0);
for (j=0;j<nb_six;j++)
{
placement_bateau_6 (matordi,nl,nc,&x,&y,&orientation);
mat6[j][0]=x;
mat6[j][1]=y;
mat6[j][2]=orientation;
}
for (j=0;j<nb_cinq;j++)
{
placement_bateau_5 (matordi,nl,nc,&x,&y,&orientation);
mat5[j][0]=x;
mat5[j][1]=y;
mat5[j][2]=orientation;
}
for (j=0;j<nb_quatre;j++)
{
placement_bateau_4 (matordi,nl,nc,&x,&y,&orientation);
mat4[j][0]=x;
mat4[j][1]=y;
mat4[j][2]=orientation;
}
for (j=0;j<nb_trois;j++)
{
placement_bateau_3 (matordi,nl,nc,&x,&y,&orientation);
mat3[j][0]=x;
mat3[j][1]=y;
mat3[j][2]=orientation;
}
for (j=0;j<nb_deux;j++)
{
placement_bateau_2 (matordi,nl,nc,&x,&y,&orientation);
mat2[j][0]=x;
mat2[j][1]=y;
mat2[j][2]=orientation;
}
//affiche_matrice (matordi,nl,nc);
system("cls");
remplit_matrice (matjoueur,nl,nc,9);
affiche_matrice (matjoueur,nl,nc);
cpt_essais=1;
cpt2=0;
cpt3=0;
cpt4=0;
cpt5=0;
cpt6=0;
do
{
entree_ligne_colonne(&x,&y,cpt_essais,nl,nc);
system("cls");
if (matordi[x][y]==2)
{
matjoueur[x][y]=2;
printf("bateau de 2 touche!\n");
}
if (matordi[x][y]==3)
{
matjoueur[x][y]=3;
if((k=bateau_coule_3(matjoueur,mat3,nb_trois))==1)
{
printf ("Bateau coule!!\n\a");
cpt3++;
}
else
printf("bateau de 3 touche!\n");
}
if (matordi[x][y]==4)
{
matjoueur[x][y]=4;
printf("bateau de 4 touche!\n");
}
if (matordi[x][y]==5)
{
matjoueur[x][y]=5;
printf("bateau de 5 touche!\n");
}
if (matordi[x][y]==6)
{
matjoueur[x][y]=6;
if((k=bateau_coule_6(matjoueur,mat6,nb_six))==1)
{
printf ("Bateau coule!!\n\a");
cpt6++;
}
else
printf("bateau de 6 touche!\n");
}
if (matordi[x][y]==0)
{
matjoueur[x][y]=0;
printf("Plouf!\n");
}
printf("\n%d bateau(x) de 2 coule(s) sur %d\n%d bateau(x) de 3 coule(s) sur %d\n%d bateau(x) de 4 coule(s) sur %d\n%d bateau(x) de 5 coule(s) sur %d\n%d bateau(x) de 6 coule(s) sur %d\n\n",cpt2,nb_deux,cpt3,nb_trois,cpt4,nb_quatre,cpt5,nb_cinq,cpt6,nb_six);
affiche_matrice(matjoueur,nl,nc);
cpt_essais++;
if (cpt2+cpt3+cpt4+cpt5+cpt6==nb_deux+nb_trois+nb_quatre+nb_cinq+nb_six)
{
break;
}
}
while (cpt2+cpt3+cpt4+cpt5+cpt6==nb_deux+nb_trois+nb_quatre+nb_cinq+nb_six);
printf("Bravo, vous avez coule tous les bateaux en %d essais\n\a\a\a",cpt_essais-1);
printf("Vous avez detruit tout les navires avant qu'il n'ait eut le temps de prevenir leur base,\nYOU ARE THE WINNER!!!!!!\n\n");
}
while (fini!=1);
}
if (choix==2)
{
printf ("\nLe but du jeu est de couler tous les navires\n\nAu depart, l'ordinateur demande d'entrer la taille de la grille de jeu.\n");
printf ("Ensuite il placera les bateaux aleatoirement dans la grilles\nselon la regle suivante:\n\n");
printf ("- deux bateaux ne peuvent pas se toucher meme par un coin,\n- ils peuvent toucher les bords de la grille\n\n");
printf ("Le jeu sera fini quand tous les bateaux seront coules.\n\n");
system("pause");
}
else
i=0;
}
return 0;
}[/cpp]
je dois créer une petite bataille navale en c dans le cadre de mon DUT reseaux et télécommunication nous utilisons Code::Blocks, j'arrive a compiler mais je n'arrive pas a jouer, aidez moi svp.
le source:[cpp]
#include <stdio.h>
#include <tpinfo.h>
#include <stdlib.h>
#include <time.h>
int MAX_LIG=10;
int MIN_LIG=6;
int MAX_COL=10;
int MIN_COL=6;
void grandeur_matrice (int *nl,int *nc);
void remplit_matrice (int mat [][MAX_COL],int nl,int nc,int x);
void affiche_matrice(int mat [][MAX_COL],int nl,int nc);
int nombre_al (int max);
void intro_nbr_bateaux (int nl, int nc, int *nb_deux,int *nb_trois,int *nb_quatre,int *nb_cinq,int *nb_six);
int verif_place (int nb_deux, int nb_trois,int nb_quatre,int nb_cinq,int nb_six,int nl,int nc);
int verif_placement_bateau_2 (int mat[][MAX_COL],int nl, int nc, int*x,int*y, int *orientation);
void placement_bateau_2 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation);
int verif_contour (int mat[][MAX_COL],int x1,int y1,int x2,int y2 );
int verif_placement_bateau_3 (int mat[][MAX_COL],int nl, int nc, int*x,int*y, int *orientation);
void placement_bateau_3 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation);
int verif_placement_bateau_4 (int mat[][MAX_COL],int nl, int nc, int*x,int*y, int *orientation);
void placement_bateau_4 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation);
int verif_placement_bateau_5 (int mat[][MAX_COL],int nl, int nc, int*x,int*y, int *orientation);
void placement_bateau_5 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation);
int verif_placement_bateau_6 (int mat[][MAX_COL],int nl, int nc, int*x,int*y, int *orientation);
void placement_bateau_6 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation);
int bateau_coule_2 (int matjoueur[][MAX_COL], int mat2[][3],int nb_deux);
int bateau_coule_3 (int matjoueur[][MAX_COL], int mat3[][3],int nb_trois);
int bateau_coule_4 (int matjoueur[][MAX_COL], int mat4[][3],int nb_quatre);
int bateau_coule_5 (int matjoueur[][MAX_COL], int mat5[][3],int nb_cinq);
int bateau_coule_6 (int matjoueur[][MAX_COL], int mat6[][3],int nb_six);
void entree_ligne_colonne (int *x,int *y,int cpt_essais,int nl, int nc);
/****************************************************************************************/
/************************ Demande les dimensions de la matrice **************************/
/****************************************************************************************/
void grandeur_matrice (int *nl,int *nc)
{
printf ("Entrer un nombre de lignes compris entre %d et %d\n",MIN_LIG,MAX_LIG);
do
scanf ("%d",nl);
while (*nl<MIN_LIG||*nl>MAX_LIG);
printf ("Entrer un nombre de colonnes compris entre %d et %d\n",MIN_COL,MAX_COL);
do
scanf ("%d",nc);
while (*nc<MIN_COL||*nc>MAX_COL);
}
/****************************************************************************************/
/****************************** Remplit la matrice **************************************/
/****************************************************************************************/
void remplit_matrice (int mat [][MAX_COL],int nl,int nc,int x)
{
int i,j;
for (i=0;i<nl;i++)
{
for (j=0;j<nc;j++)
{
mat[j]=x;
}
}
}
/****************************************************************************************/
/********************************* Affiche la matrice *****************************************/
/****************************************************************************************/
void affiche_matrice(int mat [][MAX_COL],int nl,int nc)
{
int i,j;
printf (" ");
for (i=0;i<nc;i++)
{
printf ("%d ",i);
}
printf ("\n ");
for (i=0;i<nc;i++)
{
printf ("--");
}
printf ("\n");
for (i=0;i<nl;i++)
{
printf("%d|",i);
for (j=0;j<nc;j++)
{
if (mat[j]==0||mat[j]==2||mat[j]==3||mat[j]==4)
printf ("%d ",mat[j]);
else
printf(" ");
}
printf("|");
printf ("\n");
}
printf(" ");
for (i=0;i<nc;i++)
{
printf("--");
}
printf("\n");
}
/****************************************************************************************/
/********************************* Demande coordonnes ***********************************/
/****************************************************************************************/
void entree_ligne_colonne (int *x,int *y,int cpt_essais, int nl, int nc)
{
int ok;
do
{
printf("ligne,colonne (essai %d)\n",cpt_essais);
scanf("%d%d",x,y);
if (*x<nl&&*x>=0&&*y<nc&&*y>=0)
{
ok=1;
}
else
{
ok=0;
printf("erreur:ligne(0->%d) colonne(0->%d)\n",nl-1,nc-1);
}
}
while (ok!=1);
}
/****************************************************************************************/
/******************* Verifie si les bateaux selectionner entre sur la grille ************/
/****************************************************************************************/
//IN : nombre de 2,3,4 , nombre de lignes et colonnes
//RETURN : 1 ok, 0 ko
int verif_place (int nb_deux, int nb_trois,int nb_quatre,int nb_cinq,int nb_six,int nl,int nc)
{
if (nb_deux*2+nb_trois*3+nb_quatre*4>nl*nc/3)
{
return (0);
}
else
{
return (1);
}
}
/****************************************************************************************/
/************************ Selection de nombre de bateaux adverses ***********************/
/****************************************************************************************/
void intro_nbr_bateaux (int nl, int nc, int *nb_deux,int *nb_trois,int *nb_quatre,int *nb_cinq,int *nb_six)
{
int ok;
ok=1;
do
{
if(ok==0)
{
printf ("\ntrop de bateaux pour la taille de la matrice\n\n");
}
printf("Vous n'avez droit qu'a 6 bateaux adverses choisissez judicieusement\n\n");
printf ("Une Chaloupe est un navire de taille 2,\ncombien voulez-vous que l'ennemie en aie :\n");
scanf ("%d",nb_deux);
printf ("Un Escorteur est un navire de taille 3,\ncombien voulez-vous que l'ennemie en aie :\n");
scanf ("%d",nb_trois);
printf ("Un Dragueur de Mines est un navire de taille 4,\ncombien voulez-vous que l'ennemie en aie :\n");
scanf ("%d",nb_quatre);
printf ("Un Destroyeur est un navire de taille 5,\ncombien voulez-vous que l'ennemie en aie :\n");
scanf ("%d",nb_cinq);
printf ("Un Porte-Avions est un navire de taille 6,\ncombien voulez-vous que l'ennemie en aie :\n");
scanf ("%d",nb_six);
ok=verif_place(*nb_deux,*nb_trois,*nb_quatre,*nb_cinq,*nb_six,nl,nc);
}
while (ok==0);
}
/****************************************************************************************/
/************************* Verifie si des bateaux sont coules ***************************/
/****************************************************************************************/
//Return 1 : bateau coule Return 0 : rien.
int bateau_coule_2 (int matjoueur[][MAX_COL], int mat2[][3],int nb_deux) // batteau de 2 emplacements
{
int x,y,i,a;
a=0;
for(i=0;i<nb_deux;i++)
{
if (mat2[2]==1)
{
x=mat2[0];
y=mat2[1];
if (matjoueur[x][y]==2&&matjoueur[x-1][y]==2)
{
mat2[2]=0;//"efface" le bateau de la matrice ( ne passera plus dans les if )
a=1;
}
}
if (mat2[2]==2)
{
x=mat2[0];
y=mat2[1];
if (matjoueur[x][y]==2&&matjoueur[x][y+1]==2)
{
mat2[2]=0;//"efface le bateau de la matrice"
a=1;
}
}
}
if (a==0)
{
return (0);
}
else
return (1);
}
/******************************************************************************************/
int bateau_coule_3 (int matjoueur[][MAX_COL], int mat3[][3],int nb_trois) //bateau de 3 emplacements
{
int x,y,i,a;
a=0;
for(i=0;i<nb_trois;i++)
{
if (mat3[2]==1)
{
x=mat3[0];
y=mat3[1];
if (matjoueur[x][y]==3&&matjoueur[x-1][y]==3&&matjoueur[x-2][y]==3)
{
mat3[2]=0;//"efface le bateau de la matrice"
a=1;
}
}
}
}
if (a==0)
{
return (0);
}
else
return (1);
}
/*******************************************************************************************/
int bateau_coule_4 (int matjoueur[][MAX_COL], int mat4[][3],int nb_quatre) // bateau de 4 emplacements
{
int x,y,i,a;
a=0;
for(i=0;i<nb_quatre;i++)
{
if (mat4[2]==1)
{
x=mat4[0];
y=mat4[1];
if (matjoueur[x][y]==4&&matjoueur[x-1][y]==4&&matjoueur[x-2][y]==4&&matjoueur[x-3][y]==4)
{
mat4[2]=0;//"efface le bateau de la matrice"
a=1;
}
}
if (a==0)
{
return (0);
}
else
return (1);
}
/*************************************************************************************************/
int bateau_coule_5 (int matjoueur[][MAX_COL], int mat5[][3],int nb_cinq) // bateau de 5 emplacements
{
int x,y,i,a;
a=0;
for(i=0;i<nb_cinq;i++)
{
if (mat5[2]==1)
{
x=mat5[0];
y=mat5[1];
if (matjoueur[x][y]==5&&matjoueur[x-1][y]==5&&matjoueur[x-2][y]==5&&matjoueur[x-3][y]==5&&matjoueur[x-4][y]==5)
{
mat5[2]=0;//"efface le bateau de la matrice"
a=1;
}
}
}
if (a==0)
{
return (0);
}
else
return (1);
}
/*************************************************************************************************/
int bateau_coule_6 (int matjoueur[][MAX_COL], int mat6[][3],int nb_six) // bateau de 6 emplacements
{
int x,y,i,a;
a=0;
for(i=0;i<nb_six;i++)
{
if (mat6[2]==1)
{
x=mat6[0];
y=mat6[1];
if (matjoueur[x][y]==6&&matjoueur[x-1][y]==6&&matjoueur[x-2][y]==6&&matjoueur[x-3][y]==6&&matjoueur[x-4][y]==6&&matjoueur[x-5][y]==6)
{
mat6[2]=0;//"efface le bateau de la matrice"
a=1;
}
}
}
if (a==0)
{
return (0);
}
else
return (1);
}
/****************************************************************************************/
/******************************** Placement des bateaux *********************************/
/****************************************************************************************/
/****************************** Trouve un nombre aléatoire ******************************/
//IN : int max : maximum du nombre aléatoire
// Return : a :nombre aléatoire
int nombre_al (int max)
{
int a;
a=rand();
a=a%max+1;
return (a);
}
/** verifie que les elements d'un carre de la matrice est bien egal a zero avec le point superieur gauche et le point superieur droit.*/
// IN : Coordonees des 2 point : int x1,x2,y1,y2
// Return :1/0 : ok/ko
int verif_contour (int mat[][MAX_COL],int x1,int y1,int x2,int y2 )
{
int i,j,a;
a=1;
for (i=x1;i<=x2;i++)
{
if (a==0)
{
break;
}
for (j=y1;j<=y2;j++)
{
if (mat [j]==0)
{
a=1;
}
else
{
a=0;
break;
}
}
}
if (a==1)
{
return(1);
}
if (a==0)
{
return(0);
}
else
return(0);
}
/************* Trouve un point et verifie s'il peut y placer un bateau soit en vert ou horizontal ****/
//IN : int mat, int nl, int nc : matrice , nbre de lignes , nbre de colonnes
//OUT : int *x *y *orientation : point trouve ainsi que l'orientation du bateau
//RETURN : 1 : le bateau a reussi a etre place /0 : echec du placement
int verif_placement_bateau_2 (int mat[][MAX_COL],int nl, int nc, int*x,int*y,int *orientation)
{
int a;
*orientation=nombre_al(2);
*x=nombre_al(nl);
*y=nombre_al(nc);
*x=*x-1;
*y=*y-1;
//printf ("(%d,%d,%d) ",*x,*y,*orientation);
if (*orientation==1)//nord
{
if (*x==0)
{
return(0);
}
else if (*x==1&&*y==0)
{
if ((a=verif_contour(mat,0,0,2,1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==1&&*y==nc-1)
{
if ((a=verif_contour(mat,0,nc-2,2,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==1&&(*y!=0&&*y!=nc-1))
{
if ((a=verif_contour(mat,0,*y-1,2,*y+1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-3,0,nl-1,1))==1)
{
return(1);
}
else
return (0);
}
else if (*x==nl-1&&*y==nc-1)
{
if ((a=verif_contour(mat,nl-3,nc-2,nl-1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&(*y!=0&&*y!=nc-1))
{
if ((a=verif_contour(mat,nl-3,*y-1,nl-1,*y+1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==0&&(*x!=1&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-2,0,*x+1,1))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-2,*y-1,*x+1,*y+1))==1)
{
return(1);
}
else
return (0);
}
/// Orientation est
if (*orientation==2)
{
if (*y==nc-1)
{
return (0);
}
else if (*x==0&&*y==0)
{
if ((a=verif_contour(mat,0,0,1,2))==1)
{
return (1);
}
else
return (0);
}
else if (*x==0&&*y==nc-2)
{
if ((a=verif_contour(mat,0,nc-3,1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==nc-2&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,nc-3,*x+1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-2,0,nl-1,2))==1)
{
return(1);
}
else
return (0);
}
else if (*y==0&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,0,*x+1,2))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&(*y!=0&&*y!=nc-2))
{
if ((a=verif_contour(mat,nl-2,*y-1,nl-1,*y+2))==1)
{
return (1);
}
else
return (0);
}
else if (*x==0&&(*y!=0&&*y!=nc-2))
{
if ((a=verif_contour(mat,0,*y-1,1,*y+2))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-1,*y-1,*x+1,*y+2))==1)
{
return(1);
}
else
return (0);
}
else
return (0);
}
/*************************** Place les bateaux de taille 2 dans une matrice *************************/
//IN : int mat[][MAX_COL] : nom de la matrice , int nl,nc : nbre ligne et colonnes de la matrice
//OUT : int *x,*y,*orientation
void placement_bateau_2 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation)
{
int a;
do
{
a=verif_placement_bateau_2(mat,nl,nc,x,y,orientation);
}
while (a==0);
if (*orientation==1)
{
mat [*x][*y]=2;
mat [*x-1][*y]=2;
}
else
{
mat [*x][*y]=2;
mat [*x][*y+1]=2;
}
//printf(" ok\n");
}
/**************************** Verifie le placement de bateau de taille 3 *********************/
int verif_placement_bateau_3 (int mat[][MAX_COL],int nl, int nc, int*x,int*y,int *orientation)
{
int a;
*orientation=nombre_al(2);
*x=nombre_al(nl);
*y=nombre_al(nc);
*x=*x-1;
*y=*y-1;
//printf ("(%d,%d,%d) ",*x,*y,*orientation);
if (*orientation==1)//nord
{
if (*x==0||*x==1)
{
return(0);
}
else if (*x==2&&*y==0)
{
if ((a=verif_contour(mat,0,0,3,1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==2&&*y==nc-1)
{
if ((a=verif_contour(mat,0,nc-2,3,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==2&&(*y!=0&&*y!=nc-1))
{
if ((a=verif_contour(mat,0,*y-1,3,*y+1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-4,0,nl-1,1))==1)
{
return(1);
}
else
return (0);
}
else if (*x==nl-1&&*y==nc-1)
{
if ((a=verif_contour(mat,nl-4,nc-2,nl-1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==0&&(*x!=2&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-3,0,*x+1,1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==nc-1&&(*x!=2&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-3,nc-2,*x+1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-3,*y-1,*x+1,*y+1))==1)
{
return(1);
}
else
return (0);
}
/// Orientation est
if (*orientation==2)
{
if (*y==nc-1||*y==nc-2)
{
return (0);
}
else if (*x==0&&*y==0)
{
if ((a=verif_contour(mat,0,0,1,3))==1)
{
return (1);
}
else
return (0);
}
else if (*x==0&&*y==nc-3)
{
if ((a=verif_contour(mat,0,nc-4,1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==nc-3&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,nc-4,*x+1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-2,0,nl-1,3))==1)
{
return(1);
}
else
return (0);
}
else if (*x==nl-1&&*y==nc-3)
{
if ((a=verif_contour(mat,nl-2,nc-4,nl-1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==0&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,0,*x+1,3))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&(*y!=0&&*y!=nc-3))
{
if ((a=verif_contour(mat,nl-2,*y-1,nl-1,*y+3))==1)
{
return (1);
}
else
return (0);
}
else if (*x==0&&(*y!=0&&*y!=nc-3))
{
if ((a=verif_contour(mat,0,*y-1,1,*y+3))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-1,*y-1,*x+1,*y+3))==1)
{
return(1);
}
else
return (0);
}
else
return (0);
}
/***************************** Place les bateaux de taille 3 dans une matrice ********************/
//IN : int mat[][MAX_COL] : nom de la matrice , int nl,nc : nbre ligne et colonnes de la matrice
//OUT : int *x,*y,*orientation
void placement_bateau_3 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation)
{
int a;
do
{
a=verif_placement_bateau_3(mat,nl,nc,x,y,orientation);
}
while (a==0);
if (*orientation==1)
{
mat [*x][*y]=3;
mat [*x-1][*y]=3;
mat [*x-2][*y]=3;
}
else
{
mat [*x][*y]=3;
mat [*x][*y+1]=3;
mat [*x][*y+2]=3;
}
//printf(" ok\n");
}
/******************************* Verifie le placement de bateau de taille 4 *********************/
int verif_placement_bateau_4 (int mat[][MAX_COL],int nl, int nc, int*x,int*y,int *orientation)
{
int a;
*orientation=nombre_al(2);
*x=nombre_al(nl);
*y=nombre_al(nc);
*x=*x-1;
*y=*y-1;
//printf ("(%d,%d,%d) ",*x,*y,*orientation);
if (*orientation==1)//nord
{
if (*x==0||*x==1||*x==2)
{
return(0);
}
else if (*x==3&&*y==0)
{
if ((a=verif_contour(mat,0,0,4,1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==3&&*y==nc-1)
{
if ((a=verif_contour(mat,0,nc-2,4,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==3&&(*y!=0&&*y!=nc-1))
{
if ((a=verif_contour(mat,0,*y-1,4,*y+1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-5,0,nl-1,1))==1)
{
return(1);
}
else
return (0);
}
else if (*x==nl-1&&(*y!=0&&*y!=nc-1))
{
if ((a=verif_contour(mat,nl-5,*y-1,nl-1,*y+1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==0&&(*x!=3&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-4,0,*x+1,1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==nc-1&&(*x!=3&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-4,nc-2,*x+1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-4,*y-1,*x+1,*y+1))==1)
{
return(1);
}
else
return (0);
}
/// Orientation est
if (*orientation==2)
{
if (*y==nc-1||*y==nc-2||*y==nc-3)
{
return (0);
}
else if (*x==0&&*y==0)
{
if ((a=verif_contour(mat,0,0,1,4))==1)
{
return (1);
}
else
return (0);
}
else if (*x==0&&*y==nc-4)
{
if ((a=verif_contour(mat,0,nc-5,1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==nc-4&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,nc-5,*x+1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-2,0,nl-1,4))==1)
{
return(1);
}
else
return (0);
}
else if (*x==nl-1&&*y==nc-4)
{
if ((a=verif_contour(mat,nl-2,nc-5,nl-1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==0&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,0,*x+1,4))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&(*y!=0&&*y!=nc-4))
{
if ((a=verif_contour(mat,nl-2,*y-1,nl-1,*y+4))==1)
{
return (1);
}
else
return (0);
}
else if (*x==0&&(*y!=0&&*y!=nc-4))
{
if ((a=verif_contour(mat,0,*y-1,1,*y+4))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-1,*y-1,*x+1,*y+4))==1)
{
return(1);
}
else
return (0);
}
else
return (0);
}
/****************************** Place les bateaux de taille 4 dans une matrice **********************/
//IN : int mat[][MAX_COL] : nom de la matrice , int nl,nc : nbre ligne et colonnes de la matrice
// OUT : int *x,*y,*orientation
void placement_bateau_4 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation)
{
int a;
do
{
a=verif_placement_bateau_4(mat,nl,nc,x,y,orientation);
}
while (a==0);
if (*orientation==1)
{
mat [*x][*y]=4;
mat [*x-1][*y]=4;
mat [*x-2][*y]=4;
mat [*x-3][*y]=4;
}
else
{
mat [*x][*y]=4;
mat [*x][*y+1]=4;
mat [*x][*y+2]=4;
mat [*x][*y+3]=4;
}
//printf(" ok\n");
}
/***************************** Verifie placement bateau de taille 5 *********************/
int verif_placement_bateau_5 (int mat[][MAX_COL],int nl, int nc, int*x,int*y,int *orientation)
{
int a;
*orientation=nombre_al(2);
*x=nombre_al(nl);
*y=nombre_al(nc);
*x=*x-1;
*y=*y-1;
//printf ("(%d,%d,%d) ",*x,*y,*orientation);
if (*orientation==1)//nord
{
if (*x==0||*x==1||*x==2||*x==3)
{
return(0);
}
else if (*x==4&&*y==0)
{
if ((a=verif_contour(mat,0,0,5,1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==4&&*y==nc-1)
{
if ((a=verif_contour(mat,0,nc-2,5,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==4&&(*y!=0&&*y!=nc-1))
{
if ((a=verif_contour(mat,0,*y-1,5,*y+1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-6,0,nl-1,1))==1)
{
return(1);
}
else
return (0);
}
else if (*x==nl-1&&*y==nc-1)
{
if ((a=verif_contour(mat,nl-6,nc-2,nl-1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&(*y!=0&&*y!=nc-1))
{
if ((a=verif_contour(mat,nl-6,*y-1,nl-1,*y+1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==nc-1&&(*x!=4&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-5,nc-2,*x+1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-5,*y-1,*x+1,*y+1))==1)
{
return(1);
}
else
return (0);
}
/// Orientation est
if (*orientation==2)
{
if (*y==nc-1||*y==nc-2||*y==nc-3||*y==nc-4)
{
return (0);
}
else if (*x==0&&*y==0)
{
if ((a=verif_contour(mat,0,0,1,5))==1)
{
return (1);
}
else
return (0);
}
else if (*x==0&&*y==nc-5)
{
if ((a=verif_contour(mat,0,nc-5,1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==nc-5&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,nc-6,*x+1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-2,0,nl-1,5))==1)
{
return(1);
}
else
return (0);
}
else if (*x==nl-1&&*y==nc-5)
{
if ((a=verif_contour(mat,nl-2,nc-6,nl-1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==0&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,0,*x+1,5))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&(*y!=0&&*y!=nc-5))
{
if ((a=verif_contour(mat,nl-2,*y-1,nl-1,*y+5))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-1,*y-1,*x+1,*y+5))==1)
{
return(1);
}
else
return (0);
}
else
return (0);
}
/****************************** Place les bateaux de taille 5 dans une matrice **********************/
//IN : int mat[][MAX_COL] : nom de la matrice , int nl,nc : nbre ligne et colonnes de la matrice
// OUT : int *x,*y,*orientation
void placement_bateau_5 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation)
{
int a;
do
{
a=verif_placement_bateau_5(mat,nl,nc,x,y,orientation);
}
while (a==0);
if (*orientation==1)
{
mat [*x][*y]=5;
mat [*x-1][*y]=5;
mat [*x-2][*y]=5;
mat [*x-3][*y]=5;
mat [*x-4][*y]=5;
}
else
{
mat [*x][*y]=5;
mat [*x][*y+1]=5;
mat [*x][*y+2]=5;
mat [*x][*y+3]=5;
mat [*x][*y+4]=5;
}
//printf(" ok\n");
}
/***************************** Verifie placement bateau de taille 6 *********************/
int verif_placement_bateau_6 (int mat[][MAX_COL],int nl, int nc, int*x,int*y,int *orientation)
{
int a;
*orientation=nombre_al(2);
*x=nombre_al(nl);
*y=nombre_al(nc);
*x=*x-1;
*y=*y-1;
//printf ("(%d,%d,%d) ",*x,*y,*orientation);
if (*orientation==1)//nord
{
if (*x==0||*x==1||*x==2||*x==3||*x==4)
{
return(0);
}
else if (*x==5&&*y==0)
{
if ((a=verif_contour(mat,0,0,6,1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==5&&*y==nc-1)
{
if ((a=verif_contour(mat,0,nc-2,6,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-7,0,nl-1,1))==1)
{
return(1);
}
else
return (0);
}
else if (*x==nl-1&&*y==nc-1)
{
if ((a=verif_contour(mat,nl-7,nc-2,nl-1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&(*y!=0&&*y!=nc-1))
{
if ((a=verif_contour(mat,nl-7,*y-1,nl-1,*y+1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==0&&(*x!=5&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-6,0,*x+1,1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==nc-1&&(*x!=5&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-6,nc-2,*x+1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-6,*y-1,*x+1,*y+1))==1)
{
return(1);
}
else
return (0);
}
/// Orientation est
if (*orientation==2)
{
if (*y==nc-1||*y==nc-2||*y==nc-3||*y==nc-4||*y==nc-5)
{
return (0);
}
else if (*x==0&&*y==0)
{
if ((a=verif_contour(mat,0,0,1,6))==1)
{
return (1);
}
else
return (0);
}
else if (*y==nc-6&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,nc-7,*x+1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&*y==0)
{
if((a=verif_contour(mat,nl-2,0,nl-1,6))==1)
{
return(1);
}
else
return (0);
}
else if (*x==nl-1&&*y==nc-6)
{
if ((a=verif_contour(mat,nl-2,nc-7,nl-1,nc-1))==1)
{
return (1);
}
else
return (0);
}
else if (*y==0&&(*x!=0&&*x!=nl-1))
{
if ((a=verif_contour(mat,*x-1,0,*x+1,6))==1)
{
return (1);
}
else
return (0);
}
else if (*x==nl-1&&(*y!=0&&*y!=nc-6))
{
if ((a=verif_contour(mat,nl-2,*y-1,nl-1,*y+6))==1)
{
return (1);
}
else
return (0);
}
else if (*x==0&&(*y!=0&&*y!=nc-6))
{
if ((a=verif_contour(mat,0,*y-1,1,*y+6))==1)
{
return (1);
}
else
return (0);
}
else if ((a=verif_contour(mat,*x-1,*y-1,*x+1,*y+6))==1)
{
return(1);
}
else
return (0);
}
else
return (0);
}
/****************************** Place les bateaux de taille 6 dans une matrice **********************/
//IN : int mat[][MAX_COL] : nom de la matrice , int nl,nc : nbre ligne et colonnes de la matrice
// OUT : int *x,*y,*orientation
void placement_bateau_6 (int mat[][MAX_COL], int nl, int nc,int*x,int*y,int*orientation)
{
int a;
do
{
a=verif_placement_bateau_6(mat,nl,nc,x,y,orientation);
}
while (a==0);
if (*orientation==1)
{
mat [*x][*y]=6;
mat [*x-1][*y]=6;
mat [*x-2][*y]=6;
mat [*x-3][*y]=6;
mat [*x-4][*y]=6;
mat [*x-5][*y]=6;
}
else
{
mat [*x][*y]=6;
mat [*x][*y+1]=6;
mat [*x][*y+2]=6;
mat [*x][*y+3]=6;
mat [*x][*y+4]=6;
mat [*x][*y+5]=6;
}
//printf(" ok\n");
}
/****************************************************************************************/
/************************************** MAIN ********************************************/
/****************************************************************************************/
int main (void)
{
int matjoueur [MAX_LIG][MAX_COL];
int matordi [MAX_LIG][MAX_COL];
int mat2 [16][3];
int mat3 [11][3];
int mat4 [8][3];
int mat5 [6] [3];
int mat6 [5] [3];
int x,y,orientation;
int nl, nc ;
int nb_deux, nb_trois, nb_quatre, nb_cinq, nb_six;
int i,j,k;
int cpt_essais,cpt2,cpt3,cpt4,cpt5,cpt6;
int choix;
int fini=0;
i=1;
srand(time(NULL));
while (i==1)
{
system("cls");
printf ("******************************************************************\n");
printf ("************************* COMBAT NAVAL ***************************\n");
printf ("******************************************************************\n");
printf ("1 Jouer\n2 Regles\n3 Quiter\nVeuillez entrer un choix:");
scanf ("%d",&choix);
if (choix==1)
{
do
{
system("cls");
grandeur_matrice(&nl, &nc);
intro_nbr_bateaux (nl, nc, &nb_deux, &nb_trois, &nb_quatre, &nb_cinq, &nb_six);
remplit_matrice (matordi,nl, nc,0);
for (j=0;j<nb_six;j++)
{
placement_bateau_6 (matordi,nl,nc,&x,&y,&orientation);
mat6[j][0]=x;
mat6[j][1]=y;
mat6[j][2]=orientation;
}
for (j=0;j<nb_cinq;j++)
{
placement_bateau_5 (matordi,nl,nc,&x,&y,&orientation);
mat5[j][0]=x;
mat5[j][1]=y;
mat5[j][2]=orientation;
}
for (j=0;j<nb_quatre;j++)
{
placement_bateau_4 (matordi,nl,nc,&x,&y,&orientation);
mat4[j][0]=x;
mat4[j][1]=y;
mat4[j][2]=orientation;
}
for (j=0;j<nb_trois;j++)
{
placement_bateau_3 (matordi,nl,nc,&x,&y,&orientation);
mat3[j][0]=x;
mat3[j][1]=y;
mat3[j][2]=orientation;
}
for (j=0;j<nb_deux;j++)
{
placement_bateau_2 (matordi,nl,nc,&x,&y,&orientation);
mat2[j][0]=x;
mat2[j][1]=y;
mat2[j][2]=orientation;
}
//affiche_matrice (matordi,nl,nc);
system("cls");
remplit_matrice (matjoueur,nl,nc,9);
affiche_matrice (matjoueur,nl,nc);
cpt_essais=1;
cpt2=0;
cpt3=0;
cpt4=0;
cpt5=0;
cpt6=0;
do
{
entree_ligne_colonne(&x,&y,cpt_essais,nl,nc);
system("cls");
if (matordi[x][y]==2)
{
matjoueur[x][y]=2;
printf("bateau de 2 touche!\n");
}
if (matordi[x][y]==3)
{
matjoueur[x][y]=3;
if((k=bateau_coule_3(matjoueur,mat3,nb_trois))==1)
{
printf ("Bateau coule!!\n\a");
cpt3++;
}
else
printf("bateau de 3 touche!\n");
}
if (matordi[x][y]==4)
{
matjoueur[x][y]=4;
printf("bateau de 4 touche!\n");
}
if (matordi[x][y]==5)
{
matjoueur[x][y]=5;
printf("bateau de 5 touche!\n");
}
if (matordi[x][y]==6)
{
matjoueur[x][y]=6;
if((k=bateau_coule_6(matjoueur,mat6,nb_six))==1)
{
printf ("Bateau coule!!\n\a");
cpt6++;
}
else
printf("bateau de 6 touche!\n");
}
if (matordi[x][y]==0)
{
matjoueur[x][y]=0;
printf("Plouf!\n");
}
printf("\n%d bateau(x) de 2 coule(s) sur %d\n%d bateau(x) de 3 coule(s) sur %d\n%d bateau(x) de 4 coule(s) sur %d\n%d bateau(x) de 5 coule(s) sur %d\n%d bateau(x) de 6 coule(s) sur %d\n\n",cpt2,nb_deux,cpt3,nb_trois,cpt4,nb_quatre,cpt5,nb_cinq,cpt6,nb_six);
affiche_matrice(matjoueur,nl,nc);
cpt_essais++;
if (cpt2+cpt3+cpt4+cpt5+cpt6==nb_deux+nb_trois+nb_quatre+nb_cinq+nb_six)
{
break;
}
}
while (cpt2+cpt3+cpt4+cpt5+cpt6==nb_deux+nb_trois+nb_quatre+nb_cinq+nb_six);
printf("Bravo, vous avez coule tous les bateaux en %d essais\n\a\a\a",cpt_essais-1);
printf("Vous avez detruit tout les navires avant qu'il n'ait eut le temps de prevenir leur base,\nYOU ARE THE WINNER!!!!!!\n\n");
}
while (fini!=1);
}
if (choix==2)
{
printf ("\nLe but du jeu est de couler tous les navires\n\nAu depart, l'ordinateur demande d'entrer la taille de la grille de jeu.\n");
printf ("Ensuite il placera les bateaux aleatoirement dans la grilles\nselon la regle suivante:\n\n");
printf ("- deux bateaux ne peuvent pas se toucher meme par un coin,\n- ils peuvent toucher les bords de la grille\n\n");
printf ("Le jeu sera fini quand tous les bateaux seront coules.\n\n");
system("pause");
}
else
i=0;
}
return 0;
}[/cpp]