标题:
原创五子棋算法1.0
[打印本页]
作者:
51hei丽人
时间:
2016-6-21 01:10
标题:
原创五子棋算法1.0
原创垃圾算法,两下子就能下赢的。
用法:在第一帧的场景外面处放置三个影片剪辑,分别命名为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("状态", 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说明:算法是原创的,所以电脑智商不高……作者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;
复制代码
作者:
1014407917
时间:
2018-5-17 16:35
您好,这个是个是给单片机里面写的程序码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1