找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3633|回复: 1
打印 上一主题 下一主题
收起左侧

原创五子棋算法1.0

[复制链接]
跳转到指定楼层
楼主
ID:127496 发表于 2016-6-21 01:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
原创垃圾算法,两下子就能下赢的。
用法:在第一帧的场景外面处放置三个影片剪辑,分别命名为yn,black,white
其中yn里面放一个寛16高16的透明按钮,实例名称为x (小写)
在black里面放一个黑色的棋子16*16
white放一个相同大小的白棋子。
然后再在场景里画一个16*16的期盘,每个格子寛16.4
设定棋盘的_x=10,_y=10
把以下代码放在第一帧帧上,运行即可
  1. var chess:Array = new Array();
  2. var hh:Number = 0;
  3. var now:Boolean = true;
  4. var mi:Number = 0;
  5. var desi:Array = new Array();
  6. var desj:Array = new Array();
  7. var comi:Array = new Array();
  8. var comj:Array = new Array();
  9. now = null;
  10. var gong = 5;
  11. var fang = 10;
  12. right_menu = new ContextMenu();
  13. right_menu.hideBuiltInItems();
  14. right_menu.customItems.push(new ContextMenuItem("悔棋", huiqi, false));
  15. right_menu.customItems.push(new ContextMenuItem("状态", see, false));
  16. for (i=0; i<16; i++) {
  17. chess[i] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  18. for (j=0; j<16; j++) {
  19.   _root.yn.duplicateMovieClip("yn"+i+"_"+j, 9000+i*16+j, {_x:9+i*16.39, _y:9+j*16.35});
  20.   _root["yn"+i+"_"+j].x.onRelease = function() {
  21.    _root.pd();
  22.   };
  23. }
  24. }
  25. function win(who) {
  26. if (who == 1) {
  27.   _root.now = 3;
  28.   _root.xx = "您打败了对手!共用"+hh+"回合";
  29. } else if (who == 2) {
  30.   _root.now = 3;
  31.   _root.xx = "您输了!";
  32. }
  33. }
  34. function ifwin() {
  35. for (i=0; i<16; i++) {
  36.   for (j=0; j<16; j++) {
  37.    for (k=0; k<3; k++) {
  38.     if (chess[i][j] == k) {
  39.      for (c=0; c<5; c++) {
  40.       if (chess[i+c][j] == k) {
  41.        if (c == 4) {
  42.         _root.win(k);
  43.        }
  44.       } else {
  45.        break;
  46.       }
  47.      }
  48.      for (c=0; c<5; c++) {
  49.       if (chess[i+c][j-c] == k) {
  50.        if (c == 4) {
  51.         _root.win(k);
  52.        }
  53.       } else {
  54.        break;
  55.       }
  56.      }
  57.      for (c=0; c<5; c++) {
  58.       if (chess[i][j+c] == k) {
  59.        if (c == 4) {
  60.         _root.win(k);
  61.        }
  62.       } else {
  63.        break;
  64.       }
  65.      }
  66.      for (c=0; c<5; c++) {
  67.       if (chess[i+c][j+c] == k) {
  68.        if (c == 4) {
  69.         _root.win(k);
  70.        }
  71.       } else {
  72.        break;
  73.       }
  74.      }
  75.     }
  76.    }
  77.   }
  78. }
  79. }
  80. function cd(a, b) {
  81. if (_root.hh == 0) {
  82.   a = 8;
  83.   b = 8;
  84. }
  85. _root.comi[hh] = a;
  86. _root.comj[hh] = b;
  87. _root.chess[a][b] = 2;
  88. _root.black.duplicateMovieClip("black_"+a+"_"+b, mi-1000, {_x:10+b*16.41, _y:10+a*16.4});
  89. _root.mi++;
  90. _root.now = false;
  91. _root.ifwin();
  92. trace("cd("+a+","+b+")");
  93. }
  94. function pd() {
  95. if (_root.now == false) {
  96.   x2 = _root._xmouse;
  97.   y2 = _root._ymouse;
  98.   a = int((y2-10)/16.41+0.5);
  99.   b = int((x2-10)/16.41+0.5);
  100.   trace([a, b]);
  101.   if (_root.chess[a][b] == 0) {
  102.    _root.chess[a][b] = 1;
  103.    _root.desi[hh] = a;
  104.    _root.desj[hh] = b;
  105.    _root.white.duplicateMovieClip("white_"+a+"_"+b, mi-1000, {_x:10+b*16.41, _y:10+a*16.4});
  106.    _root.mi++;
  107.    _root.hh++;
  108.    _root.now = true;
  109.   } else {
  110.    trace(_root.chess[a][b]);
  111.   }
  112. }
  113. _root.ifwin();
  114. }
  115. this.onEnterFrame = function() {
  116. _root.js = "<b>五子棋V2.0</b>\n谭泽睿\n    VS\n       "+_root.pn+"\n\n说明:算法是原创的,所以电脑智商不高……作者QQ754829466有意见欢迎说明";
  117. if (_root.now == true) {
  118.   var chesses:Array = chess;
  119.   var yes = 0;
  120.   yi = 5;
  121.   yj = 5;
  122.   for (i=0; i<11; i++) {
  123.    for (j=0; j<11; j++) {
  124.     var yes2 = 0;
  125.     if (chesses[i][j] == 0) {
  126.      chesses[i][j] = 2;
  127.      for (c=-1; c<5; c++) {
  128.       if (chesses[i+c][j] == 2) {
  129.        if (chesses[i-1][j] != 1 and chesses[i+c+2][j] != 1) {
  130.         yes2 += gong;
  131.        }
  132.       }
  133.       if (chesses[i-c][j] == 2) {
  134.        if (chesses[i+1][j] != 1 and chesses[i-c+2][j] != 1) {
  135.         yes2 += gong;
  136.        }
  137.       }
  138.       if (chesses[i][j+c] == 2) {
  139.        if (chesses[i][j-1] != 1 and chesses[i][j+c+2] != 1) {
  140.         yes2 += gong;
  141.        }
  142.       }
  143.       if (chesses[i][j-c] == 2) {
  144.        if (chesses[i][j+1] != 1 and chesses[i][j-c+2] != 1) {
  145.         yes2 += gong;
  146.        }
  147.       }
  148.       if (chesses[i+c][j+c] == 2) {
  149.        if (chesses[i-1][j-1] != 1 and chesses[i+c+2][j+c+2] != 1) {
  150.         yes2 += gong;
  151.        }
  152.       }
  153.       if (chesses[i-c][j+c] == 2) {
  154.        if (chesses[i+1][j-1] != 1 and chesses[i-c+2][j+c+2] != 1) {
  155.         yes2 += gong;
  156.        }
  157.       }
  158.       if (chesses[i+c][j-c] == 2) {
  159.        if (chesses[i-1][j+1] != 1 and chesses[i-c+2][j+c+2] != 1) {
  160.         yes2 += gong;
  161.        }
  162.       }
  163.       if (chesses[i-c][j-c] == 2) {
  164.        if (chesses[i+1][j+1] != 1 and chesses[i+c+2][j-c+2] != 1) {
  165.         yes2 += gong;
  166.        }
  167.       }
  168.       for (k=0; k<3; k++) {
  169.        if (chesses[i][j] == 2) {
  170.         for (r=0; r<5; r++) {
  171.          if (chesses[i+r][j] == k) {
  172.           if (r == 4) {
  173.            yes2 += 100;
  174.           }
  175.          } else {
  176.           break;
  177.          }
  178.         }
  179.         for (r=0; r<5; r++) {
  180.          if (chesses[i-r][j] == k) {
  181.           if (r == 4) {
  182.            yes2 += 100;
  183.           }
  184.          } else {
  185.           break;
  186.          }
  187.         }
  188.         for (r=0; r<5; r++) {
  189.          if (chesses[i-r][j-r] == k) {
  190.           if (r == 4) {
  191.            yes2 += 100;
  192.           }
  193.          } else {
  194.           break;
  195.          }
  196.         }
  197.         for (r=0; r<5; r++) {
  198.          if (chesses[i][j-r] == k) {
  199.           if (r == 4) {
  200.            yes2 += 100;
  201.           }
  202.          } else {
  203.           break;
  204.          }
  205.         }
  206.         for (r=0; r<5; r++) {
  207.          if (chesses[i-r][j+r] == k) {
  208.           if (r == 4) {
  209.            yes2 += 100;
  210.           }
  211.          } else {
  212.           break;
  213.          }
  214.         }
  215.         for (r=0; r<5; r++) {
  216.          if (chesses[i][j+r] == k) {
  217.           if (r == 4) {
  218.            yes2 += 100;
  219.           }
  220.          } else {
  221.           break;
  222.          }
  223.         }
  224.         for (r=0; r<5; r++) {
  225.          if (chesses[i+r][j+r] == k) {
  226.           if (r == 4) {
  227.            yes2 += 100;
  228.           }
  229.          } else {
  230.           break;
  231.          }
  232.         }
  233.        }
  234.       }
  235.      }
  236.      for (c=1; c<5; c++) {
  237.       if (chesses[i+c][j] == 1) {
  238.        yes2 += fang;
  239.       }
  240.       if (chesses[i-c][j] == 1) {
  241.        yes2 += fang;
  242.       }
  243.       if (chesses[i][j+c] == 1) {
  244.        yes2 += fang;
  245.       }
  246.       if (chesses[i][j-c] == 1) {
  247.        yes2 += fang;
  248.       }
  249.       if (chesses[i+c][j+c] == 1) {
  250.        yes2 += fang;
  251.       }
  252.       if (chesses[i-c][j+c] == 1) {
  253.        yes2 += fang;
  254.       }
  255.       if (chesses[i+c][j-c] == 1) {
  256.        yes2 += fang;
  257.       }
  258.       if (chesses[i-c][j-c] == 1) {
  259.        yes2 += fang;
  260.       }
  261.      }
  262.      for (r=1; r<5; r++) {
  263.       if (chesses[i+r][j] == 1) {
  264.        if (r == 3) {
  265.         yes2 += 5*fang;
  266.        } else if (r == 4) {
  267.         yes2 += fang*10;
  268.        }
  269.       } else {
  270.        break;
  271.       }
  272.      }
  273.      for (r=1; r<5; r++) {
  274.       if (chesses[i+r][j+r] == 1) {
  275.        if (r == 3) {
  276.         yes2 += 5*fang;
  277.        } else if (r == 4) {
  278.         yes2 += fang*10;
  279.        }
  280.       } else {
  281.        break;
  282.       }
  283.      }
  284.      for (r=1; r<5; r++) {
  285.       if (chesses[i+r][j-r] == 1) {
  286.        if (r == 3) {
  287.         yes2 += 5*fang;
  288.        } else if (r == 4) {
  289.         yes2 += fang*10;
  290.        }
  291.       } else {
  292.        break;
  293.       }
  294.      }
  295.      for (r=1; r<5; r++) {
  296.       if (chesses[i-r][j] == 1) {
  297.        if (r == 3) {
  298.         yes2 += 5*fang;
  299.        } else if (r == 4) {
  300.         yes2 += fang*10;
  301.        }
  302.       } else {
  303.        break;
  304.       }
  305.      }
  306.      for (r=1; r<5; r++) {
  307.       if (chesses[i-r][j+r] == 1) {
  308.        if (r == 3) {
  309.         yes2 += 5*fang;
  310.        } else if (r == 4) {
  311.         yes2 += fang*10;
  312.        }
  313.       } else {
  314.        break;
  315.       }
  316.      }
  317.      for (r=1; r<5; r++) {
  318.       if (chesses[i-r][j-r] == 1) {
  319.        if (r == 3) {
  320.         yes2 += 5*fang;
  321.        } else if (r == 4) {
  322.         yes2 += fang*10;
  323.        }
  324.       } else {
  325.        break;
  326.       }
  327.      }
  328.      for (r=1; r<5; r++) {
  329.       if (chesses[i][j+r] == 1) {
  330.        if (r == 3) {
  331.         yes2 += 5*fang;
  332.        } else if (r == 4) {
  333.         yes2 += fang*10;
  334.        }
  335.       } else {
  336.        break;
  337.       }
  338.      }
  339.      for (r=1; r<5; r++) {
  340.       if (chesses[i][j-r] == 1) {
  341.        if (r == 3) {
  342.         yes2 += 5*fang;
  343.        } else if (r == 4) {
  344.         yes2 += fang*10;
  345.        }
  346.       } else {
  347.        break;
  348.       }
  349.      }
  350.      chesses[i][j] = 0;
  351.      if (yes2>yes) {
  352.       yes = yes2;
  353.       yi = i;
  354.       yj = j;
  355.      }
  356.     }
  357.    }
  358.   }
  359.   _root.cd(yi, yj);
  360. }
  361. };
  362. function huiqi() {
  363. if (_root.hh>=1) {
  364.   trace("悔棋");
  365.   ci = _root.comi[_root.hh];
  366.   cj = _root.comj[_root.hh];
  367.   di = _root.desi[_root.hh];
  368.   dj = _root.desj[_root.hh];
  369.   _root.chess[ci][cj] = 0;
  370.   _root.comi[_root.hh] = null;
  371.   _root.comj[_root.hh] = null;
  372.   _root["black_"+ci+"_"+cj].removeMovieClip();
  373.   _root.chess[di][dj] = 0;
  374.   _root.desi[_root.hh] = null;
  375.   _root.desj[_root.hh] = null;
  376.   _root["white_"+di+"_"+dj].removeMovieClip();
  377.   _root.hh--;
  378.   _root.mi--;
  379.   _root.now = false;
  380. }
  381. }
  382. function see() {
  383. for (si=0; si<_root.hh; si++) {
  384.   trace([comi[si], comj[si]]+"|"+[desi[si], desj[si]]);
  385. }
  386. trace("第"+hh+"回合,当前棋局:");
  387. for (i=0; i<16; i++) {
  388.   trace(_root.chess[i]);
  389. }
  390. }
  391. _root.menu = right_menu;
复制代码

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:311080 发表于 2018-5-17 16:35 | 只看该作者
您好,这个是个是给单片机里面写的程序码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表