⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 randomtiledlayercellpos.java

📁 一个J2ME游戏,单机玩拼图游戏 ,游戏难度分为简单、一般、困难。简单是把一张图片分成了4块
💻 JAVA
字号:
package util;/*
 *suport TiledLayer position
 */
 import javax.microedition.lcdui.game.TiledLayer;
 import java.util.Random;
 import java.util.Date;

public class RandomTiledLayerCellPos
{
   private TiledLayer tiledLayer = null;

   private int[][] initPosArray = null;
   private int[][] randomPosArray = null;
   private int col = 0;
   private int row = 0;
   private Random g_rand = new Random();
   private int k = g_rand.nextInt();


   public RandomTiledLayerCellPos(TiledLayer tiledLayer,int col,int row)
   {
      this.tiledLayer = tiledLayer;
      initPosArray = new int[row][col];
      randomPosArray = new int[row][col];
      initArrayWith0(initPosArray);
      initArrayWith0(randomPosArray);
      this.col = col;
      this.row = row;

      int n = 1;
      for(int i=0;i<row;i++)
        for(int k=0;k<col;k++)
        {
           initPosArray[i][k]=n;
           n++;
        }
      this.setRandomArray();
   }

   public void setPos(int[][] pos)
   {
      if(null != tiledLayer)
      {
        for(int i=0;i<pos.length;i++)
        {
          for(int k=0;k<pos[0].length;k++)
          {
            tiledLayer.setCell(k,i,pos[i][k]);
          }
        }
      }
   }

   public void changeCellsPos(int X1,int Y1,int X2,int Y2)
   {
      if(null != randomPosArray)
      {
         int m = randomPosArray[X1][Y1];
         randomPosArray[X1][Y1]=randomPosArray[X2][Y2];
         randomPosArray[X2][Y2]=m;
      }
   }

   public void initArrayWith0(int[][] a)
   {
       for(int i=0;i<row;i++)
         for(int k=0;k<col;k++)
         {
            a[i][k]=0;
         }

   }

   public void setRandomArray()
   {
       RandomArray random = new  RandomArray();
       int[] b = new int[row*col];
       for(int i=0;i<b.length;i++)
          b[i] = i+1;
      int[] a = random.createRandomIntArray(b);
       int m=0;
      for(int i=0;i<row;i++)
        for(int k=0;k<col;k++)
        {
           randomPosArray[i][k]=a[m];
            m++;
        }

   }

   public boolean compareArray(int[][] a,int[][] b)
   {
      if(a.length == b.length)
      {
         for(int i=0;i<a.length;i++)
         {
           if(a[i].length == b[i].length)
           {
               for(int k=0;k<a[i].length;k++)
               {
                 if(a[i][k] != b[i][k])
                   return false;
               }
           }else
           {
               return false;
           }
         }
      }else
      {
         return false;
      }
      return true;
   }

   public boolean isExistInArray(int[][] a,int k)
   {
       for(int i=0;i<a.length;i++)
         for(int j=0;j<a[i].length;j++)
            if(a[i][j]==k)
                return true;
       return false;
   }

   public int reDraw(int r1,int c1,int r2,int c2)
   {

      if((this.initPosArray[r1][c1] != this.randomPosArray[r1][c1]) && (this.initPosArray[r2][c2] != this.randomPosArray[r2][c2]))
      {
          this.changeCellsPos(r1,c1,r2,c2);
          this.setPos(this.randomPosArray);
//          this.printArray(this.randomPosArray);
          return 0;
      }else if((this.initPosArray[r1][c1] == this.randomPosArray[r1][c1]) && (this.initPosArray[r2][c2] == this.randomPosArray[r2][c2]))
        return 1;
       else if(this.initPosArray[r1][c1] == this.randomPosArray[r1][c1])
        return 2;
       else if(this.initPosArray[r2][c2] == this.randomPosArray[r2][c2])
        return 3;
       else
        return -1;
   }

   public int[][] getRandomPosArray()
   {
       return randomPosArray;
   }
   public void setRandomPosArray(int[][] array)
   {
       this.randomPosArray = array;
   }
   public int[][] getInitPosArray()
   {
       return initPosArray;
   }
   public void setInitPosArray(int[][] array)
   {
       this.initPosArray = array;
   }

   public void printArray(int[][] a)
   {
       for(int i=0;i<row;i++)
       {
          for(int j=0;j<col;j++)
          {
            System.out.print(lpad(String.valueOf(a[i][j]),4," "));
          }
          System.out.println();
       }
       System.out.println("----------------------------------");
   }

   public String lpad(String a,int len,String fill)
   {
      if(a.length()<len)
      {
          String tem = "";
          for(int i=0;i<len-a.length();i++)
            tem+=fill;
          return a+tem;
      }
      return a;
   }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -