-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
232 lines (215 loc) · 6.74 KB
/
script.js
File metadata and controls
232 lines (215 loc) · 6.74 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
chess = new Chess(); //Initiating Chess Class
boardColor = ['#769440','#d4d6a5'] //Board Color
flag = true;
WhiteMove = true; //Board Color
prev = [] //Has the Previously Moved Id
color = [] //Has the Previously Moved Id's Colour
history = {}
var selectedId = 0; //Has the Previously Moved Id
coins = [["r","n","b","q","p","k"],["R","N","B","Q","P","K"]]
coinImg ={
'P':"./Pieces/wSoldier.png",
'Q':"./Pieces/wQueen.png",
'R':"./Pieces/wRook.png",
'N':"./Pieces/wHorse.png",
'K':"./Pieces/wKing.png",
'B':"./Pieces/wBishop.png",
'.':"",
'p':"./Pieces/Soldier.png",
'q':"./Pieces/Queen.png",
'r':"./Pieces/Rook.png",
'n':"./Pieces/Horse.png",
'k':"./Pieces/King.png",
'b':"./Pieces/Bishop.png",
} //To shift Images from one place to another
// To fill Board Colour
for(i=0;i<8;i++){
for(j=0;j<8;j++){ document.getElementById(String.fromCharCode(j+97)+i).style.backgroundColor=boardColor[(flag)?0:1];
flag=!flag;
}
flag=!flag;
}
/*
Soldier Promotion:-
------------------
Description :- Triggered on clicking an option in Pawn Promotion Modal
Here Pawn changing logic is handled here
*/
function SoldierPromotion(to){
det = chess.history[chess.history.length-1];
_ = chess.getCoordinates(det['to']);
chess.board[_[0]][_[1]] = to;
document.getElementById('i'+det['to']).src = coinImg[to];
checkCase = !isUpper(to);
if(chess.isAttacked((checkCase)?'K':'k')){
if(chess.isCheckMate((checkCase)?'K':'k')){
if(checkCase) alert('White Wins');
else alert('Black Wins');
location.reload();
return;
}
(checkCase)?alert('♔ is attacked'):alert('♔ is attacked')
}
(checkCase)?$('#SoldierPromotion').modal('hide'):$('#wSoldierPromotion').modal('hide');
}
/*
Get Possible Moves:-
------------------
Description:- Get Possible icons that can move on an instance [Turn]
*/
function getAllowedPositions(index){
AllowedPosition = []
for(i in coins[index]){
AllowedPosition.push.apply(AllowedPosition,chess.getPosition(coins[index][i]));
}
return AllowedPosition;
}
/*
Undo:-
----
Description:- Get Previously Moved coin details from chess->History
and restore previous positions
Note :- Here Moves Board logic is also Handled
*/
function undo(){
ds = chess.undo();
from = document.getElementById('i'+ds['from']);
to = document.getElementById('i'+ds['to']);
from.src = coinImg[ds['fromVal']];
to.src = coinImg[ds['toVal']];
WhiteMove = !WhiteMove;
if(chess.history.length>1){
ds = chess.history[chess.history.length-1];
updateMovesBoard(ds['fromVal'],ds['to']);
}
else{
document.getElementById('prevMov').innerHTML = '-';
}
}
/*
Get Valid Position:-
------------------
Description:- Check for a valid move that can be taken for the selected piece
where the piece is moved to the desired location and checked attacks
for king.
*/
function getValidPos(ls){
res = []
for(i in ls){
chess.move(selectedId,ls[i]);
if(!chess.isAttacked((WhiteMove)?'K':'k'))
res.push(ls[i])
chess.undo();
}
return res;
}
/*
Update Moves Board:-
------------------
Description:- To update the value present in Moves Board
*/
function updateMovesBoard(val, selectedId){
if(val!='p' && val!='P')
document.getElementById('prevMov').innerHTML = val.toUpperCase()+selectedId
else
document.getElementById('prevMov').innerHTML = selectedId
}
/*
Moves:-
-----
Description:- To Move Piece from one place to another
where on move check, checkMate,Castling
logic are handled here
*/
function getPos(id){
colorArr = [];
if(document.getElementById(id) && document.getElementById(id).style.backgroundColor=="rgb(224, 90, 72)"){
_ = chess.getCoordinates(selectedId);
updateMovesBoard(chess.board[_[0]][_[1]],selectedId);
isCastling = chess.move(selectedId,id);
console.log(isCastling)
if(isCastling){
let temp = document.getElementById('i'+isCastling[0]).src
document.getElementById('i'+isCastling[1]).src = temp;
document.getElementById('i'+isCastling[0]).src ="";
chess.move(isCastling[0],isCastling[1]);
}
if(chess.isAttacked((WhiteMove)?'K':'k')){
undo();
WhiteMove = !WhiteMove;
alert("Invalid Move King is Under Attack");
return;
}
temp = document.getElementById('i'+selectedId).src
document.getElementById('i'+id).src = temp;
document.getElementById('i'+selectedId).src ="";
for(i in prev){
dots = document.getElementById(prev[i])
dots.style.backgroundColor=color[i];
}
if(chess.isSoldierPromotion(id)){
_ = chess.getCoordinates(id);
WhiteMove = !WhiteMove;
if(isUpper(chess.board[_[0]][_[1]]))
$('#wSoldierPromotion').modal('toggle');
else
$('#SoldierPromotion').modal('toggle');
return ;
}
if(chess.isAttacked((WhiteMove)?'k':'K')){
if(chess.isCheckMate((WhiteMove)?'k':'K')){
if(WhiteMove) alert('White Wins');
else alert('Black Wins');
location.reload();
return;
}
(WhiteMove)?alert('♔ is attacked'):alert('♔ is attacked')
}
WhiteMove = !WhiteMove;
return;
}
if(getAllowedPositions((WhiteMove)?1:0).indexOf(id)!=-1){
selectedId = id;
ls = getValidPos(chess.moves(id));
for(i in prev){
if(prev[i]){
dots = document.getElementById(prev[i])
dots.style.backgroundColor=color[i];
}
}
for(i in ls){
dots = document.getElementById(ls[i])
colorArr.push(dots.style.backgroundColor);
dots.style.backgroundColor="#e05a48";
document.getElementById(ls[i]).setAttribute("ondrop","onDrop(this.id)")
document.getElementById(ls[i]).setAttribute("ondragover","onDragOver(event)")
}
color = colorArr;
prev = ls;
}
else{
console.log("Invalid Move");
}
}
// Undo Function :- triggered on key press CTRL + Z
document.addEventListener('keydown', function(event) {
if (event.ctrlKey && event.key === 'z') {
undo();
}
});
// To Check the whether given char is Upper or Lower Case
function isUpper(val) {
return val.charCodeAt(0) >= 65 && val.charCodeAt(0) <= 91;
}
//On Drop the Piece will be placed on the dropped spot
function onDrop(id){
getPos(id);
}
//On Drag getPos function will be called to get Piece Possible Moves
function onDragStart(id) {
selectedId = id.slice(1);
getPos(id.slice(1));
}
function onDragOver(event) {
event.preventDefault();
}