原創(chuàng)垃圾算法,兩下子就能下贏的。
用法:在第一幀的場景外面處放置三個影片剪輯,分別命名為yn,black,white
其中yn里面放一個寛16高16的透明按鈕,實例名稱為x (小寫)
在black里面放一個黑色的棋子16*16
white放一個相同大小的白棋子。
然后再在場景里畫一個16*16的期盤,每個格子寛16.4
設定棋盤的_x=10,_y=10
把以下代碼放在第一幀幀上,運行即可
- var chess:Array = new Array();
- var hh:Number = 0;
- var now:Boolean = true;
- var mi:Number = 0;
- var desi:Array = new Array();
- var desj:Array = new Array();
- var comi:Array = new Array();
- var comj:Array = new Array();
- now = null;
- var gong = 5;
- var fang = 10;
- right_menu = new ContextMenu();
- right_menu.hideBuiltInItems();
- right_menu.customItems.push(new ContextMenuItem("悔棋", huiqi, false));
- right_menu.customItems.push(new ContextMenuItem("狀態(tài)", see, false));
- for (i=0; i<16; i++) {
- chess[i] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
- for (j=0; j<16; j++) {
- _root.yn.duplicateMovieClip("yn"+i+"_"+j, 9000+i*16+j, {_x:9+i*16.39, _y:9+j*16.35});
- _root["yn"+i+"_"+j].x.onRelease = function() {
- _root.pd();
- };
- }
- }
- function win(who) {
- if (who == 1) {
- _root.now = 3;
- _root.xx = "您打敗了對手!共用"+hh+"回合";
- } else if (who == 2) {
- _root.now = 3;
- _root.xx = "您輸了!";
- }
- }
- function ifwin() {
- for (i=0; i<16; i++) {
- for (j=0; j<16; j++) {
- for (k=0; k<3; k++) {
- if (chess[i][j] == k) {
- for (c=0; c<5; c++) {
- if (chess[i+c][j] == k) {
- if (c == 4) {
- _root.win(k);
- }
- } else {
- break;
- }
- }
- for (c=0; c<5; c++) {
- if (chess[i+c][j-c] == k) {
- if (c == 4) {
- _root.win(k);
- }
- } else {
- break;
- }
- }
- for (c=0; c<5; c++) {
- if (chess[i][j+c] == k) {
- if (c == 4) {
- _root.win(k);
- }
- } else {
- break;
- }
- }
- for (c=0; c<5; c++) {
- if (chess[i+c][j+c] == k) {
- if (c == 4) {
- _root.win(k);
- }
- } else {
- break;
- }
- }
- }
- }
- }
- }
- }
- function cd(a, b) {
- if (_root.hh == 0) {
- a = 8;
- b = 8;
- }
- _root.comi[hh] = a;
- _root.comj[hh] = b;
- _root.chess[a][b] = 2;
- _root.black.duplicateMovieClip("black_"+a+"_"+b, mi-1000, {_x:10+b*16.41, _y:10+a*16.4});
- _root.mi++;
- _root.now = false;
- _root.ifwin();
- trace("cd("+a+","+b+")");
- }
- function pd() {
- if (_root.now == false) {
- x2 = _root._xmouse;
- y2 = _root._ymouse;
- a = int((y2-10)/16.41+0.5);
- b = int((x2-10)/16.41+0.5);
- trace([a, b]);
- if (_root.chess[a][b] == 0) {
- _root.chess[a][b] = 1;
- _root.desi[hh] = a;
- _root.desj[hh] = b;
- _root.white.duplicateMovieClip("white_"+a+"_"+b, mi-1000, {_x:10+b*16.41, _y:10+a*16.4});
- _root.mi++;
- _root.hh++;
- _root.now = true;
- } else {
- trace(_root.chess[a][b]);
- }
- }
- _root.ifwin();
- }
- this.onEnterFrame = function() {
- _root.js = "<b>五子棋V2.0</b>\n譚澤睿\n VS\n "+_root.pn+"\n\n說明:算法是原創(chuàng)的,所以電腦智商不高……作者QQ754829466有意見歡迎說明";
- if (_root.now == true) {
- var chesses:Array = chess;
- var yes = 0;
- yi = 5;
- yj = 5;
- for (i=0; i<11; i++) {
- for (j=0; j<11; j++) {
- var yes2 = 0;
- if (chesses[i][j] == 0) {
- chesses[i][j] = 2;
- for (c=-1; c<5; c++) {
- if (chesses[i+c][j] == 2) {
- if (chesses[i-1][j] != 1 and chesses[i+c+2][j] != 1) {
- yes2 += gong;
- }
- }
- if (chesses[i-c][j] == 2) {
- if (chesses[i+1][j] != 1 and chesses[i-c+2][j] != 1) {
- yes2 += gong;
- }
- }
- if (chesses[i][j+c] == 2) {
- if (chesses[i][j-1] != 1 and chesses[i][j+c+2] != 1) {
- yes2 += gong;
- }
- }
- if (chesses[i][j-c] == 2) {
- if (chesses[i][j+1] != 1 and chesses[i][j-c+2] != 1) {
- yes2 += gong;
- }
- }
- if (chesses[i+c][j+c] == 2) {
- if (chesses[i-1][j-1] != 1 and chesses[i+c+2][j+c+2] != 1) {
- yes2 += gong;
- }
- }
- if (chesses[i-c][j+c] == 2) {
- if (chesses[i+1][j-1] != 1 and chesses[i-c+2][j+c+2] != 1) {
- yes2 += gong;
- }
- }
- if (chesses[i+c][j-c] == 2) {
- if (chesses[i-1][j+1] != 1 and chesses[i-c+2][j+c+2] != 1) {
- yes2 += gong;
- }
- }
- if (chesses[i-c][j-c] == 2) {
- if (chesses[i+1][j+1] != 1 and chesses[i+c+2][j-c+2] != 1) {
- yes2 += gong;
- }
- }
- for (k=0; k<3; k++) {
- if (chesses[i][j] == 2) {
- for (r=0; r<5; r++) {
- if (chesses[i+r][j] == k) {
- if (r == 4) {
- yes2 += 100;
- }
- } else {
- break;
- }
- }
- for (r=0; r<5; r++) {
- if (chesses[i-r][j] == k) {
- if (r == 4) {
- yes2 += 100;
- }
- } else {
- break;
- }
- }
- for (r=0; r<5; r++) {
- if (chesses[i-r][j-r] == k) {
- if (r == 4) {
- yes2 += 100;
- }
- } else {
- break;
- }
- }
- for (r=0; r<5; r++) {
- if (chesses[i][j-r] == k) {
- if (r == 4) {
- yes2 += 100;
- }
- } else {
- break;
- }
- }
- for (r=0; r<5; r++) {
- if (chesses[i-r][j+r] == k) {
- if (r == 4) {
- yes2 += 100;
- }
- } else {
- break;
- }
- }
- for (r=0; r<5; r++) {
- if (chesses[i][j+r] == k) {
- if (r == 4) {
- yes2 += 100;
- }
- } else {
- break;
- }
- }
- for (r=0; r<5; r++) {
- if (chesses[i+r][j+r] == k) {
- if (r == 4) {
- yes2 += 100;
- }
- } else {
- break;
- }
- }
- }
- }
- }
- for (c=1; c<5; c++) {
- if (chesses[i+c][j] == 1) {
- yes2 += fang;
- }
- if (chesses[i-c][j] == 1) {
- yes2 += fang;
- }
- if (chesses[i][j+c] == 1) {
- yes2 += fang;
- }
- if (chesses[i][j-c] == 1) {
- yes2 += fang;
- }
- if (chesses[i+c][j+c] == 1) {
- yes2 += fang;
- }
- if (chesses[i-c][j+c] == 1) {
- yes2 += fang;
- }
- if (chesses[i+c][j-c] == 1) {
- yes2 += fang;
- }
- if (chesses[i-c][j-c] == 1) {
- yes2 += fang;
- }
- }
- for (r=1; r<5; r++) {
- if (chesses[i+r][j] == 1) {
- if (r == 3) {
- yes2 += 5*fang;
- } else if (r == 4) {
- yes2 += fang*10;
- }
- } else {
- break;
- }
- }
- for (r=1; r<5; r++) {
- if (chesses[i+r][j+r] == 1) {
- if (r == 3) {
- yes2 += 5*fang;
- } else if (r == 4) {
- yes2 += fang*10;
- }
- } else {
- break;
- }
- }
- for (r=1; r<5; r++) {
- if (chesses[i+r][j-r] == 1) {
- if (r == 3) {
- yes2 += 5*fang;
- } else if (r == 4) {
- yes2 += fang*10;
- }
- } else {
- break;
- }
- }
- for (r=1; r<5; r++) {
- if (chesses[i-r][j] == 1) {
- if (r == 3) {
- yes2 += 5*fang;
- } else if (r == 4) {
- yes2 += fang*10;
- }
- } else {
- break;
- }
- }
- for (r=1; r<5; r++) {
- if (chesses[i-r][j+r] == 1) {
- if (r == 3) {
- yes2 += 5*fang;
- } else if (r == 4) {
- yes2 += fang*10;
- }
- } else {
- break;
- }
- }
- for (r=1; r<5; r++) {
- if (chesses[i-r][j-r] == 1) {
- if (r == 3) {
- yes2 += 5*fang;
- } else if (r == 4) {
- yes2 += fang*10;
- }
- } else {
- break;
- }
- }
- for (r=1; r<5; r++) {
- if (chesses[i][j+r] == 1) {
- if (r == 3) {
- yes2 += 5*fang;
- } else if (r == 4) {
- yes2 += fang*10;
- }
- } else {
- break;
- }
- }
- for (r=1; r<5; r++) {
- if (chesses[i][j-r] == 1) {
- if (r == 3) {
- yes2 += 5*fang;
- } else if (r == 4) {
- yes2 += fang*10;
- }
- } else {
- break;
- }
- }
- chesses[i][j] = 0;
- if (yes2>yes) {
- yes = yes2;
- yi = i;
- yj = j;
- }
- }
- }
- }
- _root.cd(yi, yj);
- }
- };
- function huiqi() {
- if (_root.hh>=1) {
- trace("悔棋");
- ci = _root.comi[_root.hh];
- cj = _root.comj[_root.hh];
- di = _root.desi[_root.hh];
- dj = _root.desj[_root.hh];
- _root.chess[ci][cj] = 0;
- _root.comi[_root.hh] = null;
- _root.comj[_root.hh] = null;
- _root["black_"+ci+"_"+cj].removeMovieClip();
- _root.chess[di][dj] = 0;
- _root.desi[_root.hh] = null;
- _root.desj[_root.hh] = null;
- _root["white_"+di+"_"+dj].removeMovieClip();
- _root.hh--;
- _root.mi--;
- _root.now = false;
- }
- }
- function see() {
- for (si=0; si<_root.hh; si++) {
- trace([comi[si], comj[si]]+"|"+[desi[si], desj[si]]);
- }
- trace("第"+hh+"回合,當前棋局:");
- for (i=0; i<16; i++) {
- trace(_root.chess[i]);
- }
- }
- _root.menu = right_menu;
復制代碼
|