花语月时钟怎么过:拼图游戏的算法(推动的拼图)FLASH版

来源:百度文库 编辑:高考问答 时间:2024/05/03 09:18:25
如果有随机排序,有可能造成无解,我试想了一个算法,可是我自己写出来以后导致有可能两个位置重叠。我的想法是让一个方块真实的移动,就是随机地上下左右移动,循环50次,以后拼图打乱了,而且是可还原的。
以下是我的代码(ActionScript):
//初始化位置:lct记录16个方块的位置,lct1保存排好时的位置
lct = new Array("0|0", "100|0", "200|0", "300|0", "0|100", "100|100", "200|100", "300|100", "0|200", "100|200", "200|200", "300|200", "0|300", "100|300", "200|300", "300|300");
lct1 = new Array("300|300", "0|0", "100|0", "200|0", "300|0", "0|100", "100|100", "200|100", "300|100", "0|200", "100|200", "200|200", "300|200", "0|300", "100|300", "200|300");
//随机变换位置:原算法
/*for (i=0; i<16; i++) {
a = lct[i];
b = int(Math.random()*16);
lct[i] = lct[b];
lct[b] = a;
}*/
//新随机放置算法
ept = 15;
for (i=0; i<50; i++) {
a = lct[ept];
b = int(Math.random()*100);
if (b<25) {
if (ept != 15 && ept != 3 && ept != 7 && ept != 11) {
chg = ept+1;
}
} else if (b<50 && b>25) {
if (ept != 15 && ept != 12 && ept != 13 && ept != 14) {
chg = ept+4;
}
} else if (b<75 && b>50) {
if (ept != 0 && ept != 1 && ept != 2 && ept != 3) {
chg = ept-4;
}
} else if (b>75) {
if (ept != 0 && ept != 4 && ept != 8 && ept != 12) {
chg = ept-1;
}
}
lct[ept] = lct[chg];
lct[chg] = a;
ept = chg;
}

很想告诉你,可是看不懂!!!

我也看不懂,汗~~~~~~~~~~~``

Acessful darter rest )))
<<<<<<awerfar axsses!

我的视力是5.2,可也看no清!*_*