-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05_Checkers.html
424 lines (398 loc) · 16.6 KB
/
05_Checkers.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<HTML>
<HEAD>
<TITLE>Checkers</TITLE>
<SCRIPT LANGUAGE="JavaScript">
var table = [];
var player="X"; //players are X and Y, starts player X
var countX=12;
var countY=12;
var noDeselect=false;
var jumped=false;
var sX=8;
var sY=8;
//Board Inizialization
for (var i=3;i<5;i++)
table[i]=[];
for (var x=0;x<8;x++)
for (var y=3;y<5;y++)
table[y][x]=0;
table[0]=new Array("X",0,"X",0,"X",0,"X",0);
table[1]=new Array(0,"X",0,"X",0,"X",0,"X");
table[2]=new Array("X",0,"X",0,"X",0,"X",0);
table[5]=new Array(0,"Y",0,"Y",0,"Y",0,"Y");
table[6]=new Array("Y",0,"Y",0,"Y",0,"Y",0);
table[7]=new Array(0,"Y",0,"Y",0,"Y",0,"Y");
function move(x, y) {
if((sX==8)&&(sY==8)){ //no previous selected
if ((table[y][x]==player)||(table[y][x]==player+player)){ //Selected owned pice //player+player= XX or YY which is a king piece
sX=x;sY=y;
document.getElementById("b"+x+y).style.background='#00ff00';
noDeselect=false;
}
}else if (((table[y][x]==player)||(table[y][x]==player+player))&&(!noDeselect)){ //Change selections
document.getElementById("b"+sX+sY).style.background='#EEEEEE';
sX=x;sY=y;
document.getElementById("b"+x+y).style.background='#00ff00';
}
if(isValidMove(x,y)){ //check for valid move
document.getElementById("b"+x+y).value=document.getElementById("b"+sX+sY).value;//move
table[y][x]=table[sY][sX];
document.getElementById("b"+sX+sY).value=" ";
table[sY][sX]=0;
document.getElementById("b"+sX+sY).style.background='#EEEEEE';
//restore status variables
sX=8;
sY=8;
checkForKing(x,y);
if (jumped){ //Check if the player has another turn
if (hasAnotherTurn(x,y)){
document.getElementById("b"+x+y).style.background='#00ff00';
sX=x; sY=y;
noDeselect=true; //if player has extra turn he can move only the current piece
}else{
changePlayer();
noDeselect=false;
}
}else{
jumped=false;
noDeselect=false;
changePlayer();
}
}
}
function isValidMove(x,y){ //Check if it is a valid move
if (table[y][x]==0){ //player chose an empty cell
try{
if ((table[sY][sX]=="X")||(table[sY][sX]=="XX")){
if(!noDeselect){ //no deselect means extra turn, in extra turn player can only jump, no simple move
if ((x==sX+1)&&(y==sY+1)){ //simple move
jumped=false;
return true;
}
if ((x==sX-1)&&(y==sY+1)){
jumped=false;
return true;
}
}
if ((table[sY+1][sX+1]==player)&&(x==sX+2)&&(y==sY+2)){ //jump no eating
jumped=true;
return true;
}
if ((table[sY+1][sX-1]==player)&&(x==sX-2)&&(y==sY+2)){
jumped=true;
return true;
}
if ((table[sY+1][sX+1]=="Y")&&(x==sX+2)&&(y==sY+2)){ //jump eating
eat(sX+1,sY+1);
jumped=true;
return true;
}
if ((table[sY+1][sX-1]=="Y")&&(x==sX-2)&&(y==sY+2)){
eat(sX-1,sY+1);
jumped=true;
return true;
}
}else if ((table[sY][sX]=="Y")||(table[sY][sX]=="YY")){
if(!noDeselect){
if ((x==sX+1)&&(y==sY-1)){
jumped=false;
return true;
}
if ((x==sX-1)&&(y==sY-1)){
jumped=false;
return true;
}
}
if ((table[sY-1][sX+1]==player)&&(x==sX+2)&&(y==sY-2)){
jumped=true;
return true;
}
if ((table[sY-1][sX-1]==player)&&(x==sX-2)&&(y==sY-2)){
jumped=true;
return true;
}
if ((table[sY-1][sX+1]=="X")&&(x==sX+2)&&(y==sY-2)){
eat(sX+1,sY-1);
jumped=true;
return true;
}
if ((table[sY-1][sX-1]=="X")&&(x==sX-2)&&(y==sY-2)){
eat(sX-1,sY-1);
jumped=true;
return true;
}
}
}catch(err){} //This try catch prevent errors I go out of the bounds of the array
try{
if (table[sY][sX]=="XX"){
//In this case the piece can move backwards so check those moves
if(!noDeselect){
if ((x==sX+1)&&(y==sY-1)){ //simple move
jumped=false;
return true;
}
if ((x==sX-1)&&(y==sY-1)){
jumped=false;
return true;
}
}
if ((table[sY-1][sX+1]==player)&&(x==sX+2)&&(y==sY-2)){ //jump no eating
jumped=true;
return true;
}
if ((table[sY-1][sX-1]==player)&&(x==sX-2)&&(y==sY-2)){
jumped=true;
return true;
}
if ((table[sY-1][sX+1]==player+player)&&(x==sX+2)&&(y==sY-2)){ //jump king
jumped=true;
return true;
}
if ((table[sY-1][sX-1]==player+player)&&(x==sX-2)&&(y==sY-2)){
jumped=true;
return true;
}
if ((table[sY-1][sX+1]=="Y")&&(x==sX+2)&&(y==sY-2)){ //jump eating
eat(sX+1,sY-1);
jumped=true;
return true;
}
if ((table[sY-1][sX-1]=="Y")&&(x==sX-2)&&(y==sY-2)){
eat(sX-1,sY-1);
jumped=true;
return true;
}
if ((table[sY-1][sX+1]=="YY")&&(x==sX+2)&&(y==sY-2)){ //jump eating king
eat(sX+1,sY-1);
jumped=true;
return true;
}
if ((table[sY-1][sX-1]=="YY")&&(x==sX-2)&&(y==sY-2)){
eat(sX-1,sY-1);
jumped=true;
return true;
}
}
}catch(err){}
try{
if (table[sY][sX]=="YY"){
if(!noDeselect){
if ((x==sX+1)&&(y==sY+1)){ //simple move
jumped=false;
return true;
}
if ((x==sX-1)&&(y==sY+1)){
jumped=false;
return true;
}
}
if ((table[sY+1][sX+1]==player)&&(x==sX+2)&&(y==sY+2)){ //jump no eating
jumped=true;
return true;
}
if ((table[sY+1][sX-1]==player)&&(x==sX-2)&&(y==sY+2)){
jumped=true;
return true;
}
if ((table[sY+1][sX+1]==player+player)&&(x==sX+2)&&(y==sY+2)){ //jump king
jumped=true;
return true;
}
if ((table[sY+1][sX-1]==player+player)&&(x==sX-2)&&(y==sY+2)){
jumped=true;
return true;
}
if ((table[sY+1][sX+1]=="X")&&(x==sX+2)&&(y==sY+2)){ //jump eating
eat(sX+1,sY+1);
jumped=true;
return true;
}
if ((table[sY+1][sX-1]=="X")&&(x==sX-2)&&(y==sY+2)){
eat(sX-1,sY+1);
jumped=true;
return true;
}
if ((table[sY+1][sX+1]=="XX")&&(x==sX+2)&&(y==sY+2)){ //jump eating king
eat(sX+1,sY+1);
jumped=true;
return true;
}
if ((table[sY+1][sX-1]=="XX")&&(x==sX-2)&&(y==sY+2)){
eat(sX-1,sY+1);
jumped=true;
return true;
}
}
}catch(err){}
}
return false;
}
function checkForKing(x,y){ //check if the pice should become king
//who is playing?
if(player=="X"){
//is it at the end of the boad?
if(y==7){
//is king!
table[y][x]=player+player;
document.getElementById("b"+x+y).value=player+player;
}
}else{
if(y==0){
//is king!
table[y][x]=player+player;
document.getElementById("b"+x+y).value=player+player;
}
}
}
function hasAnotherTurn(x,y){ //When the player jumps it is possible to have another turn, check if conditions are valid
try{
if ((table[y][x]=="X")||(table[y][x]=="XX")){
if ((table[y+1][x+1]!=0)&&(table[y+2][x+2]==0))
return true;
if ((table[y+1][x-1]!=0)&&(table[y+2][x-2]==0))
return true;
}else if ((table[y][x]=="Y")||(table[y][x]=="YY")){
if ((table[y-1][x+1]!=0)&&(table[y-2][x+2]==0))
return true;
if ((table[y-1][x-1]!=0)&&(table[y-2][x-2]==0))
return true;
}
}catch(err){}
try{
if (table[y][x]=="XX"){
if ((table[y-1][x+1]!=0)&&(table[y-2][x+2]==0))
return true;
if ((table[y-1][x-1]!=0)&&(table[y-2][x-2]==0))
return true;
}else if (table[y][x]=="YY"){
if ((table[y+1][x+1]!=0)&&(table[y+2][x+2]==0))
return true;
if ((table[y+1][x-1]!=0)&&(table[y+2][x-2]==0))
return true;
}
}catch(err){}
return false;
}
function eat(x,y){ //delete piece and decrease count
document.getElementById("b"+x+y).value=" ";
if((table[y][x]=="X")||(table[y][x]=="XX"))
countX--;
else
countY--;
table[y][x]=0;
document.getElementById("writeHere").innerHTML="player X = "+countX+" player Y = "+countY;
if (countX==0)
endGame("Y");
if (countY==0)
endGame("X");
}
function changePlayer(){
if (player=="X")
player="Y";
else
player="X";
}
function endGame(player){
alert("Player "+player+" won, Congrats!");
window.location.reload(0);
}
function skipTurn(){ //let player skip an extra turn. Without this the game could enter a loophole
if (noDeselect){
noDeselect=false;
document.getElementById("b"+sX+sY).style.background='#EEEEEE';
sX=8;sY=8;
changePlayer();
}else{
alert("Sorry, you can skip turn only after jump")
}
}
</SCRIPT>
</HEAD>
<BODY>
Let's play Checkers. Starts player X.<br>
<INPUT id="skip" TYPE="button" Value="Skip Turn" onClick="skipTurn()">
<table>
<tr>
<td><INPUT id="b00" TYPE="button" style="background-color:EEEEEE" Value="X" onClick="move(0,0)"></td>
<td><INPUT id="b10" TYPE="button" style="background-color:555555" Value=" " onClick="move(1,0)"></td>
<td><INPUT id="b20" TYPE="button" style="background-color:EEEEEE" Value="X" onClick="move(2,0)"></td>
<td><INPUT id="b30" TYPE="button" style="background-color:555555" Value=" " onClick="move(3,0)"></td>
<td><INPUT id="b40" TYPE="button" style="background-color:EEEEEE" Value="X" onClick="move(4,0)"></td>
<td><INPUT id="b50" TYPE="button" style="background-color:555555" Value=" " onClick="move(5,0)"></td>
<td><INPUT id="b60" TYPE="button" style="background-color:EEEEEE" Value="X" onClick="move(6,0)"></td>
<td><INPUT id="b70" TYPE="button" style="background-color:555555" Value=" " onClick="move(7,0)"></td>
</tr>
<tr>
<td><INPUT id="b01" TYPE="button" style="background-color:555555" Value=" " onClick="move(0,1)"></td>
<td><INPUT id="b11" TYPE="button" style="background-color:EEEEEE" Value="X" onClick="move(1,1)"></td>
<td><INPUT id="b21" TYPE="button" style="background-color:555555" Value=" " onClick="move(2,1)"></td>
<td><INPUT id="b31" TYPE="button" style="background-color:EEEEEE" Value="X" onClick="move(3,1)"></td>
<td><INPUT id="b41" TYPE="button" style="background-color:555555" Value=" " onClick="move(4,1)"></td>
<td><INPUT id="b51" TYPE="button" style="background-color:EEEEEE" Value="X" onClick="move(5,1)"></td>
<td><INPUT id="b61" TYPE="button" style="background-color:555555" Value=" " onClick="move(6,1)"></td>
<td><INPUT id="b71" TYPE="button" style="background-color:EEEEEE" Value="X" onClick="move(7,1)"></td>
</tr>
<tr>
<td><INPUT id="b02" TYPE="button" style="background-color:EEEEEE" Value="X" onClick="move(0,2)"></td>
<td><INPUT id="b12" TYPE="button" style="background-color:555555" Value=" " onClick="move(1,2)"></td>
<td><INPUT id="b22" TYPE="button" style="background-color:EEEEEE" Value="X" onClick="move(2,2)"></td>
<td><INPUT id="b32" TYPE="button" style="background-color:555555" Value=" " onClick="move(3,2)"></td>
<td><INPUT id="b42" TYPE="button" style="background-color:EEEEEE" Value="X" onClick="move(4,2)"></td>
<td><INPUT id="b52" TYPE="button" style="background-color:555555" Value=" " onClick="move(5,2)"></td>
<td><INPUT id="b62" TYPE="button" style="background-color:EEEEEE" Value="X" onClick="move(6,2)"></td>
<td><INPUT id="b72" TYPE="button" style="background-color:555555" Value=" " onClick="move(7,2)"></td>
</tr>
<tr>
<td><INPUT id="b03" TYPE="button" style="background-color:555555" Value=" " onClick="move(0,3)"></td>
<td><INPUT id="b13" TYPE="button" style="background-color:EEEEEE" Value=" " onClick="move(1,3)"></td>
<td><INPUT id="b23" TYPE="button" style="background-color:555555" Value=" " onClick="move(2,3)"></td>
<td><INPUT id="b33" TYPE="button" style="background-color:EEEEEE" Value=" " onClick="move(3,3)"></td>
<td><INPUT id="b43" TYPE="button" style="background-color:555555" Value=" " onClick="move(4,3)"></td>
<td><INPUT id="b53" TYPE="button" style="background-color:EEEEEE" Value=" " onClick="move(5,3)"></td>
<td><INPUT id="b63" TYPE="button" style="background-color:555555" Value=" " onClick="move(6,3)"></td>
<td><INPUT id="b73" TYPE="button" style="background-color:EEEEEE" Value=" " onClick="move(7,3)"></td>
</tr>
<tr>
<td><INPUT id="b04" TYPE="button" style="background-color:EEEEEE" Value=" " onClick="move(0,4)"></td>
<td><INPUT id="b14" TYPE="button" style="background-color:555555" Value=" " onClick="move(1,4)"></td>
<td><INPUT id="b24" TYPE="button" style="background-color:EEEEEE" Value=" " onClick="move(2,4)"></td>
<td><INPUT id="b34" TYPE="button" style="background-color:555555" Value=" " onClick="move(3,4)"></td>
<td><INPUT id="b44" TYPE="button" style="background-color:EEEEEE" Value=" " onClick="move(4,4)"></td>
<td><INPUT id="b54" TYPE="button" style="background-color:555555" Value=" " onClick="move(5,4)"></td>
<td><INPUT id="b64" TYPE="button" style="background-color:EEEEEE" Value=" " onClick="move(6,4)"></td>
<td><INPUT id="b74" TYPE="button" style="background-color:555555" Value=" " onClick="move(7,4)"></td>
</tr>
<tr>
<td><INPUT id="b05" TYPE="button" style="background-color:555555" Value=" " onClick="move(0,5)"></td>
<td><INPUT id="b15" TYPE="button" style="background-color:EEEEEE" Value="Y" onClick="move(1,5)"></td>
<td><INPUT id="b25" TYPE="button" style="background-color:555555" Value=" " onClick="move(2,5)"></td>
<td><INPUT id="b35" TYPE="button" style="background-color:EEEEEE" Value="Y" onClick="move(3,5)"></td>
<td><INPUT id="b45" TYPE="button" style="background-color:555555" Value=" " onClick="move(4,5)"></td>
<td><INPUT id="b55" TYPE="button" style="background-color:EEEEEE" Value="Y" onClick="move(5,5)"></td>
<td><INPUT id="b65" TYPE="button" style="background-color:555555" Value=" " onClick="move(6,5)"></td>
<td><INPUT id="b75" TYPE="button" style="background-color:EEEEEE" Value="Y" onClick="move(7,5)"></td>
</tr>
<tr>
<td><INPUT id="b06" TYPE="button" style="background-color:EEEEEE" Value="Y" onClick="move(0,6)"></td>
<td><INPUT id="b16" TYPE="button" style="background-color:555555" Value=" " onClick="move(1,6)"></td>
<td><INPUT id="b26" TYPE="button" style="background-color:EEEEEE" Value="Y" onClick="move(2,6)"></td>
<td><INPUT id="b36" TYPE="button" style="background-color:555555" Value=" " onClick="move(3,6)"></td>
<td><INPUT id="b46" TYPE="button" style="background-color:EEEEEE" Value="Y" onClick="move(4,6)"></td>
<td><INPUT id="b56" TYPE="button" style="background-color:555555" Value=" " onClick="move(5,6)"></td>
<td><INPUT id="b66" TYPE="button" style="background-color:EEEEEE" Value="Y" onClick="move(6,6)"></td>
<td><INPUT id="b76" TYPE="button" style="background-color:555555" Value=" " onClick="move(7,6)"></td>
</tr>
<tr>
<td><INPUT id="b07" TYPE="button" style="background-color:555555" Value=" " onClick="move(0,7)"></td>
<td><INPUT id="b17" TYPE="button" style="background-color:EEEEEE" Value="Y" onClick="move(1,7)"></td>
<td><INPUT id="b27" TYPE="button" style="background-color:555555" Value=" " onClick="move(2,7)"></td>
<td><INPUT id="b37" TYPE="button" style="background-color:EEEEEE" Value="Y" onClick="move(3,7)"></td>
<td><INPUT id="b47" TYPE="button" style="background-color:555555" Value=" " onClick="move(4,7)"></td>
<td><INPUT id="b57" TYPE="button" style="background-color:EEEEEE" Value="Y" onClick="move(5,7)"></td>
<td><INPUT id="b67" TYPE="button" style="background-color:555555" Value=" " onClick="move(6,7)"></td>
<td><INPUT id="b77" TYPE="button" style="background-color:EEEEEE" Value="Y" onClick="move(7,7)"></td>
</tr>
</table>
<div id="writeHere"></div>
</BODY>
</HTML>