您的位置:首页 > Web前端

关于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!!");

    }

    
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  游戏 j2me 算法 buffer null