-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoteur.js
More file actions
126 lines (120 loc) · 3.84 KB
/
Moteur.js
File metadata and controls
126 lines (120 loc) · 3.84 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var Moteur=function(id,taille_cell,color){
this.id=id;
this.taille_cell=taille_cell;
this.color=color;
this.arene=new Arene(id,taille_cell,color);
this.carrelage=new Carrelage(this.arene.nbLigne(),this.arene.nbColonne(),parseInt(this.arene.tailleCellule()*0.9),this.arene,"#72A431");
this.occupation=new Occupation(this.arene,this.carrelage);
this.tabSer=[];
this.tabSer.push(new Serpent(this.arene.nbLigne(),"#FF612C",this.arene,"haut"));
this.tabSer.push(new Serpent(this.arene.nbLigne(),"#007DD6",this.arene,"bas"));
for (var i = 0; i < this.arene.nbLigne(); i++) {
this.occupation.occuper(new Point(i,0));
this.occupation.occuper(new Point(i,this.arene.nbLigne()-1));
}
this.occupation.visuDebug(this.carrelage);
this.nbLigne=function(){
return this.arene.nbLigne();
}
this.nbColonne=function(){
return this.arene.nbColonne();
}
this.ajouterSerpent=function(couleur="#a59b6f",depart){
switch(depart){
case "haut":
this.tabSer.push(new Serpent(this.arene.nbLigne(),couleur,this.arene,"haut"));
break;
case "bas":
this.tabSer.push(new Serpent(this.arene.nbLigne(),couleur,this.arene,"bas"))
break;
default:
console.log("Erreur: Position non reconnu pour les serpent");
return;
}
}
this.positionProtagoniste=function(){
var tabdePos=[];
for(var i=0;i<this.tabSer.length;i++){
tabdePos.push(this.tabSer[i].toList());
}
return tabdePos;
}
this.proposerMouvement=function(indice,mouvement){
var serpentCible=this.tabSer[indice];
var tete = serpentCible.tete();
var nouv_tete;
switch(mouvement)
{
case "gch" :
if(this.occupation.listeVoisinLibres(tete).length==0){
alert("Joueur "+indice+" as perdu");
envie=0;
return;
}
if(tete.x<=0){
nouv_tete=this.occupation.voisinLibreAuHasard(tete);
break;
}
if(this.occupation.estLibre(new Point(tete.x-1,tete.y)))
nouv_tete = tete.voisin("gch");
else {
nouv_tete=this.occupation.voisinLibreAuHasard(tete);
}
break;
case "haut" :
if(this.occupation.listeVoisinLibres(tete).length==0){
alert("Joueur "+indice+" as perdu");
envie=0;
return;
}
if(tete.y<=0){
nouv_tete=this.occupation.voisinLibreAuHasard(tete);
break;
}
if(this.occupation.estLibre(new Point(tete.x,tete.y-1)))
nouv_tete = tete.voisin("haut");
else {
nouv_tete=this.occupation.voisinLibreAuHasard(tete);
}
break;
case "drt" :
if(this.occupation.listeVoisinLibres(tete).length==0){
alert("Joueur "+indice+" as perdu");
envie=0;
return;
}
if(tete.x>=this.arene.nbColonne()-1){
nouv_tete=this.occupation.voisinLibreAuHasard(tete);
break;
}
if(this.occupation.estLibre(new Point(tete.x+1,tete.y)))
nouv_tete = tete.voisin("drt");
else {
nouv_tete=this.occupation.voisinLibreAuHasard(tete);
}
break;
case "bas" :
if(this.occupation.listeVoisinLibres(tete).length==0){
alert("Joueur "+indice+" as perdu");
envie=0;
return;
}
if(tete.y>=this.arene.nbLigne()-1){
nouv_tete=this.occupation.voisinLibreAuHasard(tete);
break;
}
if(this.occupation.estLibre(new Point(tete.x,tete.y+1)))
nouv_tete = tete.voisin("bas");
else {
nouv_tete=this.occupation.voisinLibreAuHasard(tete);
}
break;
default:
return;
}
this.occupation.liberer(serpentCible.queue());
serpentCible.placerTete(nouv_tete);
this.occupation.occuper(nouv_tete);
this.occupation.visuDebug(this.carrelage);
}
}