关于j2me实现拼图游戏的算法实现

来源:互联网 发布:python adb命令 编辑:程序博客网 时间:2024/05/19 02:16

 

0    1   2     3

4    5   6     7

8    9   10   11

12  13 14 

 

因为前些日子做了一些简单的益智游戏,所以涉及了一些算法上的问题,本来以为这些小游戏非常简单,但也为了效率的确令我小头痛了一下。

开始很快的写玩拼图游戏,玩了玩发现有时候会无解,到论坛看了看,好多人在探讨如何验证拼图无解,大概就是矩阵的逆序数+数眼的奇偶性判断。

正痛苦的时候老付跟我说,你回忆一下咱小时候玩拼图怎么玩的呢,我说我拼不好的话就给它们都扣下来再按上,他说都是完整的拼图手动随意打乱的。

果然啊,其实如果是拼图游戏不必如此费力还要去验证是否有解,如果是生成的整齐矩阵,手动打乱的话必然能保证有解。

初始化时,根据随机数去让图块4方向填补空位。循环100次就ok了。

 

部分代码如下:

 

 

  1.     //4X4的数组
  2.     public static final int  game1[][]={
  3.                          {0,1,2,3},
  4.                          {4,5,6,7},
  5.                          {8,9,10,11},
  6.                          {12,13,14,15}
  7.     };
  8.    //得到顺序2维数组
  9.     public int [][] get2arry(int arry[][]){
  10.             arry=null;
  11.             arry=new int[4][4];
  12.             for(int i=0;i<arry.length;++i){
  13.                 for(int j=0;j<arry[i].length;++j){
  14.                     arry[i][j]=i*4+j;
  15.                 }
  16.             }
  17.         return arry;
  18.     }
  19.     //初始化图块数组
  20.     public void InitMy() {
  21.         if (buffergame == null) {
  22.             int index = 0;
  23.             int bufferInt = 0;
  24.             buffergame = this.get2arry(buffergame);
  25.             while (true) {
  26.                 for (int i = 0; i < buffergame.length; ++i) {
  27.                     for (int j = 0; j < buffergame[i].length; ++j) {
  28.                         if (buffergame[i][j] == 0) {
  29.                             bufferInt = t.getRandomNumber(02000);
  30.                             if (bufferInt < 500) {
  31.                                 if (i == 3) {
  32.                                     break;
  33.                                 }
  34.                                 buffergame[i][j] = buffergame[i +
  35.                                         1][j];
  36.                                 buffergame[i + 1][j] = 0;
  37.                                 ++index;
  38.                             } else if (bufferInt < 1000) {
  39.                                 if (i == 0) {
  40.                                     break;
  41.                                 }
  42.                                 buffergame[i][j] = buffergame[i -
  43.                                         1][j];
  44.                                 buffergame[i - 1][j] = 0;
  45.                                 ++index;
  46.                             } else if (bufferInt < 1500) {
  47.                                 if (j == 3) {
  48.                                     break;
  49.                                 }
  50.                                 buffergame[i][j] = buffergame[i][j +
  51.                                         1];
  52.                                 buffergame[i][j + 1] = 0;
  53.                                 ++index;
  54.                             } else {
  55.                                 if (j == 0) {
  56.                                     break;
  57.                                 }
  58.                                 buffergame[i][j] = buffergame[i][j -
  59.                                         1];
  60.                                 buffergame[i][j - 1] = 0;
  61.                                 ++index;
  62.                             }
  63.                         }
  64.                     }
  65.                 }
  66.                 if (index > 200) {
  67.                     break;
  68.                 }
  69. //                        int pos1, pos2;
  70. //                        for (int i = 0; i < buffergame.length; ++i) {
  71. //                            for (int j = 0; j < buffergame[i].length; ++j) {
  72. //                                while (true) {
  73. //                                    pos1 = t.getRandomNumber(0, 3);
  74. //                                    pos2 = t.getRandomNumber(0, 3);
  75. //                                    if (buffer[pos1][pos2] != 100) {
  76. //                                        buffergame[i][j] = buffer[pos1][pos2];
  77. //                                        buffer[pos1][pos2] = 100;
  78. ////                                        System.out.println("buffergame[" + i +
  79. ////                                                "][" + j + "]==" +
  80. ////                                                buffergame[i][j]);
  81. //                                        break;
  82. //                                    }
  83. //                                }
  84. //                            }
  85. //                        }
  86. //                        //如果奇偶性不同 从新生成
  87. //                        if(this.TestArry(buffergame)){
  88. //                            if(Type.isprint)
  89. //                                System.out.println("有解");
  90. //                            break;
  91. //                        }else{
  92. //                            if(Type.isprint)
  93. //                                System.out.println("无解");
  94. //                        }
  95. //                    }
  96.             }
  97.         }
  98.         if (Type.isprint)
  99.             System.out.println("Init is ok!!");
  100.     }