关于j2me实现拼图游戏的算法实现
2008-12-29 21:52
363 查看
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14
因为前些日子做了一些简单的益智游戏,所以涉及了一些算法上的问题,本来以为这些小游戏非常简单,但也为了效率的确令我小头痛了一下。
开始很快的写玩拼图游戏,玩了玩发现有时候会无解,到论坛看了看,好多人在探讨如何验证拼图无解,大概就是矩阵的逆序数+数眼的奇偶性判断。
正痛苦的时候老付跟我说,你回忆一下咱小时候玩拼图怎么玩的呢,我说我拼不好的话就给它们都扣下来再按上,他说都是完整的拼图手动随意打乱的。
果然啊,其实如果是拼图游戏不必如此费力还要去验证是否有解,如果是生成的整齐矩阵,手动打乱的话必然能保证有解。
初始化时,根据随机数去让图块4方向填补空位。循环100次就ok了。
部分代码如下:
//4X4的数组
public static final int game1[][]={
{0,1,2,3},
{4,5,6,7},
{8,9,10,11},
{12,13,14,15}
};
//得到顺序2维数组
public int [][] get2arry(int arry[][]){
arry=null;
arry=new int[4][4];
for(int i=0;i<arry.length;++i){
for(int j=0;j<arry[i].length;++j){
arry[i][j]=i*4+j;
}
}
return arry;
}
//初始化图块数组
public void InitMy() {
if (buffergame == null) {
int index = 0;
int bufferInt = 0;
buffergame = this.get2arry(buffergame);
while (true) {
for (int i = 0; i < buffergame.length; ++i) {
for (int j = 0; j < buffergame[i].length; ++j) {
if (buffergame[i][j] == 0) {
bufferInt = t.getRandomNumber(0, 2000);
if (bufferInt < 500) {
if (i == 3) {
break;
}
buffergame[i][j] = buffergame[i +
1][j];
buffergame[i + 1][j] = 0;
++index;
} else if (bufferInt < 1000) {
if (i == 0) {
break;
}
buffergame[i][j] = buffergame[i -
1][j];
buffergame[i - 1][j] = 0;
++index;
} else if (bufferInt < 1500) {
if (j == 3) {
break;
}
b483
buffergame[i][j] = buffergame[i][j +
1];
buffergame[i][j + 1] = 0;
++index;
} else {
if (j == 0) {
break;
}
buffergame[i][j] = buffergame[i][j -
1];
buffergame[i][j - 1] = 0;
++index;
}
}
}
}
if (index > 200) {
break;
}
// int pos1, pos2;
// for (int i = 0; i < buffergame.length; ++i) {
// for (int j = 0; j < buffergame[i].length; ++j) {
// while (true) {
// pos1 = t.getRandomNumber(0, 3);
// pos2 = t.getRandomNumber(0, 3);
// if (buffer[pos1][pos2] != 100) {
// buffergame[i][j] = buffer[pos1][pos2];
// buffer[pos1][pos2] = 100;
//// System.out.println("buffergame[" + i +
//// "][" + j + "]==" +
//// buffergame[i][j]);
// break;
// }
// }
// }
// }
// //如果奇偶性不同 从新生成
// if(this.TestArry(buffergame)){
// if(Type.isprint)
// System.out.println("有解");
// break;
// }else{
// if(Type.isprint)
// System.out.println("无解");
// }
// }
}
}
if (Type.isprint)
System.out.println("Init is ok!!");
}
相关文章推荐
- 关于若干选举算法的解释与实现
- [转]flex+BlazeDS关于军标的实现思路和算法
- 【学习】关于电梯算法的C++实现
- 【智能查询】:关于智能查找输入的算法实现1
- 【经典算法】:关于硬币称重问题及编程实现
- 关于j2me应用实现会话跟踪
- 分享关于j2mexmlReader的轻量级实现
- 关于矩阵旋转算法的实现
- 体验C#——关于数组合并的简单算法的实现
- 关于游戏中拦截算法的实现
- 关于j2me game双缓冲实现探讨
- 关于cs231n作业1中KNN算法实现的感悟
- 关于棋盘马走“日”字问题的回溯算法实现
- 关于SLG战略游戏中,人物移动范围的算法(js实现)
- 关于思维, 智力, 算法实现等
- 关于RSA硬件算法的实现问题!
- 关于统计变换(CT/MCT/RMCT)算法的学习和实现
- 关于《射雕英雄传》的数学算法问题 JAVA实现
- 关于点阵字库在J2ME的实现。
- 关于算法4中重定向在eclipse中的实现