-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgestion_bateau.c
84 lines (69 loc) · 2.47 KB
/
gestion_bateau.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// Created by Admin on 27/05/2021.
//
#include "gestion_bateau.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void generation_bateau(boat * boat, Grid *boat_grid ) {
int i=0;
srand(time(NULL)*boat->id);
do {
boat->orientation = rand() % 2;
switch(boat->orientation) {
case 1 :
boat->CooX = rand() % N ;
boat->CooY = rand() % (N-boat->length) ;
break;
case 0 :
boat->CooX = rand() % (N-boat->length) ;
boat->CooY = rand() % N ;
break;
default :
printf("Erreur : orientation ni 0 ni 1");
exit(0);
}
} while(verification_emplacement_bateau(boat, boat_grid) != 1);
do {
i++;
implentation_bateau(boat, boat_grid);
} while (boat_grid->grid[boat->CooX][boat->CooY] == '_');
if (i<1)
printf("%d implementations !\n",i);
}
void implentation_bateau(boat * boat, Grid * boat_grid) {
int i;
if(boat->orientation == 0) { //implentation verticale (axe des x)
for (i=0; i < boat->length ; i++) {
boat_grid->grid[boat->CooX+i][boat->CooY] = boat->id;
}
} else {
for (i=0; i < boat->length; i++) { //implentation horizontale (axe des y)
boat_grid->grid[boat->CooX][boat->CooY+i] = boat->id;
}
}
}
int verification_emplacement_bateau(boat * boat, Grid * boat_grid) {
int X = boat->CooX, Y = boat->CooY;
if(boat->orientation == 0) { //regarde pour orientation verticale
for (; X - boat->CooX < boat->length; X++) { //la vérification doit être fait pour toutes les cases occupées par le bateau
if (boat_grid->grid[X][Y] != '_') { //'_' représente une case vide : si elle n'est pas vide, un bateau est déjà à cette endroit là et on ne peut donc pas générer le nouveau bateau
if ( boat_grid->grid[X][Y] != boat->id ||boat_grid->grid[X][Y] != boat->id_dead ){
return(0);
}
}
}
return(1);
} else {if(boat->orientation == 1) {
for (; Y - boat->CooY < boat->length; Y++ ) {
if (boat_grid->grid[X][Y] != '_') {
if ( boat_grid->grid[X][Y] != boat->id ||boat_grid->grid[X][Y] != boat->id_dead ){
return(0);
}
}
}
return(1);
}
}
return(0);
}