關於j2me實現拼圖遊戲的算法實現

 

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.     }

    

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章