-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
494 lines (492 loc) · 19.4 KB
/
game.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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/**
* Created by 西爽 on 16-11-9.
*/
!function(){
var boxObj = document.getElementById('box');
var liObj = boxObj.getElementsByClassName('lili');
var horseObj = boxObj.getElementsByClassName('horse');
var fireObj = boxObj.getElementsByClassName('fire');
var elephantObj = boxObj.getElementsByClassName('elephant');
var carObj = boxObj.getElementsByClassName('car');
var soldierObj = boxObj.getElementsByClassName('soldier');
var scholarObj = boxObj.getElementsByClassName('scholar');
var kingObj = boxObj.getElementsByClassName('king');
var flashObj = document.getElementsByClassName('flash');
var oldPosX,oldPosY;
var topPos= [[0,'car'],[1,'horse'],[2,'elephant'],[3,'scholar'],[4,'king'],[5,'scholar'],[6,'elephant'],[7,'horse'],[8,'car'],[19,'fire'],[25,'fire'],[27,'soldier'],[29,'soldier'],[31,'soldier'],[33,'soldier'],[35,'soldier']];
var Sudoku=new Array(3,4,5,12,13,14,21,22,23);
var flag=0;
var count=1;
function hasClass(elem, cls) {
cls = cls || '';
if (cls.replace(/\s/g, '').length == 0) return false; //当cls没有参数时,返回false
return new RegExp(' ' + cls + ' ').test(' ' + elem.className + ' ');
}
function addClass(ele, cls) {
if (!hasClass(ele, cls)) {
ele.className = ele.className == '' ? cls : ele.className + ' ' + cls;
}
}
function removeClass(ele,cls) {
if (hasClass(ele,cls)) {
var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
ele.className=ele.className.replace(reg,' ');
}
}
function move(x,y,j){
for(var s = 0; s < liObj.length; s++) {
var qiObj = liObj[s].getElementsByTagName('div');
if(hasClass(qiObj[qiObj.length-1],'flash')){
//马的走法;
if(hasClass(flashObj[0],'horse')){
return horseF(x,y,j);
}
//炮的走法;
if(hasClass(flashObj[0],'fire')){
return fireF(x,y,j);
}
//象的走法;
if(hasClass(flashObj[0],'elephant')){
return elephantF(x,y,j);
}
//车的走法;
if(hasClass(flashObj[0],'car')){
return carF(x,y,j);
}
//兵的走法
if(hasClass(flashObj[0],'soldier')){
return soldierF(x,y,j);
}
//将的走法
if(hasClass(flashObj[0],'king')){
return kingF(x,y,j);
}
//士的走法
if(hasClass(flashObj[0],'scholar')){
return scholarF(x,y,j);
}
}
};
}
function Todo (Obj,j){
//alert(1);
for(var fHorse=0;fHorse<Obj.length;fHorse++){
if(hasClass(Obj[fHorse],'flash')){
break;
}
}
//alert(fHorse);
var cssum =liObj[j].children;
//console.log(cssum.length);
if(cssum.length>2){
//console.log(liObj[j].children);
liObj[j].appendChild(Obj[fHorse]);
liObj[j].removeChild(liObj[j].children[2]);
removeClass(flashObj[0],'flash');
if((kingObj.length<2)){
if(count%2){
alert('红色赢')
}
else{
alert('蓝色赢')
}
}
}
else{
liObj[j].appendChild(Obj[fHorse]);
removeClass(flashObj[0],'flash');
if((kingObj.length<2)){
if(count%2){
alert('红色赢')
}
else{
alert('蓝色赢')
}
}
}
return count +=1;
}
function horseF(x,y,j){
var oldPos=oldPosX+oldPosY*9;
console.log(oldPos,111)
if(
((x == oldPosX+2 && y==oldPosY-1)&&(liObj[oldPos+1].children.length<3))||
((x == oldPosX+2 && y==oldPosY+1)&&(liObj[oldPos+1].children.length<3))||
((x == oldPosX-2 && y==oldPosY-1)&&(liObj[oldPos-1].children.length<3))||
((x == oldPosX-2 && y==oldPosY+1)&&(liObj[oldPos-1].children.length<3))||
((x == oldPosX-1 && y==oldPosY+2)&&(liObj[oldPos+9].children.length<3))||
((x == oldPosX+1 && y==oldPosY+2)&&(liObj[oldPos+9].children.length<3))||
((x == oldPosX-1 && y==oldPosY-2)&&(liObj[oldPos-9].children.length<3))||
((x == oldPosX+1 && y==oldPosY-2)&&(liObj[oldPos-9].children.length<3)))
{
Todo(horseObj,j)
}
else{
removeClass(flashObj[0],'flash');
}
}
function fireF(x,y,j){
var oldPos=oldPosX+oldPosY*9;
//alert(1);
var chessPieces = liObj[j].getElementsByTagName('div')[2];//选择位置的棋子div
var ziObj = liObj[j].getElementsByTagName('div');//点击位置的div数组
for(var ffire=0;ffire<fireObj.length;ffire++){
if(hasClass(fireObj[ffire],'flash')){
//alert(1);
//alert(liObj[70].getElementsByTagName('div').length);
if(ziObj.length>2){
if((kingObj.length<2)){
if(count%2){
alert('红色赢')
}
else{
alert('蓝色赢')
}
}
//点击下去有棋子。
if(Math.abs(oldPos-j)%9==0){
//竖着走,判断中间有且只有一颗棋子
var paoMid=0;
var pmargin=(j-oldPos)/9;
for (var kpshu=0;kpshu<Math.abs(pmargin);kpshu++){
if(liObj[oldPos+9*(kpshu+1)*pmargin/Math.abs(pmargin)].getElementsByTagName('div').length>2){
paoMid+=1;
}
}
if(paoMid==2){
Todo(fireObj,j)
}
}else if(Math.abs(oldPos-j)<9){
var pmargin=j-oldPos;
var paoMid=0;
for (var kpshu=1;kpshu<Math.abs(pmargin);kpshu++){
if(liObj[oldPos+kpshu*pmargin/Math.abs(pmargin)].getElementsByTagName('div').length>2){
paoMid+=1;
}
}
if(paoMid==1){
Todo(fireObj,j)
}
}
}
else{
if((kingObj.length<2)){
if(count%2){
alert('红色赢')
}
else{
alert('蓝色赢')
}
}
//点下去没有棋子,要判断中间不包括任何棋子
if(Math.abs(oldPos-j)%9==0){
//竖着走
//alert(Math.abs(oldPos-j)/9);
var pmargin=(j-oldPos)/9;
//竖着中间隔着多少数字
for (var kpshu=0;kpshu<Math.abs(pmargin);kpshu++){
// alert(1);
if(liObj[oldPos+9*(kpshu+1)*pmargin/Math.abs(pmargin)].getElementsByTagName('div').length>2){
return false;
}
}
liObj[j].appendChild(fireObj[ffire]);
removeClass(flashObj[0],'flash');
count+=1;//炮这边有个坑 第70个位置进来的话 会到有棋子的判断里面
}
else if(Math.abs(oldPos-j)<9){
//横着走
var pmargin=j-oldPos;
for (var kpshu=1;kpshu<Math.abs(pmargin);kpshu++){
if(liObj[oldPos+kpshu*pmargin/Math.abs(pmargin)].getElementsByTagName('div').length>2){
return false;
}
}
liObj[j].appendChild(fireObj[ffire]);
removeClass(flashObj[0],'flash');
count+=1;//炮这边有个坑 第70个位置进来的话 会到有棋子的判断里面
}
}
}
}
}
function elephantF(x,y,j){
var oldPos=oldPosX+oldPosY*9;
//alert(oldPos)
if((oldPos<44&&j<44)||(oldPos>44&&j>44)){
if(
((x == oldPosX+2 && y==oldPosY+2)&&(liObj[oldPos+10].children.length<3))||
((x == oldPosX+2 && y==oldPosY-2)&&(liObj[oldPos-8].children.length<3))||
((x == oldPosX-2 && y==oldPosY+2)&&(liObj[oldPos+8].children.length<3))||
((x == oldPosX-2 && y==oldPosY-2)&&(liObj[oldPos-10].children.length<3)))
{
Todo(elephantObj,j)
}
else{
removeClass(flashObj[0],'flash');
}
}
else{
removeClass(flashObj[0],'flash');
alert('此象不能过河')
}
}
function carF(x,y,j){
var oldPos=oldPosX+oldPosY*9;
if(Math.abs(oldPos-j)%9==0){
//shuzhezou
var pmargin=(j-oldPos)/9;
for (var kpshu=0;kpshu<Math.abs(pmargin)-1;kpshu++){
// alert(1);
if(liObj[oldPos+9*(kpshu+1)*pmargin/Math.abs(pmargin)].getElementsByTagName('div').length>2){
return false;
}
}
Todo(carObj,j)
}
else if(Math.abs(oldPos-j)<9) {
//横着走
var pmargin = j - oldPos;
for (var kpshu = 1; kpshu < Math.abs(pmargin); kpshu++) {
if (liObj[oldPos + kpshu * pmargin / Math.abs(pmargin)].getElementsByTagName('div').length > 2) {
return false;
}
}
Todo(carObj,j)
}
}
function soldierF(x,y,j){
var oldPos=oldPosX+oldPosY*9;
//alert(oldPos)
if(oldPos<45){
//红色方
//console.log(j,oldPos);
//alert(count);
if(count%2==0){
//已经过河的蓝色方
if((j == oldPos-9)||(j == oldPos+1)||(j == oldPos-1)){
Todo(soldierObj,j)
}
}
else{
//红色在红色这边(兵还没有过河)
if(j == oldPos+9) {
//alert(1);
Todo(soldierObj,j)
}
else{
removeClass(flashObj[0],'flash');
}
}
}
if(oldPos>44){
if(count%2==1){
if((j == oldPos+9)||(j == oldPos+1)||(j == oldPos-1)){
Todo(soldierObj,j)
}
}
else{
if(j == oldPos-9) {
Todo(soldierObj,j)
}
else{
removeClass(flashObj[0],'flash');
}
}
}
}
function kingF(x,y,j){
var oldPos=oldPosX+oldPosY*9;
if(oldPos<45){
//红色方九宫格
for(var i in Sudoku){
//console.log(Sudoku[i]);
if(j==Sudoku[i]){
Todo(kingObj,j)
}
}
}
if(oldPos>45){
//红色方九宫格
for(var i in Sudoku){
var Sudokut=new Array;
//console.log(Sudoku[i]);
Sudokut.push(89-Sudoku[i]);
//console.log(Sudokut)
for(var c in Sudokut){
if(j==Sudokut[c]){
Todo(kingObj,j)
}
}
}
}
}
function scholarF(x,y,j){
var oldPos=oldPosX+oldPosY*9;
if(oldPos<45){
//红色方九宫格
for(var i in Sudoku){
//console.log(Sudoku[i]);
if(j==Sudoku[i]){
if(
((x == oldPosX+1 && y==oldPosY-1))||
((x == oldPosX+1 && y==oldPosY+1))||
((x == oldPosX-1 && y==oldPosY-1))||
((x == oldPosX-1 && y==oldPosY+1)))
{
Todo(scholarObj,j)
}
}
}
}
if(oldPos>45){
//红色方九宫格
for(var i in Sudoku){
var Sudokut=new Array;
//console.log(Sudoku[i]);
Sudokut.push(89-Sudoku[i]);
//console.log(Sudokut)
for(var c in Sudokut){
if(j==Sudokut[c]){
if(
((x == oldPosX+1 && y==oldPosY-1))||
((x == oldPosX+1 && y==oldPosY+1))||
((x == oldPosX-1 && y==oldPosY-1))||
((x == oldPosX-1 && y==oldPosY+1)))
{
Todo(scholarObj,j)
}
}
}
}
}
}
var map = {
//入口
init : function(){
var self=this;
self.bindEvt();
},
//创建棋盘
bindEvt : function(){
var self=this;
for(var i=0; i<90; i++){//创建棋谱
boxObj.innerHTML +=" <li class='lili'> <div class='line'></div> <div class='list'></div> </li>"
}
//创建中间部分(上半部分)
for(var midt=37;midt<44;midt++){
var listObj = liObj[midt].getElementsByClassName('list')[0];
//console.log(listObj);
listObj.setAttribute("style","height:50%");
}
//创建中间部分(下半部分)
for(var midb=46;midb<53;midb++){
var listObj = liObj[midb].getElementsByClassName('list')[0];
listObj.setAttribute("style","height:50%;top:50%");
}
//创建四周
for(var sleft=0;sleft<9;sleft++){
liObj[sleft].getElementsByClassName('list')[0].setAttribute("style","height:50%;top:50%");
liObj[89-sleft].getElementsByClassName('list')[0].setAttribute("style","height:50%;");
liObj[sleft*9].getElementsByClassName('line')[0].setAttribute("style","width:50%;left:50%");
liObj[(sleft+1)*9-1].getElementsByClassName('line')[0].setAttribute("style","width:50%;");
liObj[liObj.length-1].getElementsByClassName('line')[0].setAttribute("style","width:50%;");
liObj[liObj.length-9].getElementsByClassName('line')[0].setAttribute("style","width:50%;left:50%");
}
self.starGame();
self.getPos();
},
//布置棋子初始位置 并且作为游戏的开始
starGame : function(){
function addPos(pos,name,paly){
//console.log(pos,name,paly)
liObj[pos].innerHTML +="<div class="+"'"+name+" "+paly+"'"+"></div>";
}
//console.log(topPos[1][0],topPos[1][1]);
for(var posnum=0;posnum<topPos.length;posnum++){
addPos(topPos[posnum][0],topPos[posnum][1],'playR');
addPos(89-(topPos[posnum][0]),topPos[posnum][1],'playB');
console.log(posnum)
}
//给各个棋子添加内容
function addtext(ele,what){
var eleObj = boxObj.getElementsByClassName(ele);
//console.log(eleObj);
for (var kk=0; kk<eleObj.length;kk++){
eleObj[kk].innerHTML = what;
//console.log( eleObj[kk])
}
};
addtext('horse',"馬");
addtext('car',"车");
addtext('elephant',"象");
addtext('soldier',"兵");
addtext('fire',"炮");
addtext('king',"将");
addtext('scholar',"士");
},
//点击后获得对应位置
getPos : function(){
var self=this;
for (var i = 0; i < liObj.length; i++) {
(function(j){
liObj[j].addEventListener("click", function(e) {
//化成坐标;
var x = j%9;
var y = parseInt(j/9);
//console.log(count,999);
//console.log(j,888);
var chessPieces = liObj[j].getElementsByTagName('div')[2];//选择位置的棋子div
var ziObj = liObj[j].getElementsByTagName('div');//点击位置的div数组
// tro j:点击的位置,
if(count%2&&(ziObj.length>2)){
//奇数步并且有棋子(红方走)
if(hasClass(ziObj[ziObj.length-1],'playR')){
if(flashObj.length>0){
//若棋盘已经有闪动的棋子
removeClass(flashObj[0],'flash');
addClass(chessPieces,'flash');
return oldPosX = x,oldPosY = y,flag=1;
}
else{
addClass(chessPieces,'flash');
return oldPosX = x,oldPosY = y,flag=1;
}
}
}
if((count%2==0)&&(ziObj.length>2)){
//偶数步并且有棋子(蓝方走)
if(hasClass(ziObj[ziObj.length-1],'playB')){
if(flashObj.length>0){
//若棋盘已经有闪动的棋子
removeClass(flashObj[0],'flash');
addClass(chessPieces,'flash');
return oldPosX = x,oldPosY = y,flag=0;
}
else{
addClass(chessPieces,'flash');
return oldPosX = x,oldPosY = y,flag=0;
}
}
}
if(flag){
if(flashObj.length>0){
if(count%2&&(ziObj.length=2)){
move(x,y,j);
}
}
}
if(!flag){
if(flashObj.length>0){
if((count%2==0)&&(ziObj.length=2)){
move(x,y,j);
}
}
}
}, false);
})(i) ;
}
}
};
map.init();
}();