-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOccupation.js
More file actions
69 lines (61 loc) · 1.75 KB
/
Occupation.js
File metadata and controls
69 lines (61 loc) · 1.75 KB
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
var Occupation=function(arene,carrelage){
this.arene=arene;
this.carrelage=carrelage;
this.tab=new Array(arene.nbColonne());
for(var i=0;i<arene.nbColonne();i++){
this.tab[i]=new Array(arene.nbLigne());
}
for(i=0;i<arene.nbColonne();i++){
for (var j = 0; j < arene.nbLigne(); j++) {
this.tab[i][j]=0;
}
}
this.occuper=function(cell){
this.tab[cell.x][cell.y]=1;
}
this.liberer=function(cell){
this.tab[cell.x][cell.y]=0;
}
this.touLiberer=function(){
for(var i=0;i<arene.nbColonne();i++){
for (var j = 0; j < arene.nbLigne(); j++) {
this.tab[i][j]=0;
}
}
}
this.estLibre=function(cell){
return this.tab[cell.x][cell.y]==0;
}
this.listeVoisinLibres=function(cell){
var liste=[];
var haut=cell.y-1;
var bas=cell.y+1;
var droite=cell.x+1;
var gauche=cell.x-1;
if(haut >= 0 && this.tab[cell.x][haut]==0)
liste.push(new Point(cell.x,haut));
if(bas < arene.nbLigne() && this.tab[cell.x][bas]==0)
liste.push(new Point(cell.x,bas));
if(droite < arene.nbColonne() && this.tab[droite][cell.y]==0)
liste.push(new Point(droite,cell.y));
if(gauche >= 0 && this.tab[gauche][cell.y]==0)
liste.push(new Point(gauche,cell.y));
return liste;
}
this.voisinLibreAuHasard=function(cell){
var liste=this.listeVoisinLibres(cell);
if(liste.length==0)
return undefined;
return liste[parseInt(Math.random()*liste.length)];
}
this.visuDebug=function(carrelage){
for(var i=0;i<arene.nbColonne();i++){
for (var j = 0; j < arene.nbLigne(); j++) {
if(this.tab[i][j]==0)
carrelage.colorier(i,j,carrelage.color);
if(this.tab[i][j]==1)
carrelage.colorier(i,j,"#ff0000");
}
}
}
}