-
Notifications
You must be signed in to change notification settings - Fork 33
/
piece_object.js
365 lines (337 loc) · 13.9 KB
/
piece_object.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
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
function Piece(type, index) {
this.index = index;
this.type = type;
this.icon = document.createElement('div');
this.icon.appendChild(document.querySelector('#' + type).cloneNode());
let turnOrder = document.createElement('div');
turnOrder.style.position = 'absolute';
turnOrder.style.top = turnOrder.style.left = '2px';
this.icon.appendChild(turnOrder);
this.icon.style.position = 'absolute';
this.icon.style.transform = 'translate(-50%,-50%)';
this.deploymentCounter = type == 'queen' ? 5 : 3; //PC changed this line 5:3 is original, 3 is time between spawns
this.turnCount = 1
if (this.type == "pawn") {
const dirEl = document.createElement('div');
dirEl.style.position = 'absolute';
dirEl.style.top = dirEl.style.right = '2px';
if (Math.floor(this.index / 7) == 5) {
this.upDirection = true;
dirEl.textContent = '🡡';
} else if (Math.floor(this.index / 7) == 1) {
this.upDirection = false;
dirEl.textContent = '🡣';
}
this.icon.appendChild(dirEl);
}
this.resize = () => {
const fi = this.icon.querySelector('img');
const squareSize = getBoardSquareSize();
fi.style.maxWidth = fi.style.maxHeight = squareSize;
fi.style.height = fi.style.width = squareSize;
}
this.checkAlive = () => {
if (playerIndex != this.index) occupiedSquares.push(this.index);
if(!playerIndex != this.index)
{ // REMOVE BLUE SQUARE IN CASE OF DEATH
if(this.index<7)
{
let sqr= document.querySelector( `.board>div:nth-child(${42-(7*this.index)+1})` ) ;
sqr.style.background = "";
}
else if(this.index>41)
{
let sqr= document.querySelector(`.board>div:nth-child(${342-(7*this.index)+1})`);
sqr.style.background = "";
}
else if (this.index==7 || this.index==14 || this.index==21|| this.index==28|| this.index==35)
{
let sqr= document.querySelector( `.board>div:nth-child(${Math.round((0.142857*this.index)+42)+1})`);
sqr.style.background ="";
}
else if (this.index==13 || this.index==20 || this.index==27|| this.index==34|| this.index==41|| this.index==48)
{
let sqr= document.querySelector(`.board>div:nth-child(${Math.round((0.142857*this.index)-0.857143)+1})`);
sqr.style.background = "";
}
}
return playerIndex != this.index;
};
this.move = () => {
if(gameMode == "loop"){
if(this.index<7)
{
this.index=42-(7*this.index);
}
else if(this.index>41)
{
this.index=342-(7*this.index);
}
else if (this.index==7 || this.index==14 || this.index==21|| this.index==28|| this.index==35)
{
this.index=Math.round((0.142857*this.index)+42);
}
else if (this.index==13 || this.index==20 || this.index==27|| this.index==34|| this.index==41|| this.index==48)
{
this.index=Math.round((0.142857*this.index)-0.857143);
}
this.removeblue();
}
if (this.deploymentCounter == 0) {
// check if player is on me
// if so, return false
// else
// generate possible moves
let possibleMoves = protopieces[this.type].generateMoves(this.index);
possibleMoves = possibleMoves.filter((i) => !isOccupied(i));
// Pawn edge case since these only move in one direction
if (this.type == "pawn") {
possibleMoves = possibleMoves.filter((i) => this.upDirection ? i < this.index : i > this.index)
}
if (!possibleMoves.length) return false; // die
// check if I can hit player and not drunk
if (possibleMoves.includes(playerIndex) && gameMode != 'drunk') {
this.index = playerIndex;
// if i can hit player, hit player
} else {
if(this.type == "pawn" && this.moved) { // Pawns: Only move every other turn
this.moved = false;
return false
}
this.index =
possibleMoves[Math.floor(Math.random() * possibleMoves.length)];
}
this.moved = true;
//Checking for Pawn-Promotion:
if (this.type == "pawn") {
let currentRow = Math.floor(this.index / 7);
let promoting = currentRow == 0 || currentRow == 6;
if (promoting) {
this.type = "queen";
this.icon.replaceChild(document.querySelector('#' + this.type).cloneNode(), this.icon.childNodes[0]);
this.icon.removeChild(this.icon.childNodes[2])
}
}
return true;
} else if (this.deploymentCounter > 1) {
this.deploymentCounter--;
return true;
} else {
this.icon.children[0].style.height = '5vh';
this.deploymentCounter--;
return true;
}
};
this.removeblue= () =>{
let sqr= document.querySelector( `.board>div:nth-child(${this.index+1}`) ;
if (sqr.style.background=="blue")
{
sqr.style.background='';
}
}
// Part of is square safe
this.canHitSquare = (SquareIndex) => {
if (this.deploymentCounter <= 1) {
// check if player is on me
// if so, return false because square is safe as can continue moving
// else
// generate possible moves
let possibleMoves = protopieces[this.type].generateMoves(this.index);
possibleMoves = possibleMoves.filter((i) => !isOccupied(i));
// Pawn edge case since these only move in one direction
if (this.type == "pawn") {
possibleMoves = possibleMoves.filter((i) => this.upDirection ? i < this.index : i > this.index)
}
if (!possibleMoves.length) return false; // die
// check if I can hit the specified square
if (possibleMoves.includes(SquareIndex) && gameMode != 'drunk') {
return true;
} else {
return false;
}
} else {
return false;
}
};
this.cleanup = () => {
//this.icon.remove();
if (this.type != 'pawn' || !this.moved)
this.moved = this.deploymentCounter != 0;
};
this.draw = () => {
turnOrder.innerText = this.deploymentCounter;
if (this.deploymentCounter == 0 && gameMode != 'strategist') {
this.icon.classList.add("fullyDeployed")
turnOrder.style.display = 'none';
} else if (this.deploymentCounter == 0) {
turnOrder.innerText = pieces.indexOf(this) + 1;
}
this.resize();
drawToIndex(this.icon, this.index);
if(gameMode == "loop")
{
if(this.index<7)
{
let sqr= document.querySelector( `.board>div:nth-child(${42-(7*this.index)+1})` ) ;
sqr.style.background = "blue";
}
else if(this.index>41)
{
let sqr= document.querySelector(`.board>div:nth-child(${342-(7*this.index)+1})`);
sqr.style.background = "blue";
}
else if (this.index==7 || this.index==14 || this.index==21|| this.index==28|| this.index==35)
{
let sqr= document.querySelector( `.board>div:nth-child(${Math.round((0.142857*this.index)+42)+1})`);
sqr.style.background ="blue";
}
else if (this.index==13 || this.index==20 || this.index==27|| this.index==34|| this.index==41|| this.index==48)
{
let sqr= document.querySelector(`.board>div:nth-child(${Math.round((0.142857*this.index)-0.857143)+1})`);
sqr.style.background = "blue";
}
}
};
}
let isOccupied = (index) => {
for (let i of pieces) {
if (i.index == index) return true;
}
return gameMode != 'strategist' && occupiedSquares.indexOf(index) != -1;
};
let protopieces = {
knight: {
value: 3,
spawnTries: 5,
generateMoves: (index) => {
let possibleMoves = [];
/*Up two over one left*/
if (index % 7 > 0 && Math.floor(index / 7) > 1) possibleMoves.push(-15);
/*Up two over one right */
if (index % 7 < 6 && Math.floor(index / 7) > 1) possibleMoves.push(-13);
/*Left two up one*/
if (index % 7 > 1 && Math.floor(index / 7) > 0) possibleMoves.push(-9);
/*Right two up one*/
if (index % 7 < 5 && Math.floor(index / 7) > 0) possibleMoves.push(-5);
/*Right two down one*/
if (index % 7 > 1 && Math.floor(index / 7) < 6) possibleMoves.push(5);
/*Left two down one*/
if (index % 7 < 5 && Math.floor(index / 7) < 6) possibleMoves.push(9);
/*Down two over one right */
if (index % 7 > 0 && Math.floor(index / 7) < 5) possibleMoves.push(13);
/*Down two over one left*/
if (index % 7 < 6 && Math.floor(index / 7) < 5) possibleMoves.push(15);
possibleMoves = possibleMoves.map((i) => i + index);
return possibleMoves;
},
},
king: {
value: 2,
spawnTries: 6,
generateMoves: (index) => {
let possibleMoves = [];
if (index % 7 > 0) possibleMoves.push(-1);
if (index % 7 < 6) possibleMoves.push(1);
if (Math.floor(index / 7) > 0) possibleMoves.push(-7);
if (Math.floor(index / 7) < 6) possibleMoves.push(7);
if (index % 7 > 0 && Math.floor(index / 7) > 0) possibleMoves.push(-8);
if (index % 7 < 6 && Math.floor(index / 7) > 0) possibleMoves.push(-6);
if (index % 7 > 0 && Math.floor(index / 7) < 6) possibleMoves.push(6);
if (index % 7 < 6 && Math.floor(index / 7) < 6) possibleMoves.push(8);
possibleMoves = possibleMoves.map((i) => i + index);
return possibleMoves;
},
},
rook: {
value: 3,
spawnTries: 4,
generateMoves: (index) => {
let possibleMoves = [];
let left = index % 7,
top = Math.floor(index / 7);
for (let i = 0; i < top; i++) {
let d = (index % 7) + (top - i - 1) * 7;
if (!isOccupied(d)) possibleMoves.push(d);
else break;
}
for (let i = 0; i < left; i++) {
let d = index - (i + 1);
if (!isOccupied(d)) possibleMoves.push(d);
else break;
}
for (let i = 0; i < 6 - left; i++) {
d = index + (i + 1);
if (!isOccupied(d)) possibleMoves.push(d);
else break;
}
for (let i = 0; i < 6 - top; i++) {
d = (index % 7) + (top + i + 1) * 7;
if (!isOccupied(d)) possibleMoves.push(d);
else break;
}
return possibleMoves;
},
},
bishop: {
value: 3,
spawnTries: 4,
generateMoves: (index) => {
let possibleMoves = [];
let left = index % 7,
top = Math.floor(index / 7);
let idash = index;
for (let i = 0; i < top && i < left; i++) {
d = idash -= 8;
if (!isOccupied(d)) possibleMoves.push(d);
else break;
}
idash = index;
for (let i = 0; i < 6 - top && i < 6 - left; i++) {
d = idash += 8;
if (!isOccupied(d)) possibleMoves.push(d);
else break;
}
idash = index;
for (let i = 0; i < top && i < 6 - left; i++) {
d = idash -= 6;
if (!isOccupied(d)) possibleMoves.push(d);
else break;
}
idash = index;
for (let i = 0; i < 6 - top && i < left; i++) {
d = idash += 6;
if (!isOccupied(d)) possibleMoves.push(d);
else break;
}
return possibleMoves;
},
},
queen: {
value: 5,
spawnTries: 1,
generateMoves: (index) => {
let possibleMoves = protopieces.bishop.generateMoves(index);
possibleMoves.push(...protopieces.rook.generateMoves(index));
return possibleMoves;
},
},
pawn: {
value: 1,
spawnTries: 4,
generateMoves: (index) => {
let possibleMoves = []
// Player Knight is not directly above or below the pawn. If it was, the pawn would not have any moves
if (playerIndex - index != -7) possibleMoves.push(-7);
if (playerIndex - index != 7) possibleMoves.push(7);
// If the difference between White Knight and Pawn is 6 or 8 and the Pwan is not on either side border,
// then the White Knight has to be diagonal from the pawn
if (playerIndex - index == -6 && (index % 7 != 0 || index % 7 != 6) ) possibleMoves.push(-6);
if (playerIndex - index == -6 && (index % 7 != 0 || index % 7 != 6) )possibleMoves.push(6);
if (playerIndex - index == 8 && (index % 7 != 0 || index % 7 != 6) ) possibleMoves.push(8);
if (playerIndex - index == -8 && index % 7 != 0) possibleMoves.push(-8);
possibleMoves = possibleMoves.map((i) => i + index);
return possibleMoves;
}
}
};
let protoArr = Object.entries(protopieces);