首页新闻找找看学习计划

jquery做的五子棋,怎么调整难度?

0
悬赏园豆:100 [已解决问题] 解决于 2015-09-01 10:02
  1 <script>
  2 $(document).ready(function() {
  3     fiveChess.init();
  4 });
  5 
  6 var fiveChess = {
  7     NO_CHESS: 0,
  8     BLACK_CHESS: -1,//黑棋
  9     WHITE_CHESS: 1,//白棋
 10     chessArr: [],    //记录棋子
 11     chessBoardHtml: "",
 12     humanPlayer: "black",    //玩家棋子颜色
 13     AIPlayer: "white",    //AI棋子颜色
 14     isPlayerTurn: true, //轮到player下棋
 15     totalGames: cookieHandle.getCookie("totalGames") || 0,    //总局数
 16     winGames: cookieHandle.getCookie("winGames") || 0,    //玩家赢局数
 17     isGameStart: false,    //游戏已经开始
 18     isGameOver: false, //游戏结束
 19     playerLastChess: [], //玩家最后下子位置
 20     AILastChess: [], //AI最后下子位置
 21 
 22     init: function () {
 23         this.chessBoardHtml = $(".chessboard").html();
 24         var _fiveChess = this;
 25         //右侧操作按钮
 26         $(".operating-panel a").click(function (event) {
 27             event.preventDefault();
 28             var id = $(this).attr("id");
 29             if (_fiveChess.isGameStart && id !== "replay_btn" ) { return; }    //正在游戏 不操作
 30             switch (id) {
 31                 case "black_btn":
 32                     _fiveChess.humanPlayer = "black";
 33                     _fiveChess.AIPlayer = "white";
 34                     break;
 35                 case "white_btn":
 36                     _fiveChess.humanPlayer = "white";
 37                     _fiveChess.AIPlayer = "black";
 38                     break;
 39                 case "first_move_btn":
 40                     _fiveChess.isPlayerTurn = true;
 41                     break;
 42                 case "second_move_btn":
 43                     _fiveChess.isPlayerTurn = false;
 44                     break;
 45                 case "replay_btn":
 46                     _fiveChess.resetChessBoard();
 47                     if (_fiveChess.isGameStart) {    //点重玩
 48                         _fiveChess.gameover();
 49                     }
 50                     else {    //点开始
 51                         _fiveChess.gameStart();
 52                     }
 53                     break;
 54             }
 55             });
 56         this.resetChessBoard();
 57     },
 58     //重置棋盘
 59     resetChessBoard: function () {
 60         $("div.chessboard").html(this.chessBoardHtml);
 61         $("#result_tips").html("");
 62         this.isGameOver = false;
 63         this.isPlayerTurn = $("#first_move_btn").hasClass("selected");
 64         //初始化棋子状态
 65         var i, j;
 66         for (i = 0; i < 15; i++) {
 67             this.chessArr[i] = [];
 68             for (j = 0; j < 15; j++) {
 69                 this.chessArr[i][j] = this.NO_CHESS;
 70             }
 71         }
 72         //player下棋事件
 73         var _fiveChess = this;
 74         $("div.chessboard div").click(function () {
 75             if (!_fiveChess.isPlayerTurn || _fiveChess.isGameOver) {
 76                 return;
 77             }
 78             if (!_fiveChess.isGameStart) {
 79                 _fiveChess.gameStart();
 80             }
 81             var index = $(this).index(),
 82                 i = index / 15 | 0,
 83                 j = index % 15;
 84             if (_fiveChess.chessArr[i][j] === _fiveChess.NO_CHESS) {
 85                 _fiveChess.playChess(i, j, _fiveChess.humanPlayer);
 86                 if (i === 0 && j === 0) {
 87                     $(this).removeClass("hover-up-left");
 88                 }
 89                 else if (i === 0 && j === 14) {
 90                     $(this).removeClass("hover-up-right");
 91                 }
 92                 else if (i === 14 && j === 0) {
 93                     $(this).removeClass("hover-down-left");
 94                 }
 95                 else if (i === 14 && j === 14) {
 96                     $(this).removeClass("hover-down-right");
 97                 }
 98                 else if (i === 0) {
 99                     $(this).removeClass("hover-up");
100                 }
101                 else if (i === 14) {
102                     $(this).removeClass("hover-down");
103                 }
104                 else if (j === 0) {
105                     $(this).removeClass("hover-left");
106                 }
107                 else if (j === 14) {
108                     $(this).removeClass("hover-right");
109                 }
110                 else {
111                     $(this).removeClass("hover");
112                 }
113                 _fiveChess.playerLastChess = [i, j];
114                 _fiveChess.playerWinOrNot(i, j);
115             }
116         });
117         //鼠标在棋盘上移动效果
118         $("div.chessboard div").hover(//hover事件
119             function () {
120                 if (!_fiveChess.isPlayerTurn || _fiveChess.isGameOver) { return; }
121                 var index = $(this).index(),
122                     i = index / 15 | 0,
123                     j = index % 15;
124                 if (_fiveChess.chessArr[i][j] === _fiveChess.NO_CHESS) {
125                     /**===的意思是:要是两个值类型不同,返回false 
126                        要是两个值都是number类型,并且数值相同,返回true 
127                        要是两个值都是stirng,并且两个值的String内容相同,返回true 
128                        要是两个值都是true或者都是false,返回true 
129                        要是两个值都是指向相同的Object,Arraya或者function,返回true 
130                        要是两个值都是null或者都是undefined,返回true */
131                     if (i === 0 && j === 0) {
132                         $(this).addClass("hover-up-left");
133                     }
134                     else if (i === 0 && j === 14) {
135                         $(this).addClass("hover-up-right");
136                     }
137                     else if (i === 14 && j === 0) {
138                         $(this).addClass("hover-down-left");
139                     }
140                     else if (i === 14 && j === 14) {
141                         $(this).addClass("hover-down-right");
142                     }
143                     else if (i === 0) {
144                         $(this).addClass("hover-up");
145                     }
146                     else if (i === 14) {
147                         $(this).addClass("hover-down");
148                     }
149                     else if (j === 0) {
150                         $(this).addClass("hover-left");
151                     }
152                     else if (j === 14) {
153                         $(this).addClass("hover-right");
154                     }
155                     else {
156                         $(this).addClass("hover");
157                     }
158                 }
159             },
160             function () {
161                 if (!_fiveChess.isPlayerTurn || _fiveChess.isGameOver) { return; }
162                 var index = $(this).index(),
163                     i = index / 15 | 0,
164                     j = index % 15;
165                     if (i === 0 && j === 0) {
166                         $(this).removeClass("hover-up-left");
167                     }
168                     else if (i === 0 && j === 14) {
169                         $(this).removeClass("hover-up-right");
170                     }
171                     else if (i === 14 && j === 0) {
172                         $(this).removeClass("hover-down-left");
173                     }
174                     else if (i === 14 && j === 14) {
175                         $(this).removeClass("hover-down-right");
176                     }
177                     else if (i === 0) {
178                         $(this).removeClass("hover-up");
179                     }
180                     else if (i === 14) {
181                         $(this).removeClass("hover-down");
182                     }
183                     else if (j === 0) {
184                         $(this).removeClass("hover-left");
185                     }
186                     else if (j === 14) {
187                         $(this).removeClass("hover-right");
188                     }
189                     else {
190                         $(this).removeClass("hover");
191                     }
192             }
193         );
194     },
195     gameStart: function () {//游戏开始方法
196         this.totalGames++;
197         //AI先手
198         if (!this.isPlayerTurn) {
199             this.AImoveChess();
200         }
201         this.isGameStart = true;
202         $(".operating-panel p a").addClass("disable");
203         $("#replay_btn").html("重玩");
204     },
205     gameOver: function () {//游戏结束方法
206         this.isGameStart = false;
207         $(".operating-panel a").removeClass("disable");
208         $("#replay_btn").html("开始");
209     },
210 
211     //下棋 i行,j列,color颜色
212     playChess: function (i, j, color) {
213         this.chessArr[i][j] = color === "black" ? this.BLACK_CHESS : this.WHITE_CHESS;
214         if (color === this.AIPlayer) {
215             $("div.chessboard div." + color + "-last").addClass(color).removeClass(color + "-last");
216             $("div.chessboard div:eq(" + (i * 15 + j) + ")").addClass(color + "-last");
217         }
218         else {
219             $("div.chessboard div:eq(" + (i * 15 + j) + ")").addClass(color);
220         }
221     },
222     //玩家是否取胜
223     playerWinOrNot: function (i, j) {
224         var nums = 1,    //连续棋子个数
225             chessColor = this.humanPlayer === "black" ? this.BLACK_CHESS : this.WHITE_CHESS,
226             m, n;
227         //x方向
228         for (m = j - 1; m >= 0; m--) {
229             if (this.chessArr[i][m] === chessColor) {
230                 nums++;
231             }
232             else {
233                 break;
234             }
235         }
236         for (m = j + 1; m < 15; m++) {
237             if (this.chessArr[i][m] === chessColor) {
238                 nums++;
239             }
240             else {
241                 break;
242             }
243         }
244         if (nums >= 5) {
245             this.playerWin();
246             return;
247         }
248         else {
249             nums = 1;
250         }
251         //y方向
252         for (m = i - 1; m >= 0; m--) {
253             if (this.chessArr[m][j] === chessColor) {
254                 nums++;
255             }
256             else {
257                 break;
258             }
259         }
260         for (m = i + 1; m < 15; m++) {
261             if (this.chessArr[m][j] === chessColor) {
262                 nums++;
263             }
264             else {
265                 break;
266             }
267         }
268         if (nums >= 5) {
269             this.playerWin();
270             return;
271         }
272         else {
273             nums = 1;
274         }
275         //左斜方向
276         for (m = i - 1, n = j - 1; m >= 0 && n >= 0; m--, n--) {
277             if (this.chessArr[m][n] === chessColor) {
278                 nums++;
279             }
280             else {
281                 break;
282             }
283         }
284         for (m = i + 1, n = j + 1; m < 15 && n < 15; m++, n++) {
285             if (this.chessArr[m][n] === chessColor) {
286                 nums++;
287             }
288             else {
289                 break;
290             }
291         }
292         if (nums >= 5) {
293             this.playerWin();
294             return;
295         }
296         else {
297             nums = 1;
298         }
299         //右斜方向
300         for (m = i - 1, n = j + 1; m >= 0 && n < 15; m--, n++) {
301             if (this.chessArr[m][n] === chessColor) {
302                 nums++;
303             }
304             else {
305                 break;
306             }
307         }
308         for (m = i + 1, n = j - 1; m < 15 && n >= 0; m++, n--) {
309             if (this.chessArr[m][n] === chessColor) {
310                 nums++;
311             }
312             else {
313                 break;
314             }
315         }
316         if (nums >= 5) {
317             this.playerWin();
318             return;
319         }
320         this.AImoveChess();
321     },
322     playerWin: function () {
323         this.winGames++;
324         cookieHandle.setCookie({ name: "winGames", value: this.winGames, expiresHours: 365 * 24 });
325         this.showResult(true);
326         this.gameOver();
327     },
328     //AI下棋
329     AImoveChess: function () {
330         this.isPlayerTurn = false;
331         var maxX = 0,
332             maxY = 0,
333             maxWeight = 0,
334             i, j, tem;
335         for (i = 14; i >= 0; i--) {
336             for (j = 14; j >= 0; j--) {
337                 if (this.chessArr[i][j] !== this.NO_CHESS) {
338                     continue;
339                 }
340                 tem = this.computeWeight(i, j);
341                 if (tem > maxWeight) {
342                     maxWeight = tem;
343                     maxX = i;
344                     maxY = j;
345                 }
346             }
347         }
348         this.playChess(maxX, maxY, this.AIPlayer);
349         this.AILastChess = [maxX, maxY];
350         if ((maxWeight >= 100000 && maxWeight < 250000) || (maxWeight >= 500000)) {//判断权重值
351             this.showResult(false);
352             this.gameOver();
353         }
354         else {
355             this.isPlayerTurn = true;
356         }
357     },
358     showResult: function(isPlayerWin) {
359         if (isPlayerWin) {
360             $("#result_tips").html("恭喜你,获胜了!");
361         }
362         else {
363             $("#result_tips").html("很可惜,你输了!");
364         }
365         this.isGameOver = true;
366         this.showWinChesses(isPlayerWin);
367     },
368     //标记显示获胜棋子
369     showWinChesses: function (isPlayerWin) {
370         var nums = 1,    //连续棋子个数
371             lineChess = [],    //连续棋子位置
372             i,
373             j,
374             chessColor,
375             m, n;
376         if (isPlayerWin) {
377             chessColor = this.humanPlayer === "black" ? this.BLACK_CHESS : this.WHITE_CHESS;
378             i = this.playerLastChess[0];
379             j = this.playerLastChess[1];
380         }
381         else {
382             chessColor = this.AIPlayer === "black" ? this.BLACK_CHESS : this.WHITE_CHESS;
383             i = this.AILastChess[0];
384             j = this.AILastChess[1];
385         }
386         $("div.chessboard div." + this.AIPlayer + "-last").addClass(this.AIPlayer).removeClass(this.AIPlayer + "-last");
387         //x方向
388         lineChess[0] = [i];//二维数组,代表几行几列
389         lineChess[1] = [j];
390         for (m = j - 1; m >= 0; m--) {
391             if (this.chessArr[i][m] === chessColor) {
392                 lineChess[0][nums] = i;
393                 lineChess[1][nums] = m;
394                 nums++;
395             }
396             else {
397                 break;
398             }
399         }
400         for (m = j + 1; m < 15; m++) {
401             if (this.chessArr[i][m] === chessColor) {
402                 lineChess[0][nums] = i;
403                 lineChess[1][nums] = m;
404                 nums++;
405             }
406             else {
407                 break;
408             }
409         }
410         if (nums >= 5) {
411             for (k = nums - 1; k >= 0; k--) {
412                 this.markChess(lineChess[0][k] * 15 + lineChess[1][k], isPlayerWin ? this.humanPlayer : this.AIPlayer);
413             }
414             return;
415         }
416         //y方向
417         nums = 1;
418         lineChess[0] = [i];
419         lineChess[1] = [j];
420         for (m = i - 1; m >= 0; m--) {
421             if (this.chessArr[m][j] === chessColor) {
422                 lineChess[0][nums] = m;
423                 lineChess[1][nums] = j;
424                 nums++;
425             }
426             else {
427                 break;
428             }
429         }
430         for (m = i + 1; m < 15; m++) {
431             if (this.chessArr[m][j] === chessColor) {
432                 lineChess[0][nums] = m;
433                 lineChess[1][nums] = j;
434                 nums++;
435             }
436             else {
437                 break;
438             }
439         }
440         if (nums >= 5) {
441             for (k = nums - 1; k >= 0; k--) {
442                 this.markChess(lineChess[0][k] * 15 + lineChess[1][k], isPlayerWin ? this.humanPlayer : this.AIPlayer);
443             }
444             return;
445         }
446         //左斜方向
447         nums = 1;
448         lineChess[0] = [i];
449         lineChess[1] = [j];
450         for (m = i - 1, n = j - 1; m >= 0 && n >= 0; m--, n--) {
451             if (this.chessArr[m][n] === chessColor) {
452                 lineChess[0][nums] = m;
453                 lineChess[1][nums] = n;
454                 nums++;
455             }
456             else {
457                 break;
458             }
459         }
460         for (m = i + 1, n = j + 1; m < 15 && n < 15; m++, n++) {
461             if (this.chessArr[m][n] === chessColor) {
462                 lineChess[0][nums] = m;
463                 lineChess[1][nums] = n;
464                 nums++;
465             }
466             else {
467                 break;
468             }
469         }
470         if (nums >= 5) {
471             for (k = nums - 1; k >= 0; k--) {
472                 this.markChess(lineChess[0][k] * 15 + lineChess[1][k], isPlayerWin ? this.humanPlayer : this.AIPlayer);
473             }
474             return;
475         }
476         //右斜方向
477         nums = 1;
478         lineChess[0] = [i];
479         lineChess[1] = [j];
480         for (m = i - 1, n = j + 1; m >= 0 && n < 15; m--, n++) {
481             if (this.chessArr[m][n] === chessColor) {
482                 lineChess[0][nums] = m;
483                 lineChess[1][nums] = n;
484                 nums++;
485             }
486             else {
487                 break;
488             }
489         }
490         for (m = i + 1, n = j - 1; m < 15 && n >= 0; m++, n--) {
491             if (this.chessArr[m][n] === chessColor) {
492                 lineChess[0][nums] = m;
493                 lineChess[1][nums] = n;
494                 nums++;
495             }
496             else {
497                 break;
498             }
499         }
500         if (nums >= 5) {
501             for (k = nums - 1; k >= 0; k--) {
502                 this.markChess(lineChess[0][k] * 15 + lineChess[1][k], isPlayerWin ? this.humanPlayer : this.AIPlayer);
503             }
504         }
505     },
506     markChess: function (pos, color) {
507         $("div.chessboard div:eq(" + pos + ")").removeClass(color).addClass(color + "-last");
508     },
509     //下子到i,j X方向 结果: 多少连子 两边是否截断
510     putDirectX: function (i, j, chessColor) {
511         var m, n,
512             nums = 1,
513             side1 = false,
514             side2 = false;
515         for (m = j - 1; m >= 0; m--) {
516             if (this.chessArr[i][m] === chessColor) {
517                 nums++;
518             }
519             else {
520                 if (this.chessArr[i][m] === this.NO_CHESS) {
521                     side1 = true;
522                 }
523                 break;
524             }
525         }
526         for (m = j + 1; m < 15; m++) {
527             if (this.chessArr[i][m] === chessColor) {
528                 nums++;
529             }
530             else {
531                 if (this.chessArr[i][m] === this.NO_CHESS) {
532                     side2 = true;
533                 }
534                 break;
535             }
536         }
537         return {"nums": nums, "side1": side1, "side2": side2};
538     },
539     //下子到i,j Y方向 结果
540     putDirectY: function (i, j, chessColor) {
541         var m, n,
542             nums = 1,
543             side1 = false,
544             side2 = false;
545         for (m = i - 1; m >= 0; m--) {
546             if (this.chessArr[m][j] === chessColor) {
547                 nums++;
548             }
549             else {
550                 if (this.chessArr[m][j] === this.NO_CHESS) {
551                     side1 = true;
552                 }
553                 break;
554             }
555         }
556         for (m = i + 1; m < 15; m++) {
557             if (this.chessArr[m][j] === chessColor) {
558                 nums++;
559             }
560             else {
561                 if (this.chessArr[m][j] === this.NO_CHESS) {
562                     side2 = true;
563                 }
564                 break;
565             }
566         }
567         return {"nums": nums, "side1": side1, "side2": side2};
568     },
569     //下子到i,j XY方向 结果
570     putDirectXY: function (i, j, chessColor) {
571         var m, n,
572             nums = 1,
573             side1 = false,
574             side2 = false;
575         for (m = i - 1, n = j - 1; m >= 0 && n >= 0; m--, n--) {
576             if (this.chessArr[m][n] === chessColor) {
577                 nums++;
578             }
579             else {
580                 if (this.chessArr[m][n] === this.NO_CHESS) {
581                     side1 = true;
582                 }
583                 break;
584             }
585         }
586         for (m = i + 1, n = j + 1; m < 15 && n < 15; m++, n++) {
587             if (this.chessArr[m][n] === chessColor) {
588                 nums++;
589             }
590             else {
591                 if (this.chessArr[m][n] === this.NO_CHESS) {
592                     side2 = true;
593                 }
594                 break;
595             }
596         }
597         return {"nums": nums, "side1": side1, "side2": side2};
598     },
599     putDirectYX: function (i, j, chessColor) {
600         var m, n,
601             nums = 1,
602             side1 = false,
603             side2 = false;
604         for (m = i - 1, n = j + 1; m >= 0 && n < 15; m--, n++) {
605             if (this.chessArr[m][n] === chessColor) {
606                 nums++;
607             }
608             else {
609                 if (this.chessArr[m][n] === this.NO_CHESS) {
610                     side1 = true;
611                 }
612                 break;
613             }
614         }
615         for (m = i + 1, n = j - 1; m < 15 && n >= 0; m++, n--) {
616             if (this.chessArr[m][n] === chessColor) {
617                 nums++;
618             }
619             else {
620                 if (this.chessArr[m][n] === this.NO_CHESS) {
621                     side2 = true;
622                 }
623                 break;
624             }
625         }
626         return {"nums": nums, "side1": side1, "side2": side2};
627     },
628     //计算下子至i,j的权重
629     computeWeight: function (i, j) {
630         var weight = 14 - (Math.abs(i - 7) + Math.abs(j - 7)), //基于棋盘位置权重
631             pointInfo = {},    //某点下子后连子信息
632             chessColor = this.AIPlayer === "black" ? this.BLACK_CHESS : this.WHITE_CHESS;
633         //x方向
634         pointInfo = this.putDirectX(i, j, chessColor);
635         weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true);//AI下子权重
636         pointInfo = this.putDirectX(i, j, -chessColor);
637         weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false);//player下子权重
638         //y方向
639         pointInfo = this.putDirectY(i, j, chessColor);
640         weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true);//AI下子权重
641         pointInfo = this.putDirectY(i, j, -chessColor);
642         weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false);//player下子权重
643         //左斜方向
644         pointInfo = this.putDirectXY(i, j, chessColor);
645         weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true);//AI下子权重
646         pointInfo = this.putDirectXY(i, j, -chessColor);
647         weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false);//player下子权重
648         //右斜方向
649         pointInfo = this.putDirectYX(i, j, chessColor);
650         weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true);//AI下子权重
651         pointInfo = this.putDirectYX(i, j, -chessColor);
652         weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false);//player下子权重
653         return weight;
654     },
655     //权重方案   独:两边为空可下子,单:一边为空 定式库
656     weightStatus: function (nums, side1, side2, isAI) {
657         var weight = 0;
658            switch (nums) {
659                 case 1:
660                     if (side1 && side2) {//两个为真才成立
661                         weight = isAI ? 15 : 10;    //独一 三元运算符语法是 条件 ? 结果1 : 结果2;. 这里你把条件写在问号(?)的前面后面跟着用冒号(:)分隔的结果1和结果2。满足条件时结果1否则结果2
662                     }
663                     break;
664                 case 2:
665                     if (side1 && side2) {
666                         weight = isAI ? 100 : 50;    //独二
667                     }
668                     else if (side1 || side2) {//一个为真就成立
669                         weight = isAI ? 10 : 5;    //单二
670                     }
671                     break;
672                 case 3:
673                     if (side1 && side2) {
674                         weight = isAI ? 100 : 200;    //独三
675                     }
676                     else if (side1 || side2) {
677                         weight = isAI ? 30 : 20;    //单三
678                     }
679                     break;
680                 case 4:
681                     if (side1 && side2) {
682                         weight = isAI ? 1 : 2000;    //独四
683                     }
684                     else if (side1 || side2) {
685                         weight = isAI ? 400 : 100;    //单四
686                     }
687                     break;
688                 case 5:
689                     weight = isAI ? 100000 : 10000;    //
690                     break;
691                 default:
692                     weight = isAI ? 500000 : 250000;
693                     break;
694             }
695             return weight;
696     }
697 };
698 </script>

应该从什么地方入手调整,比如说现在要改成简单的

梵蒂冈大使的主页 梵蒂冈大使 | 初学一级 | 园豆:2
提问于:2015-08-27 10:57
< >
分享
最佳答案
0

假设难度为5级,,1级最低,5级最高,那么你现在写的代码的难度就是5,修改AImoveChess 方法就可以改变难度

AImoveChess  在这个方法中,你现在的maxWeight变成一个数组,存排序后最高的5个权重数据,ai用第一个数据下棋就是5级难度,用第二个数据就是4级难度。

收获园豆:100
刘宏玺 | 专家六级 |园豆:14004 | 2015-08-27 11:24

可有代码贴出来参考下

梵蒂冈大使 | 园豆:2 (初学一级) | 2015-08-27 12:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册