-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.js
More file actions
447 lines (434 loc) · 15.1 KB
/
init.js
File metadata and controls
447 lines (434 loc) · 15.1 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//var grid = initGrille();
var lastimage=false;
var retour ;
var grid = new Array();
grid = [[
{type: 'tour', player: 1},
{type: 'cheval', player: 1},
{type: 'fou', player: 1},
{type: 'reine', player: 1},
{type: 'roi', player: 1},
{type: 'fou', player: 1},
{type: 'cheval', player: 1},
{type: 'tour', player: 1}
],[
{type: 'pion', player: 1},
{type: 'pion', player: 1},
{type: 'pion', player: 1},
{type: 'pion', player: 1},
{type: 'pion', player: 1},
{type: 'pion', player: 1},
{type: 'pion', player: 1},
{type: 'pion', player: 1}
],[false,false,false,false,false,false,false,false],[false,false,false,false,false,false,false,false],[false,false,false,false,false,false,false,false],[false,false,false,false,false,false,false,false],
[
{type: 'pion', player: 2},
{type: 'pion', player: 2},
{type: 'pion', player: 2},
{type: 'pion', player: 2},
{type: 'pion', player: 2},
{type: 'pion', player: 2},
{type: 'pion', player: 2},
{type: 'pion', player: 2}
],[
{type: 'tour', player: 2},
{type: 'cheval', player: 2},
{type: 'fou', player: 2},
{type: 'reine', player: 2},
{type: 'roi', player: 2},
{type: 'fou', player: 2},
{type: 'cheval', player: 2},
{type: 'tour', player: 2}
]]
function select(x, y){
let G = grid;
if(!G[x][y]) return;
let type = G[x][y].type;
let p = G[x][y].player;
let moves = [];
if(type == 'pion'){
if(p == 1){
if (x+1 <8){if(!G[x+1][y]) moves.push('+0+1');}
if(x == 1 && !G[x+2][y] && !G[x+1][y]) moves.push('+0+2');
}
else{
if (x-1 >0){if(!G[x-1][y]) moves.push('+0-1');}
if(x == 6 && !G[x-2][y] && ! G[x-1][y]) moves.push('+0-2');
}
if (x+1 <8 && y+1 <8) {if(!!G[x+1] && !!G[x+1][y+1] && G[x+1][y+1].player != 1 && G[x+1][y+1].player != p) moves.push('+1+1');}
if (x-1>=0 && y+1 <8) {if(!!G[x-1] && !!G[x-1][y+1] && G[x-1][y+1].player != 2 && G[x-1][y+1].player != p) moves.push('+1-1');}
if (x+1 <8 && y-1 >=0) {if(!!G[x+1] && !!G[x+1][y-1] && G[x+1][y-1].player != 1 && G[x+1][y-1].player != p) moves.push('-1+1');}
if (x-1 >=0 && y-1 >=0) {if(!!G[x-1] && !!G[x-1][y-1] && G[x-1][y-1].player != 2 && G[x-1][y-1].player != p) moves.push('-1-1');}
}
if(type == 'tour' || type == "reine"){
for(let i = 1; i <= 8; ++i){
if (y+i >7) break;
if(!G[x][y+i]) moves.push('+' + i + '+0');
if(!!G[x][y+i] && G[x][y+i].player == p){
break;
}
if(!!G[x][y+i] && G[x][y+i].player != p){
moves.push('+' + i + '+0');
break;
}
}
for(let i = 1; i <= 8; ++i){
if (y-i < 0) break;
if(!G[x][y-i]) moves.push('-' + i + '+0');
if(!!G[x][y-i] && G[x][y-i].player == p){
break;
}
if(!!G[x][y-i] && G[x][y-i].player != p){
moves.push('-' + i + '+0');
break;
}
}
for(let i = 1; i <= 8; ++i){
if (x+i>7) break;
if(!!G[x+i] && !G[x+i][y]) moves.push('+0+' + i);
if(!!G[x+i] && !!G[x+i][y] && G[x+i][y].player == p){
break;
}
if(!!G[x+i] && !!G[x+i][y] && G[x+i][y].player != p){
moves.push('+0+' + i);
break;
}
}
for(let i = 1; i <= 8; ++i){
if (x-i <0) break;
if(!!G[x-i] && !G[x-i][y]) moves.push('+0-' + i);
if(!!G[x-i] && !!G[x-i][y] && G[x-i][y].player == p){
break;
}
if(!!G[x-i] && !!G[x-i][y] && G[x-i][y].player != p){
moves.push('+0-' + i);
break;
}
}
}
if(type == 'fou' || type == "reine"){
for(let i = 1; i <= 8; ++i){
if (x+i > 7 || y+i >7) break;
if(!!G[x+i] && !G[x+i][y+i]) moves.push('+' + i + '+' + i);
if(!!G[x+i] && !!G[x+i][y+i] && G[x+i][y+i].player == p){
break;
}
if(!!G[x+i] && !!G[x+i][y+i] && G[x+i][y+i].player != p){
moves.push('+' + i + '+' + i);
break;
}
}
for(let i = 1; i <= 8; ++i){
if (x-i<0 || y+i >7) break;
if(!!G[x-i] && !G[x-i][y+i]) moves.push('+' + i + '-' + i);
if(!!G[x-i] && !!G[x-i][y+i] && G[x-i][y+i].player == p){
break;
}
if(!!G[x-i] && !!G[x-i][y+i] && G[x-i][y+i].player != p){
moves.push('+' + i + '-' + i);
break;
}
}
for(let i = 1; i <= 8; ++i){
if (x+i >7 || y-i < 0 ) break;
if(!!G[x+i] && !G[x+i][y-i]) moves.push('-' + i + '+' + i);
if(!!G[x+i] && !!G[x+i][y-i] && G[x+i][y-i].player == p){
break;
}
if(!!G[x+i] && !!G[x+i][y-i] && G[x+i][y-i].player != p){
moves.push('-' + i + '+' + i);
break;
}
}
for(let i = 1; i <= 8; ++i){
if (x-i <0 || y-i <0) break;
if(!!G[x-i] && !G[x-i][y-i]) moves.push('-' + i + '-' + i);
if(!!G[x-i] && !!G[x-i][y-i] && G[x-i][y-i].player == p){
break;
}
if(!!G[x-i] && !!G[x-i][y-i] && G[x-i][y-i].player != p){
moves.push('-' + i + '-' + i);
break;
}
}
}
if(type == 'cheval'){
if (x+1<8&&y+2<8) {if(!!G[x+1] && (!G[x+1][y+2] || !!G[x+1][y+2] && G[x+1][y+2].player != p)) moves.push('+2+1');}
if (y-2 >=0&&x+1<8) {if(!!G[x+1] && (!G[x+1][y-2] || !!G[x+1][y-2] && G[x+1][y-2].player != p)) moves.push('-2+1');}
if (x-1 >= 0&&y+2<8) {if(!!G[x-1] && (!G[x-1][y+2] || !!G[x-1][y+2] && G[x-1][y+2].player != p)) moves.push('+2-1');}
if (y-2 >= 0 && x-1>=0) {if(!!G[x-1] && (!G[x-1][y-2] || !!G[x-1][y-2] && G[x-1][y-2].player != p)) moves.push('-2-1');}
if (x+2<8 && y+1<8) {if(!!G[x+2] && (!G[x+2][y+1] || !!G[x+2][y+1] && G[x+2][y+1].player != p)) moves.push('+1+2');}
if (x-2 >=0 && y+1<8){if(!!G[x-2] && (!G[x-2][y+1] || !!G[x-2][y+1] && G[x-2][y+1].player != p)) moves.push('+1-2');}
if (y-1>=0 && x+2<8){if(!!G[x+2] && (!G[x+2][y-1] || !!G[x+2][y-1] && G[x+2][y-1].player != p)) moves.push('-1+2');}
if (x-2>=0 && y-1>=0) {if(!!G[x-2] && (!G[x-2][y-1] || !!G[x-2][y-1] && G[x-2][y-1].player != p)) moves.push('-1-2');} //J'ai intervertit tous les couples
}
if(type == "roi"){//rejouter la contrainte de déplacement vis à vis du roi adverse
if(!!G[x+1] && (!G[x+1][y] || !!G[x+1][y] && G[x+1][y].player != p)) moves.push('+0+1');
if(!!G[x-1] && (!G[x-1][y] || !!G[x-1][y] && G[x-1][y].player != p)) moves.push('+0-1');
if(!G[x][y+1] || !!G[x][y+1] && G[x][y+1].player != p) moves.push('+1+0');
if(!G[x][y-1] || !!G[x][y-1] && G[x][y-1].player != p) moves.push('-1+0');
if(!!G[x+1] && (!G[x+1][y+1] || !!G[x+1][y+1] && G[x+1][y+1].player != p)) moves.push('+1+1');
if(!!G[x-1] && (!G[x-1][y+1] || !!G[x-1][y+1] && G[x-1][y+1].player != p)) moves.push('+1-1');
if(!!G[x+1] && (!G[x+1][y-1] || !!G[x+1][y-1] && G[x+1][y-1].player != p)) moves.push('-1+1');
if(!!G[x-1] && (!G[x-1][y-1] || !!G[x-1][y-1] && G[x-1][y-1].player != p)) moves.push('-1-1');
}
return restrict(moves, x, y);
}
function restrict(L, x, y){
let D = [];
L.forEach((d) => {
let dx = parseInt(d[3]);
if(d[2] == '-') dx = -dx;
let dy = parseInt(d[1]);
if(d[0] == '-') dy = -dy;
D.push([x, y, dx, dy]);
});
return D;
}
/*function move3(x,y,dx,dy){
retour = grid[x+dx][y+dy];
grid[x+dx].splice(y+dy,1,grid[x][y]);
grid[x][y]=false;
console.log(grid[x+dx][y+dy]);
console.log(retour);
console.log(grid);
console.log(x+dx,y+dy);
console.log(grid[x+dx][y+dy]);
}*/
function move(x, y, dx, dy,id){
if (playerID == noir) {
tour+=1;
document.getElementById('zonetext').textContent+=" "+tour+".";
}
if (grid[x][y].player == playerID) {
/*console.log(x);
console.log(dx);
console.log(x+dx);*/
var a = y+dy;
var b = x+dx;
if(id!=playerID) {
deplacement(x, y, dx*40, dy*40);
grid[b].splice(a,1,grid[x][y]);
//grid[x+dx][y+dy]={type:' ',player:0};
//grid[b][a] = grid[x][y];
/*for (var i = 0; i <8; i++) {
}*/
grid[x][y] = false;}
else{
console.log(montexte);
sendCmd(JSON.stringify({type: 'move', x: x, y: y, dx: dx, dy: dy, room: roomID, player:playerID, texte:montexte}));
deplacement(x, y, dx*40, dy*40);
grid[b].splice(a,1,grid[x][y]);
//grid[x+dx][y+dy]={type:' ',player:0};
//grid[b][a] = grid[x][y];
/*for (var i = 0; i <8; i++) {
}*/
grid[x][y] = false;
}
montour=false;
}
}
function move2(x,y,dx,dy,id){
/*console.log(x);
console.log(dx);
console.log(x+dx);*/
var a = y+dy;
var b = x+dx;
if(id!=playerID) {
deplacement(x, y, dx*40, dy*40);
grid[b].splice(a,1,grid[x][y]);
//grid[x+dx][y+dy]={type:' ',player:0};
//grid[b][a] = grid[x][y];
/*for (var i = 0; i <8; i++) {
}*/
grid[x][y] = false;}
else{
console.log(montexte);
sendCmd(JSON.stringify({type: 'move', x: x, y: y, dx: dx, dy: dy, room: roomID, player:playerID, texte:montexte}));
deplacement(x, y, dx*40, dy*40);
grid[b].splice(a,1,grid[x][y]);
//grid[x+dx][y+dy]={type:' ',player:0};
//grid[b][a] = grid[x][y];
/*for (var i = 0; i <8; i++) {
}*/
grid[x][y] = false;
}
if (playerID==blanc){
tour+=1;
document.getElementById('zonetext').textContent+=" "+tour+".";
}
}
function undowmove(x,y,dx,dy,pion,elm,id){
sendCmd(JSON.stringify({type: 'undomove', x: x, y: y, dx: dx, dy: dy,pion:pion, room: roomID, player:playerID, lastimage:lastimage,texte:montexte,elm:elm}));
montour=true;
undowdeplacement(x,y,dx*40,dy*40,pion);
grid[x][y] = grid[x+dx][y+dy];
if (lastimage){
grid[x+dx][y+dy]={type:lastimage.type,player:lastimage.player};
lastimage=false;
}
else{
grid[x+dx][y+dy]=false;
}/*
if (grid[x][y].player == playerID) {
if(id!=playerID){
montour=true;
undowdeplacement(x,y,dx*40,dy*40,pion);
grid[x][y] = grid[x+dx][y+dy];
if (lastimage){
grid[x+dx][y+dy]={type:lastimage.type,player:lastimage.player};
lastimage=false;
}
else{
grid[x+dx][y+dy]=false;
}
}
else {
sendCmd(JSON.stringify({type: 'undomove', x: x, y: y, dx: dx, dy: dy,pion:pion, room: roomID, player:playerID, texte:montexte}));
montour=true;
undowdeplacement(x,y,dx*40,dy*40,pion);
grid[x][y] = grid[x+dx][y+dy];
if (lastimage){
grid[x+dx][y+dy]={type:lastimage.type,player:lastimage.player};
lastimage=false;
}
else{
grid[x+dx][y+dy]=false;
}
}
}*/
}
function undowmove2(x,y,dx,dy,pion,id,lastimage2){
if (id!=playerID) {
console.log("je suis la");
console.log(lastimage2);
montour=true;
undowdeplacement2(x,y,dx*40,dy*40,pion,lastimage2);
grid[x][y] = grid[x+dx][y+dy];
if (lastimage2){
grid[x+dx][y+dy]={type:lastimage2.type,player:lastimage2.player};
lastimage=false;
}
else{
grid[x+dx][y+dy]=false;
}
}
else {
sendCmd(JSON.stringify({type: 'undomove', x: x, y: y, dx: dx, dy: dy,pion:pion, room: roomID, player:playerID, texte:montexte}));
montour=true;
undowdeplacement(x,y,dx*40,dy*40,pion);
grid[x][y] = grid[x+dx][y+dy];
if (lastimage2){
grid[x+dx][y+dy]={type:lastimage2.type,player:lastimage2.player};
lastimage=false;
}
else{
grid[x+dx][y+dy]=false;
}
}
}
var ctx2 = document.getElementById('pieces').getContext('2d');
var ctx3 = document.getElementById('pieces_prise').getContext('2d');
ctx3.font ="20px Georgia";
ctx3.strokeText("Pièces Prises :",0,20);
ctx3.x = 0;
ctx3.y = 40;
function draw(x,y,url){
var image = new Image();
image.addEventListener('load',function(){
var less= image.height;
ctx2.drawImage(image,x-32,y-less-4);
},false);
image.src = url;
image.load;
}
function undraw(x,y){
ctx2.clearRect(x,y,40,40);
}
function deplacement(x,y,deltax,deltay){ //deltax et deltay compris entre -8 et 8 pour le déplacement
undraw(y*40,x*40);
if (grid[x+deltax/40][y+deltay/40]!=false&&grid[x+deltax/40][y+deltay/40].player != playerID){
var image = new Image();
image.src = 'Images/pions/'+grid[x+deltax/40][y+deltay/40].type+'_'+grid[x+deltax/40][y+deltay/40].player+'.png';
lastimage = {url:'Images/pions/'+grid[x+deltax/40][y+deltay/40].type+'_'+grid[x+deltax/40][y+deltay/40].player+'.png',type : grid[x+deltax/40][y+deltay/40].type,player : grid[x+deltax/40][y+deltay/40].player};
ctx3.drawImage(image,ctx3.x,ctx3.y); //afficher en dessous de l'échiquier la pièce mangée
ctx3.x +=40;
}
undraw(y*40+deltay,x*40+deltax);
draw(y*40+deltay+40,x*40+deltax+40,'Images/pions/'+grid[x][y].type+'_'+grid[x][y].player+'.png');
}
function undowdeplacement(x,y,deltax,deltay,piece){
if (lastimage&& piece.player!=lastimage.player) { //si une pièce avait été prise et qu'elle n'est pas au même joueur
undraw(y*40+deltay,x*40+deltax);
draw(y*40+deltay+40,x*40+deltax+40,lastimage.url);
draw(y*40+40,x*40+40,'Images/pions/'+grid[x+deltax/40][y+deltay/40].type+'_'+grid[x+deltax/40][y+deltay/40].player+'.png');
ctx3.x -=40;
ctx3.clearRect(ctx3.x,ctx3.y,40,40);
}
else{ //sinon
undraw(y*40+deltay,x*40+deltax);
//draw(y*40+deltay,x*40+deltax,lastimage);
draw(y*40+40,x*40+40,'Images/pions/'+grid[x+deltax/40][y+deltay/40].type+'_'+grid[x+deltax/40][y+deltay/40].player+'.png');
}
}
function undowdeplacement2(x,y,deltax,deltay,piece,lastimage2){
if (lastimage2&& piece.player!=lastimage2.player) { //si une pièce avait été prise et qu'elle n'est pas au même joueur
undraw(y*40+deltay,x*40+deltax);
draw(y*40+deltay+40,x*40+deltax+40,lastimage2.url);
draw(y*40+40,x*40+40,'Images/pions/'+grid[x+deltax/40][y+deltay/40].type+'_'+grid[x+deltax/40][y+deltay/40].player+'.png');
ctx3.x -=40;
ctx3.clearRect(ctx3.x,ctx3.y,40,40);
}
else{ //sinon
undraw(y*40+deltay,x*40+deltax);
//draw(y*40+deltay,x*40+deltax,lastimage);
draw(y*40+40,x*40+40,'Images/pions/'+grid[x+deltax/40][y+deltay/40].type+'_'+grid[x+deltax/40][y+deltay/40].player+'.png');
}
}
//initialisation du plateau avec toute les pièces
for(i=1;i<9;i++){
for(j=1;j<9;j++){
if (grid[i-1][j-1]){
draw(j*40,i*40,'Images/pions/'+grid[i-1][j-1].type+'_'+grid[i-1][j-1].player+'.png');
}
}
}
//initialisation des imgs pour promotion ici
setTimeout(()=>{console.log(playerID,"playerID");var img1 = document.createElement("img");
img1.src = "Images/pions/cheval_"+playerID+".png";
img1.onclick = function() {achanger_cheval(playerID);};
var img2 = document.createElement("img");
img2.src = "Images/pions/tour_"+playerID+".png";
img2.onclick = function () {achanger_tour(playerID);};
var img3 = document.createElement("img");
img3.src = "Images/pions/reine_"+playerID+".png";
img3.onclick = function(){achanger_reine(playerID);};
var img4 = document.createElement("img");
img4.src = "Images/pions/fou_"+playerID+".png";
img4.onclick = function() {achanger_fou(playerID);};
document.getElementById('childrens').appendChild(img1);
document.getElementById('childrens').appendChild(img2);
document.getElementById('childrens').appendChild(img3);
document.getElementById('childrens').appendChild(img4);}, 2000);
/*var img1 = document.createElement("img");
img1.src = "Images/pions/cheval_"+playerID+".png";
img1.onclick = function() {achanger_cheval(playerID);};
var img2 = document.createElement("img");
img2.src = "Images/pions/tour_"+playerID+".png";
img2.onclick = function () {achanger_tour(playerID);};
var img3 = document.createElement("img");
img3.src = "Images/pions/reine_"+playerID+".png";
img3.onclick = function(){achanger_reine(playerID);};
var img4 = document.createElement("img");
img4.src = "Images/pions/fou_"+playerID+".png";
img4.onclick = function() {achanger_fou(playerID);};
document.getElementById('childrens').appendChild(img1);
document.getElementById('childrens').appendChild(img2);
document.getElementById('childrens').appendChild(img3);
document.getElementById('childrens').appendChild(img4);*/
//Reste à faire :
//Le roc
//Faire un timer pour les parties blitz
//Interaction interface lors d'un échec et mat (facile) (faire dabord la variable tour, fonction ez apres)
//IA si le temps le permet
//Implémenter la prise de pion 'en passant'