首页新闻找找看学习计划

求解,怎么提高AI的智商,这个程序太弱智了。

0
悬赏园豆:20 [已关闭问题] 关闭于 2015-09-06 19:38
  1 <script>
  2 $(document).ready(function() {
  3     GoBang.init();
  4 });
  5 
  6 var GoBang = {
  7     No_Chess: 0,
  8     Black_Chess: -1,
  9     White_Chess: 1,
 10     chessArr: [],    //记录棋子
 11     chessBoardHtml: "",
 12     GamePlayer: "black",    //玩家棋子颜色
 13     ComputerPlayer: "white",    //电脑棋子颜色
 14     isPlayerTurn: true, //轮到玩家下棋
 15     isGameStart: false,    //游戏已经开始
 16     isGameOver: false, //游戏结束
 17     playerLastChess: [], //玩家最后下子位置
 18     ComputerLastChess: [], //电脑最后下子位置
 19 
 20     init: function () {
 21         this.chessBoardHtml = $("div.GoBangBoard").html();
 22         var _GoBang = this;
 23         //右侧操作按钮
 24         $(".operating-panel a").click(function (event) {
 25             event.preventDefault();
 26             var id = $(this).attr("id");
 27             if (_GoBang.isGameStart && id !== "replay_btn" ) { return; }    //正在游戏 不操作
 28             switch (id) {
 29                 case "black_btn":
 30                     _GoBang.GamePlayer = "black";
 31                     _GoBang.ComputerPlayer = "white";
 32                     break;
 33                 case "white_btn":
 34                     _GoBang.GamePlayer = "white";
 35                     _GoBang.ComputerPlayer = "black";
 36                     break;
 37                 case "first_move_btn":
 38                     _GoBang.isPlayerTurn = true;
 39                     break;
 40                 case "second_move_btn":
 41                     _GoBang.isPlayerTurn = false;
 42                     break;
 43                 case "replay_btn":
 44                     _GoBang.resetChessBoard();
 45                     if (_GoBang.isGameStart) {    //点重玩
 46                         _GoBang.gameOver();
 47                     }
 48                     else {    //点开始
 49                         _GoBang.gameStart();
 50                     }
 51                     break;
 52             }
 53             if (id !== "replay_btn") {
 54                 $(this).addClass("selected").siblings().removeClass("selected");
 55             }
 56         });
 57     },
 58     //重置棋盘
 59     resetChessBoard: function () {
 60         $("div.GoBangBoard").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 _GoBang = this;
 74         $("div.GoBangBoard div").click(function () {
 75             if (!_GoBang.isPlayerTurn || _GoBang.isGameOver) {
 76                 return;
 77             }
 78             if (!_GoBang.isGameStart) {
 79                 _GoBang.gameStart();
 80             }
 81             var index = $(this).index(),
 82                 i = index / 15 | 0,
 83                 j = index % 15;
 84             if (_GoBang.chessArr[i][j] === _GoBang.No_Chess) {
 85                 _GoBang.playChess(i, j, _GoBang.GamePlayer);
 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                 _GoBang.playerLastChess = [i, j];
114                 _GoBang.playerWinOrNot(i, j);
115             }
116         });
117     },
118     gameStart: function () {
119         //电脑先手
120         if (!this.isPlayerTurn) {
121             this.ComputermoveChess();
122         }
123         this.isGameStart = true;
124         $(".operating-panel p a").addClass("disable");
125         $("#replay_btn").html("重&nbsp;&nbsp;&nbsp;玩");
126     },
127     gameOver: function () {
128         this.isGameStart = false;
129         $(".operating-panel a").removeClass("disable");
130         $("#replay_btn").html("开&nbsp;&nbsp;&nbsp;始");
131     },
132 
133     //下棋 i行,j列,color颜色
134     playChess: function (i, j, color) {
135         this.chessArr[i][j] = color === "black" ? this.Black_Chess : this.White_Chess;
136         if (color === this.ComputerPlayer) {
137             $("div.GoBangBoard div." + color + "-last").addClass(color).removeClass(color + "-last");
138             $("div.GoBangBoard div:eq(" + (i * 15 + j) + ")").addClass(color + "-last");
139         }
140         else {
141             $("div.GoBangBoard div:eq(" + (i * 15 + j) + ")").addClass(color);
142         }
143     },
144     //玩家是否取胜
145     playerWinOrNot: function (i, j) {
146         var nums = 1,    //连续棋子个数
147             chessColor = this.GamePlayer === "black" ? this.Black_Chess : this.White_Chess,
148             m, n;
149         //x方向
150         for (m = j - 1; m >= 0; m--) {
151             if (this.chessArr[i][m] === chessColor) {
152                 nums++;
153             }
154             else {
155                 break;
156             }
157         }
158         for (m = j + 1; m < 15; m++) {
159             if (this.chessArr[i][m] === chessColor) {
160                 nums++;
161             }
162             else {
163                 break;
164             }
165         }
166         if (nums >= 5) {
167             this.playerWin();
168             return;
169         }
170         else {
171             nums = 1;
172         }
173         //y方向
174         for (m = i - 1; m >= 0; m--) {
175             if (this.chessArr[m][j] === chessColor) {
176                 nums++;
177             }
178             else {
179                 break;
180             }
181         }
182         for (m = i + 1; m < 15; m++) {
183             if (this.chessArr[m][j] === chessColor) {
184                 nums++;
185             }
186             else {
187                 break;
188             }
189         }
190         if (nums >= 5) {
191             this.playerWin();
192             return;
193         }
194         else {
195             nums = 1;
196         }
197         //左斜方向
198         for (m = i - 1, n = j - 1; m >= 0 && n >= 0; m--, n--) {
199             if (this.chessArr[m][n] === chessColor) {
200                 nums++;
201             }
202             else {
203                 break;
204             }
205         }
206         for (m = i + 1, n = j + 1; m < 15 && n < 15; m++, n++) {
207             if (this.chessArr[m][n] === chessColor) {
208                 nums++;
209             }
210             else {
211                 break;
212             }
213         }
214         if (nums >= 5) {
215             this.playerWin();
216             return;
217         }
218         else {
219             nums = 1;
220         }
221         //右斜方向
222         for (m = i - 1, n = j + 1; m >= 0 && n < 15; m--, n++) {
223             if (this.chessArr[m][n] === chessColor) {
224                 nums++;
225             }
226             else {
227                 break;
228             }
229         }
230         for (m = i + 1, n = j - 1; m < 15 && n >= 0; m++, n--) {
231             if (this.chessArr[m][n] === chessColor) {
232                 nums++;
233             }
234             else {
235                 break;
236             }
237         }
238         if (nums >= 5) {
239             this.playerWin();
240             return;
241         }
242         this.ComputermoveChess();
243     },
244     //电脑下棋
245     ComputermoveChess: function () {
246         this.isPlayerTurn = false;
247         var maxX = 0,
248             maxY = 0,
249             maxWeight = 0,
250             i, j, tem;
251         for (i = 14; i >= 0; i--) {
252             for (j = 14; j >= 0; j--) {
253                 if (this.chessArr[i][j] !== this.No_Chess) {
254                     continue;
255                 }
256                 tem = this.computeWeight(i, j);
257                 if (tem > maxWeight) {
258                     maxWeight = tem;
259                     maxX = i;
260                     maxY = j;
261                 }
262             }
263         }
264         this.playChess(maxX, maxY, this.ComputerPlayer);
265         this.ComputerLastChess = [maxX, maxY];
266         if ((maxWeight >= 100000 && maxWeight < 250000) || (maxWeight >= 500000)) {
267             this.showResult(false);
268             this.gameOver();
269         }
270         else {
271             this.isPlayerTurn = true;
272         }
273     },
274     showResult: function(isPlayerWin) {
275         if (isPlayerWin) {
276             $("#result_tips").html("GameWin!");
277         }
278         else {
279             $("#result_tips").html("GameOver!");
280         }
281         this.isGameOver = true;
282         this.showWinChesses(isPlayerWin);
283     },
284     //标记显示获胜棋子
285     showWinChesses: function (isPlayerWin) {
286         var nums = 1,    //连续棋子个数
287             lineChess = [],    //连续棋子位置
288             i,
289             j,
290             chessColor,
291             m, n;
292         if (isPlayerWin) {
293             chessColor = this.GamePlayer === "black" ? this.Black_Chess : this.White_Chess;
294             i = this.playerLastChess[0];
295             j = this.playerLastChess[1];
296         }
297         else {
298             chessColor = this.ComputerPlayer === "black" ? this.Black_Chess : this.White_Chess;
299             i = this.ComputerLastChess[0];
300             j = this.ComputerLastChess[1];
301         }
302         $("div.GoBangBoard div." + this.ComputerPlayer + "-last").addClass(this.ComputerPlayer).removeClass(this.ComputerPlayer + "-last");
303         //x方向
304         lineChess[0] = [i];
305         lineChess[1] = [j];
306         for (m = j - 1; m >= 0; m--) {
307             if (this.chessArr[i][m] === chessColor) {
308                 lineChess[0][nums] = i;
309                 lineChess[1][nums] = m;
310                 nums++;
311             }
312             else {
313                 break;
314             }
315         }
316         for (m = j + 1; m < 15; m++) {
317             if (this.chessArr[i][m] === chessColor) {
318                 lineChess[0][nums] = i;
319                 lineChess[1][nums] = m;
320                 nums++;
321             }
322             else {
323                 break;
324             }
325         }
326         if (nums >= 5) {
327             for (k = nums - 1; k >= 0; k--) {
328                 this.markChess(lineChess[0][k] * 15 + lineChess[1][k], isPlayerWin ? this.GamePlayer : this.ComputerPlayer);
329             }
330             return;
331         }
332         //y方向
333         nums = 1;
334         lineChess[0] = [i];
335         lineChess[1] = [j];
336         for (m = i - 1; m >= 0; m--) {
337             if (this.chessArr[m][j] === chessColor) {
338                 lineChess[0][nums] = m;
339                 lineChess[1][nums] = j;
340                 nums++;
341             }
342             else {
343                 break;
344             }
345         }
346         for (m = i + 1; m < 15; m++) {
347             if (this.chessArr[m][j] === chessColor) {
348                 lineChess[0][nums] = m;
349                 lineChess[1][nums] = j;
350                 nums++;
351             }
352             else {
353                 break;
354             }
355         }
356         if (nums >= 5) {
357             for (k = nums - 1; k >= 0; k--) {
358                 this.markChess(lineChess[0][k] * 15 + lineChess[1][k], isPlayerWin ? this.GamePlayer : this.ComputerPlayer);
359             }
360             return;
361         }
362         //左斜方向
363         nums = 1;
364         lineChess[0] = [i];
365         lineChess[1] = [j];
366         for (m = i - 1, n = j - 1; m >= 0 && n >= 0; m--, n--) {
367             if (this.chessArr[m][n] === chessColor) {
368                 lineChess[0][nums] = m;
369                 lineChess[1][nums] = n;
370                 nums++;
371             }
372             else {
373                 break;
374             }
375         }
376         for (m = i + 1, n = j + 1; m < 15 && n < 15; m++, n++) {
377             if (this.chessArr[m][n] === chessColor) {
378                 lineChess[0][nums] = m;
379                 lineChess[1][nums] = n;
380                 nums++;
381             }
382             else {
383                 break;
384             }
385         }
386         if (nums >= 5) {
387             for (k = nums - 1; k >= 0; k--) {
388                 this.markChess(lineChess[0][k] * 15 + lineChess[1][k], isPlayerWin ? this.GamePlayer : this.ComputerPlayer);
389             }
390             return;
391         }
392         //右斜方向
393         nums = 1;
394         lineChess[0] = [i];
395         lineChess[1] = [j];
396         for (m = i - 1, n = j + 1; m >= 0 && n < 15; m--, n++) {
397             if (this.chessArr[m][n] === chessColor) {
398                 lineChess[0][nums] = m;
399                 lineChess[1][nums] = n;
400                 nums++;
401             }
402             else {
403                 break;
404             }
405         }
406         for (m = i + 1, n = j - 1; m < 15 && n >= 0; m++, n--) {
407             if (this.chessArr[m][n] === chessColor) {
408                 lineChess[0][nums] = m;
409                 lineChess[1][nums] = n;
410                 nums++;
411             }
412             else {
413                 break;
414             }
415         }
416         if (nums >= 5) {
417             for (k = nums - 1; k >= 0; k--) {
418                 this.markChess(lineChess[0][k] * 15 + lineChess[1][k], isPlayerWin ? this.GamePlayer : this.ComputerPlayer);
419             }
420         }
421     },
422     markChess: function (pos, color) {
423         $("div.GoBangBoard div:eq(" + pos + ")").removeClass(color).addClass(color + "-last");
424     },
425     //下子到i,j X方向 结果: 多少连子 两边是否截断
426     putDirectX: function (i, j, chessColor) {
427         var m, n,
428             nums = 1,
429             side1 = false,
430             side2 = false;
431         for (m = j - 1; m >= 0; m--) {
432             if (this.chessArr[i][m] === chessColor) {
433                 nums++;
434             }
435             else {
436                 if (this.chessArr[i][m] === this.No_Chess) {
437                     side1 = true;
438                 }
439                 break;
440             }
441         }
442         for (m = j + 1; m < 15; m++) {
443             if (this.chessArr[i][m] === chessColor) {
444                 nums++;
445             }
446             else {
447                 if (this.chessArr[i][m] === this.No_Chess) {
448                     side2 = true;
449                 }
450                 break;
451             }
452         }
453         return {"nums": nums, "side1": side1, "side2": side2};
454     },
455     //下子到i,j Y方向 结果
456     putDirectY: function (i, j, chessColor) {
457         var m, n,
458             nums = 1,
459             side1 = false,
460             side2 = false;
461         for (m = i - 1; m >= 0; m--) {
462             if (this.chessArr[m][j] === chessColor) {
463                 nums++;
464             }
465             else {
466                 if (this.chessArr[m][j] === this.No_Chess) {
467                     side1 = true;
468                 }
469                 break;
470             }
471         }
472         for (m = i + 1; m < 15; m++) {
473             if (this.chessArr[m][j] === chessColor) {
474                 nums++;
475             }
476             else {
477                 if (this.chessArr[m][j] === this.No_Chess) {
478                     side2 = true;
479                 }
480                 break;
481             }
482         }
483         return {"nums": nums, "side1": side1, "side2": side2};
484     },
485     //下子到i,j XY方向 结果
486     putDirectXY: function (i, j, chessColor) {
487         var m, n,
488             nums = 1,
489             side1 = false,
490             side2 = false;
491         for (m = i - 1, n = j - 1; m >= 0 && n >= 0; m--, n--) {
492             if (this.chessArr[m][n] === chessColor) {
493                 nums++;
494             }
495             else {
496                 if (this.chessArr[m][n] === this.No_Chess) {
497                     side1 = true;
498                 }
499                 break;
500             }
501         }
502         for (m = i + 1, n = j + 1; m < 15 && n < 15; m++, n++) {
503             if (this.chessArr[m][n] === chessColor) {
504                 nums++;
505             }
506             else {
507                 if (this.chessArr[m][n] === this.No_Chess) {
508                     side2 = true;
509                 }
510                 break;
511             }
512         }
513         return {"nums": nums, "side1": side1, "side2": side2};
514     },
515     putDirectYX: function (i, j, chessColor) {
516         var m, n,
517             nums = 1,
518             side1 = false,
519             side2 = false;
520         for (m = i - 1, n = j + 1; m >= 0 && n < 15; m--, n++) {
521             if (this.chessArr[m][n] === chessColor) {
522                 nums++;
523             }
524             else {
525                 if (this.chessArr[m][n] === this.No_Chess) {
526                     side1 = true;
527                 }
528                 break;
529             }
530         }
531         for (m = i + 1, n = j - 1; m < 15 && n >= 0; m++, n--) {
532             if (this.chessArr[m][n] === chessColor) {
533                 nums++;
534             }
535             else {
536                 if (this.chessArr[m][n] === this.No_Chess) {
537                     side2 = true;
538                 }
539                 break;
540             }
541         }
542         return {"nums": nums, "side1": side1, "side2": side2};
543     },
544     //计算下子至i,j的权重
545     computeWeight: function (i, j) {
546         var weight = 14 - (Math.abs(i - 7) + Math.abs(j - 7)), //基于棋盘位置权重
547             pointInfo = {},    //某点下子后连子信息
548             chessColor = this.ComputerPlayer === "black" ? this.Black_Chess : this.White_Chess;
549         //x方向
550         pointInfo = this.putDirectX(i, j, chessColor);
551         weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true);//电脑下子权重
552         pointInfo = this.putDirectX(i, j, -chessColor);
553         weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false);//player下子权重
554         //y方向
555         pointInfo = this.putDirectY(i, j, chessColor);
556         weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true);//电脑下子权重
557         pointInfo = this.putDirectY(i, j, -chessColor);
558         weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false);//player下子权重
559         //左斜方向
560         pointInfo = this.putDirectXY(i, j, chessColor);
561         weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true);//电脑下子权重
562         pointInfo = this.putDirectXY(i, j, -chessColor);
563         weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false);//player下子权重
564         //右斜方向
565         pointInfo = this.putDirectYX(i, j, chessColor);
566         weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true);//电脑下子权重
567         pointInfo = this.putDirectYX(i, j, -chessColor);
568         weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false);//player下子权重
569         return weight;
570     },
571     //权重方案   独:两边为空可下子,单:一边为空
572     weightStatus: function (nums, side1, side2, isComputer) {
573         var weight = 0;
574         switch (nums) {
575             case 1:
576                 if (side1 && side2) {
577                     weight = isComputer ? 15 : 10;    //独一
578                 }
579                 break;
580             case 2:
581                 if (side1 && side2) {
582                     weight = isComputer ? 100 : 50;    //独二
583                 }
584                 else if (side1 || side2) {
585                     weight = isComputer ? 10 : 5;    //单二
586                 }
587                 break;
588             case 3:
589                 if (side1 && side2) {
590                     weight = isComputer ? 500 : 200;    //独三
591                 }
592                 else if (side1 || side2) {
593                     weight = isComputer ? 30 : 20;    //单三
594                 }
595                 break;
596             case 4:
597                 if (side1 && side2) {
598                     weight = isComputer ? 5000 : 2000;    //独四
599                 }
600                 else if (side1 || side2) {
601                     weight = isComputer ? 400 : 100;    //单四
602                 }
603                 break;
604             case 5:
605                 weight = isComputer ? 100000 : 10000;    //
606                 break;
607             default:
608                 weight = isComputer ? 500000 : 250000;
609                 break;
610         }
611         return weight;
612     }
613 };
614 </script>

已知的是不能百分百赢

梵蒂冈大使的主页 梵蒂冈大使 | 初学一级 | 园豆:2
提问于:2015-09-03 15:28
< >
分享
所有回答(2)
0

算法还是慢慢来吧,需要积累和牛逼的数学知识的

稳稳的河 | 园豆:4183 (老鸟四级) | 2015-09-03 16:32
1

AI的智商是由写这个AI的人的智商决定的。。。

XiaoFaye | 园豆:3082 (老鸟四级) | 2015-09-04 05:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册