DEV/DEV1.1/CM1/exo5.c

57 lines
1.2 KiB
C
Raw Normal View History

2024-10-22 15:38:09 +02:00
# include <stdio.h>
# include <stdlib.h>
# define TAILLE_GRILLE 10
int navires_encore_presents(int grille[][TAILLE_GRILLE], int len_grille) {
/* Renvoie 1 si des navires sont encore présents, faux sinon */
int i;
int j;
for (i = 0; i != len_grille; i++) {
for (j = 0; j != len_grille; j++) {
if (grille[i][j] == 1) {
return 1;
}
}
}
return 0;
}
int main(void) {
/* Si un bateau est présent, la case vaut 1, sinon, elle vaut 0 */
int grille[TAILLE_GRILLE][TAILLE_GRILLE] = {
{0,0,0,0,0,0,0,0,1,1},
{0,1,0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,1,1,1,0,0,0},
{0,1,1,1,1,0,0,0,0,1},
{0,0,0,0,0,0,0,0,0,1},
{0,0,0,0,0,0,0,0,0,1},
};
int tir_x, tir_y;
int nb_coups = 0;
while (navires_encore_presents(grille, TAILLE_GRILLE) == 1) {
printf("Coordonnées ? ");
scanf("%d", &tir_x);
getchar();
scanf("%d", &tir_y);
nb_coups++;
if (grille[tir_x][tir_y] == 1) {
printf("Touché\n");
grille[tir_x][tir_y] = 0;
}
else {
printf("Dans l'eau\n");
}
}
printf("...\nPartie terminée en %d coups\n", nb_coups);
return EXIT_SUCCESS;
}