-
Notifications
You must be signed in to change notification settings - Fork 1
/
check.js
152 lines (134 loc) · 3.26 KB
/
check.js
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
/*
Here are the following functions used for cheking if there is a check of a checkmate
initCheck() will create a new map in a check situation (developpers only)
initCheck() will create a new map in a check mate situation (developpers only)
getKing(color) will return the king informations of the specified color
isInCheck() will an array which contain all the pieces that checked the king, or false if there is no
canWeEatThatSucker(x, y) will return an array which contains coordonates of pieces which can eat the piece (located in (x,y)) threating the king
kingEscape() will return true if the king can move without being check, false if not
*/
function initCheck()
{
pieces = [];
pieces.push(new Tour(0, 5, noir));
pieces.push(new Fou(1, 1, noir));
pieces.push(new Cavalier(5, 2, blanc));
pieces.push(new Roi(5, 5, blanc));
pieces.push(new Pion(2, 5, blanc));
display(blanc);
}
function initCheck1()
{
pieces = [];
pieces.push(new Tour(0, 5, noir));
pieces.push(new Fou(6, 4, noir));
pieces.push(new Cavalier(5, 2, blanc));
pieces.push(new Roi(5, 5, blanc));
display(blanc);
}
function initCheckMate()
{
pieces = [];
pieces.push(new Tour(1, 0, noir));
pieces.push(new Dame(2, 1, noir));
pieces.push(new Roi(7, 0, blanc));
display(blanc);
}
function getKing(color)
{
for (var i = 0; i < pieces.length; i++)
{
if (pieces[i].piece == 'roi' && pieces[i].joueur == color)
return (pieces[i]);
}
return (false);
}
function isInCheck()
{
var king = getKing(tour);
var tab = new Array();
for (var j = 0; j < pieces.length; ++j)
{
if (pieces[j].joueur != tour && pieces[j].canMove(king.pos_x, king.pos_y))
{
tab.push(new Array(pieces[j].pos_x, pieces[j].pos_y));
}
}
if (tab.length < 1)
return null;
else
return tab;
}
function canBlockCheck(dangerousPositions, color)
{
var oldPieces = pieces.slice(0);
var tab = new Array();
for (var j = 0; j < dangerousPositions.length; ++j)
{
tmpPos = dangerousPositions[j];
pieces = oldPieces.slice(0);
for (k = 0; k < pieces.length; ++k)
{
if (pieces[k].joueur != color || pieces[k].piece == 'roi')
continue ;
if (pieces[k].canMove(tmpPos[0], tmpPos[1]))
{
old_x = pieces[k].pos_x;
old_y = pieces[k].pos_y;
pieces[k].pos_x = tmpPos[0];
pieces[k].pos_y = tmpPos[1];
if (isInCheck() == false)
{
tab.push([tmpPos[0], tmpPos[1], pieces[k]]);
}
pieces[k].pos_x = old_x;
pieces[k].pos_y = old_y;
}
}
}
pieces = oldPieces.slice(0);
return tab;
}
function canWeEatThatSucker(x, y)
{
var sucker = getPiece(x, y);
var whoCan = new Array();
for (i in pieces)
{
if (pieces[i].joueur == tour && pieces[i].alive == true)
{
tmp_x = pieces[i].pos_x;
tmp_y = pieces[i].pos_y;
possibs = movePossibs(tmp_x, tmp_y);
for (key in possibs)
{
if (possibs[key][0] == x && possibs[key][1] == y)
whoCan.push(Array(tmp_x, tmp_y));
}
}
}
if (whoCan.length == 0)
return false;
return whoCan;
}
function kingEscape()
{
king = getKing(tour);
x = king.pox_x;
y = king.pos_y;
possibs = movePossibs(king.pos_x, king.pos_y);
for (i in possibs)
{
king.pos_x = possibs[i][0];
king.pos_y = possibs[i][1];
if (isInCheck() == false)
{
king.pos_x = x;
king.pos_y = y;
return true;
}
}
king.pos_x = x;
king.pos_y = y;
return false;
}