-
Notifications
You must be signed in to change notification settings - Fork 0
/
russian.html
400 lines (350 loc) · 13.1 KB
/
russian.html
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
<html>
<head>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<table id="chessboard" onclick="handleClick(event)">
<tbody>
<tr>
<td id="a1"></td><td id="b1"></td><td id="c1"></td><td id="d1"></td><td id="e1"></td><td id="f1"></td><td id="g1"></td><td id="h1"></td>
</tr>
<tr>
<td id="a2"></td><td id="b2"></td><td id="c2"></td><td id="d2"></td><td id="e2"></td><td id="f2"></td><td id="g2"></td><td id="h2"></td>
</tr>
<tr>
<td id="a3"></td><td id="b3"></td><td id="c3"></td><td id="d3"></td><td id="e3"></td><td id="f3"></td><td id="g3"></td><td id="h3"></td>
</tr>
<tr>
<td id="a4"></td><td id="b4"></td><td id="c4"></td><td id="d4"></td><td id="e4"></td><td id="f4"></td><td id="g4"></td><td id="h4"></td>
</tr>
<tr>
<td id="a5"></td><td id="b5"></td><td id="c5"></td><td id="d5"></td><td id="e5"></td><td id="f5"></td><td id="g5"></td><td id="h5"></td>
</tr>
<tr>
<td id="a6"></td><td id="b6"></td><td id="c6"></td><td id="d6"></td><td id="e6"></td><td id="f6"></td><td id="g6"></td><td id="h6"></td>
</tr>
<tr>
<td id="a7"></td><td id="b7"></td><td id="c7"></td><td id="d7"></td><td id="e7"></td><td id="f7"></td><td id="g7"></td><td id="h7"></td>
</tr>
<tr>
<td id="a8"></td><td id="b8"></td><td id="c8"></td><td id="d8"></td><td id="e8"></td><td id="f8"></td><td id="g8"></td><td id="h8"></td>
</tr>
</tbody>
</table>
<div class="text-center text-success" id="text"></div>
</body>
</html>
<style>
body {
background: #f2f2e1;
width: 100%;
}
td {
width: 50px;
height: 50px;
text-align: center;
line-height: 50px;
}
tr:nth-child(even)>td:nth-child(odd){
background-color: #57aa6a;
}
tr:nth-child(even)>td:nth-child(even){
background-color: #f2f2e1;
}
tr:nth-child(odd)>td:nth-child(odd){
background-color: #f2f2e1;
}
tr:nth-child(odd)>td:nth-child(even){
background-color: #57aa6a;
}
#chessboard {
border-collapse: collapse;
margin: 0 auto;
border: 3px solid #57aa6a;
}
img {
height: 48px;
}
#text{
margin-top: 30px;
}
</style>
<script>
$(function() {
initiateTable();
});
// Declaring ticks of the checkers game table.
var horizontalTicks = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
var verticalTicks = ['1', '2', '3', '4', '5', '6', '7', '8'];
// White coordinates and path to image.
var whiteCoordinates = ['a8', 'c8', 'e8', 'g8', 'b7', 'd7', 'f7', 'h7', 'a6', 'c6', 'e6', 'g6'];
var whitePath = 'img/1.png';
var masterWhitePath = 'img/master1.png';
// Red coordinates and path to image.
var redCoordinates = ['a2', 'c2', 'e2', 'g2', 'b1', 'd1', 'f1', 'h1', 'b3', 'd3', 'f3', 'h3'];
var redPath = 'img/2.png';
var masterRedPath = 'img/master2.png';
// An array that are used in controlling unnecessary stylings.
var styledElements = [];
// These variables are used for controlling piece movements.
var lastClickedImageCoordinate;
var lastClickedImagePath; // White starts.
var conqueredEnemyPiece;
var moveCounter = 0;
var lastMovedImage;
// Get coordinate and insert images.
function insertImage(coordinate, pngPath){
var img = new Image();
var div = document.getElementById(coordinate);
img.onload = function() {
div.appendChild(img);
};
if( verticalTicks.indexOf(coordinate.slice(1,2)) == 0 && pngPath == whitePath ){
img.src = masterWhitePath;
}
else if( verticalTicks.indexOf(coordinate.slice(1,2)) == 7 && pngPath == redPath ){
img.src = masterRedPath;
}
else{
img.src = pngPath;
}
}
// Remove images from given coordinate
function removeImage(coordinate){
var coordinate = document.getElementById(coordinate);
var image = $(coordinate).find("img");
$(image).remove();
}
// On game start, insert images to checkers table.
function initiateTable(){
for(var i=0; i < whiteCoordinates.length; i++){
insertImage(whiteCoordinates[i], whitePath);
insertImage(redCoordinates[i], redPath);
}
}
// Listens for click events, then sends clicked cell info to relevant function.
function handleClick(e) {
// Call decider function.
decideWhereToMove(e.target);
}
//
function clearGlobalVariables(){
// For each styled element, clear styling.
for(var i=0; i<styledElements.length; i++){
document.getElementById(styledElements[i]).removeAttribute("style");
}
// Clear that array.
styledElements = [];
// Clear enemy piece variable.
conqueredEnemyPiece = undefined;
// Clear alert text.
document.getElementById('text').innerHTML = '';
}
// Do necessary checks for the clicked cell and decide where to move.
function decideWhereToMove(clickedCell){
// If clicked cell is an image element, then do apply decision phases.
if ( $(clickedCell).is('img') ){
// Get image parents id and images src value.
var imgCoordinate = $(clickedCell).parent().attr("id");
var imgPath = $(clickedCell).attr("src");
if(moveCounter == 0){
var ifStatement = 'imgPath !== whitePath';
var innerHTMLText = "<div class='col-md-4'></div><div class='col-md-4 alert alert-danger text-center'><strong>OOPS!</strong> WHITE STARTS!</div><div class='col-md-4'></div ";
}
else{
var ifStatement = "imgPath == lastMovedImage";
var innerHTMLText = "<div class='col-md-4'></div><div class='col-md-4 alert alert-danger text-center'><strong>OOPS!</strong> OPPONENT'S TURN!</div><div class='col-md-4'></div ";
}
if( eval(ifStatement) ){
document.getElementById('text').innerHTML = innerHTMLText;
}
else{
// Store these variables in general variables.
lastClickedImageCoordinate = imgCoordinate;
lastClickedImagePath = imgPath;
if(imgPath.indexOf('master') > -1){
var masterPossibleMoves = getMasterPossibleMoves(imgCoordinate);
var finalPossibleMoves = getCheckedPossibleMoves(masterPossibleMoves, imgPath, imgCoordinate);
}
else{
// Find possible moves for this cell.
var regularPossibleMoves = getRegularPossibleMoves(imgCoordinate, imgPath);
var irregularPossibleMoves = getRegularPossibleMoves(imgCoordinate, imgPath, 1);
// Apply some rules for suggested moves.
var checkedPossibleMoves = getCheckedPossibleMoves(regularPossibleMoves, imgPath, imgCoordinate);
var irCheckedPossibleMoves = getCheckedPossibleMoves(irregularPossibleMoves, imgPath, imgCoordinate, 1);
if(checkedPossibleMoves['ignoreRegularMoves'] == 1){
var finalPossibleMoves = checkedPossibleMoves.concat(irCheckedPossibleMoves);
}
else if(irCheckedPossibleMoves.length > 0 && checkedPossibleMoves['ignoreRegularMoves'] !== 1){
var finalPossibleMoves = irCheckedPossibleMoves;
}
else{
var finalPossibleMoves = checkedPossibleMoves;
}
}
// Manipulate each cell that is suggested as a possible move.
for(var i=0; i<finalPossibleMoves.length; i++){
if ( finalPossibleMoves[i] != null){
document.getElementById(finalPossibleMoves[i]).style.border = "1px solid #000";
styledElements.push(finalPossibleMoves[i]);
}
}
}
}
// If clicked cell is not an image element, execute the following statements.
else{
var cellCoordinate = $(clickedCell).attr("id");
// If clicked cell is in suggested moves to the followings.
if ( $.inArray(cellCoordinate, styledElements) > -1 ){
// Insert new image to the cell that clicked.
insertImage(cellCoordinate, lastClickedImagePath);
// Increment counter.
moveCounter++;
// Change last moved item.
lastMovedImage = lastClickedImagePath;
// Remove old image.
removeImage(lastClickedImageCoordinate);
// Remove enemy if there is any.
removeImage(conqueredEnemyPiece);
}
// Clear all global variables in order to calculate next moves without any error.
clearGlobalVariables();
}
}
// Declare possible coordinates to move for each given coordinate-color.
function getRegularPossibleMoves(coordinate, path, irregular){
var coordinateHorizontal = horizontalTicks.indexOf(coordinate.slice(0,1));
var coordinateVertical = verticalTicks.indexOf(coordinate.slice(1,2));
var result = [];
// Required controls for reverse possible moves.
if(irregular == 1){
if(path == whitePath){
path = redPath;
}
else{
path = whitePath;
}
}
if(path == 'img/1.png'){
if (coordinateHorizontal == 0){
result = [horizontalTicks[coordinateHorizontal+1] + verticalTicks[coordinateVertical-1]];
}
else if(coordinateHorizontal == 7){
result = [horizontalTicks[coordinateHorizontal-1] + verticalTicks[coordinateVertical-1]];
}
else{
result = [horizontalTicks[coordinateHorizontal-1] + verticalTicks[coordinateVertical-1], horizontalTicks[coordinateHorizontal+1] + verticalTicks[coordinateVertical-1]];
}
}
else{
if (coordinateHorizontal == 0){
result = [horizontalTicks[coordinateHorizontal+1] + verticalTicks[coordinateVertical+1]];
}
else if(coordinateHorizontal == 7){
result = [horizontalTicks[coordinateHorizontal-1] + verticalTicks[coordinateVertical+1]];
}
else{
result = [horizontalTicks[coordinateHorizontal-1] + verticalTicks[coordinateVertical+1], horizontalTicks[coordinateHorizontal+1] + verticalTicks[coordinateVertical+1]];
}
}
return result;
}
// Check if there is an opponent soldier in the cell that you are about to move.
function getCheckedPossibleMoves(regularPossibleMoves, imgPath, currentCoordinate, isIrregular){
var checkedPossibleMoves = [];
var conquerVal = [];
var irregularConquers = [];
for(var i=0; i<regularPossibleMoves.length; i++){
var element = document.getElementById(regularPossibleMoves[i]);
if ( $(element).find('img').length > 0 ){
if ( $(element).find('img').attr("src").indexOf(imgPath) !== -1 ){
checkedPossibleMoves.push(null);
}
else{
checkedPossibleMoves[i] = conquerEnemyPiece(currentCoordinate, regularPossibleMoves[i]);
conquerVal.push(checkedPossibleMoves[i]);
if(isIrregular == 1){
irregularConquers.push(checkedPossibleMoves[i]);
}
}
}
else{
checkedPossibleMoves.push(regularPossibleMoves[i]);
}
}
// If irregular, just send the possible conquer moves.
if(isIrregular == 1){
return irregularConquers;
}
else{
// If there is a chance to conquer an enemy piece, ignore other move possibilities.
if(conquerVal.length > 0 && isNan(conquerVal) == false){
conquerVal['ignoreRegularMoves'] = 1;
return conquerVal;
}
else{
return checkedPossibleMoves;
}
}
}
// Necessary controls: if there is another piece of enemy behind the piece that we're about to conquer, then don't suggest this.
function conquerEnemyPiece(currentCoordinate, enemyCoordinate){
var result = '';
var currentCoordinateHorizontal = horizontalTicks.indexOf(currentCoordinate.slice(0,1));
var currentCoordinateVertical = verticalTicks.indexOf(currentCoordinate.slice(1,2));
var enemyCoordinateHorizontal = horizontalTicks.indexOf(enemyCoordinate.slice(0,1));
var enemyCoordinateVertical = verticalTicks.indexOf(enemyCoordinate.slice(1,2));
if (enemyCoordinateHorizontal == 0 || enemyCoordinateHorizontal == 7){
result = null;
}
else if(enemyCoordinateVertical == 0 || enemyCoordinateVertical == 7) {
result = null;
}
else{
if(currentCoordinateHorizontal < enemyCoordinateHorizontal && currentCoordinateVertical < enemyCoordinateVertical){
result = horizontalTicks[enemyCoordinateHorizontal+1] + verticalTicks[enemyCoordinateVertical+1];
}
else if(currentCoordinateHorizontal > enemyCoordinateHorizontal && currentCoordinateVertical < enemyCoordinateVertical){
result = horizontalTicks[enemyCoordinateHorizontal-1] + verticalTicks[enemyCoordinateVertical+1];
}
else if(currentCoordinateHorizontal < enemyCoordinateHorizontal && currentCoordinateVertical > enemyCoordinateVertical){
result = horizontalTicks[enemyCoordinateHorizontal+1] + verticalTicks[enemyCoordinateVertical-1];
}
else{
result = horizontalTicks[enemyCoordinateHorizontal-1] + verticalTicks[enemyCoordinateVertical-1];
}
}
// Check if the cell that current piece is about to move is empty or not.
if(result !== null){
resultElement = document.getElementById(result);
if ( $(resultElement).find('img').length > 0 ){
return null;
}
else{
conqueredEnemyPiece = enemyCoordinate;
return result;
}
}
}
// Checks if an array contains full null variables.
function isNan(array){
return array.join().replace(/,/g,'').length === 0;
};
// Calculate possible moves for a master soldier.
function getMasterPossibleMoves(coordinate){
var coordinateHorizontal = horizontalTicks.indexOf(coordinate.slice(0,1));
var coordinateVertical = verticalTicks.indexOf(coordinate.slice(1,2));
var result = [];
for(var i=0; i<horizontalTicks.length; i++){
for(var k=0; k<verticalTicks.length; k++){
if(Math.abs(coordinateHorizontal-i) == Math.abs(coordinateVertical-k)){
result.push(horizontalTicks[i] + verticalTicks[k]);
}
}
}
return result;
}
</script>