J2ME连连看基础功能源代码(含详细注释)

//界面类代码import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Graphics;/*** 连连看游戏界面*/public class LinkCanvas extends Canvas implements Runnable{   /**游戏逻辑类*/   GameEngine engine;     /**屏幕宽度*/   int width;   /**屏幕高度*/   int height;   public LinkCanvas(){     //创建对象     engine = new GameEngine();     //获得屏幕的高度和宽度     width = getWidth();     height = getHeight();     //启动线程     Thread t = new Thread(this);     t.start();   }   /**   * 绘制方法   */   protected void paint(Graphics g) {     //清屏     clearScreen(g);     //绘制地图     engine.paintMap(g);     //绘制选择框     engine.paintSelectArea(g);     //绘制连线     engine.paintLinkLine(g);   }   /**   * 清屏方法   * @param g 画笔   */   private void clearScreen(Graphics g){     g.setColor(0xffffff);     g.fillRect(0, 0, width, height);     g.setColor(0);   }   public void keyPressed(int keyCode){     int action = getGameAction(keyCode);     switch(action){     case UP:       engine.moveUP();       break;     case DOWN:       engine.moveDown();       break;     case LEFT:       engine.moveLeft();       break;     case RIGHT:       engine.moveRight();       break;     case FIRE:       engine.fire();//选择块       break;     }   }   public void run() {     try{       while(true){         //延时         Thread.sleep(100);         //每次判断逻辑         engine.action();         repaint();       }     }catch(Exception e){       e.printStackTrace();     }   }}//逻辑类源代码import java.util.*;import javax.microedition.lcdui.*;/*** 游戏数据和逻辑类*/public class GameEngine {   /**选中块的个数*/   private int selectTileNum = 0;   //第一个选择块的行号和列号   /**行号*/   private int firstRow;   /**列号*/   private int firstCol;   //第二个选择块的行号和列号   /**行号*/   private int secondRow;   /**列号*/   private int secondCol;   //当前选择框,默认在左上角   /**当前选择框的行号*/   private int cRow;   /**当前选择框的列号*/   private int cCol;   /**最大行数*/   private final int MAX_ROW = 10;   /**最大列数*/   private final int MAX_COL = 10;   /**地图数据,0代表空,数据1-10分别代表十种不同的结构*/   private int[][] map = new int[MAX_ROW][MAX_COL];   /**随机数对象*/   private Random ran = new Random();   //地图区域左上角的坐标   private final int LEFTX = 20;   private final int LEFTY = 50;   /**每个单元格的宽度*/   private final int TILE_WIDTH = 20;   /**每个单元格的高度*/   private final int TILE_HEIGHT = 20;   /**连线类型*/   private int linkType;   /**无法连线*/   private final int NO_LINK = 0;   /**水平连线*/   private final int H_LINK = 1;   /**垂直联系*/   private final int V_LINK = 2;   /**一个拐点,先移动x*/   private final int ONE_CORNER_FIRSTX = 3;   /**一个拐点,先移动y*/   private final int ONE_CORNER_FIRSTY = 4;   /**两个拐点,待完善*/   private final int TWO_CORNER = 5;   /**   * 两次拐弯的行号和列号   * 数据格式为:   *  第一个拐点的行号,第一个拐点的列号,第二个拐点的行号,第二个拐点的列号   */   int[] p = new int[4];   public GameEngine(){     //初始化地图数据     initMap();   }   /**   * 初始化地图数据   */   private void initMap(){     for(int row = 0;row < map.length;row++){       for(int col = 0;col < map[row].length;col++){         map[row][col] = row + 1;       }     }     //循环打乱10次     int tempRow;     int tempCol;     int temp;     for(int i = 0;i < 10;i++){       for(int row = 0;row < map.length;row++){         for(int col = 0;col < map[row].length;col++){           //随机行号           tempRow = Math.abs(ran.nextInt() % 10);           //随机列号           tempCol = Math.abs(ran.nextInt() % 10);           //如果不是同一个单元格,则交换数据           if(!((tempRow == row) && (tempCol == col))){             temp = map[row][col];             map[row][col] = map[tempRow][tempCol];             map[tempRow][tempCol] = temp;           }         }       }     }   }   /**   * 绘制地图数据   * @param g 画笔   */   public void paintMap(Graphics g){     for(int row = 0;row < map.length;row++){       for(int col = 0;col

0){       cCol--;     }   }   /**   * 向右移动选择框   */   public void moveRight(){     if(cCol 0){       cRow--;     }   }   /**   * 向下移动选择框   */   public void moveDown(){     if(cRow c2){ //第一块位于右侧       for(int col = c1 - 1;col > c2;col--){         //如果有非空块         if(map[r1][col] != 0){           return false;         }       }     }else{ //第一块位于左侧       for(int col = c2 - 1;col > c1;col--){         //如果有非空块         if(map[r1][col] != 0){           return false;         }       }     }     return true;   }   /**   * 判断块(r1,c1)和块(r2,c2)之间是否是空列   * 不包含这两个块   * @param r1 块1的行号   * @param c1 块1的列号   * @param r2 块2的行号   * @param c2 块2的列号   * @return true代表为空,false代表不为空   */   private boolean isEmptyCol(int r1,int c1,int r2,int c2){     //判断是否位于同一列     if(c1 != c2){       return false;     }     //判断两个块的相对位置     if(r2 > r1){//第一个块在上方       for(int row = r1 + 1;row < r2;row++){         //如果有非空块         if(map[row][c1] != 0){           return false;         }       }     }else{//第二个块在上方       for(int row = r2 + 1;row = 0;row--){       //如果有数据不为空,则直接结束该方向的尝试       if(map[row][c1] != 0){         break;       }       //存储第一个拐点的坐标       p[0] = row;       p[1] = c1;       //每次都尝试转折,则变成一个转点的操作       result = isOneCornerLink(row,c1,r2,c2);       //如果可以连接       if(result != NO_LINK){         //存储第二个拐点的位置         switch(result){         case ONE_CORNER_FIRSTX:           p[2] = row;           p[3] = c2;           break;         case ONE_CORNER_FIRSTY:           p[2] = r2;           p[3] = c1;           break;         }         return true;       }     }     //块1向下     for(int row = r1 + 1;row = 0;col--){       //如果有数据不为空,则直接结束该方向的尝试       if(map[r1][col] != 0){         break;       }       //存储第一个拐点的坐标       p[0] = r1;       p[1] = col;       //每次都尝试转折,则变成一个转点的操作       result = isOneCornerLink(r1,col,r2,c2);       //如果可以连接       if(result != NO_LINK){         //存储第二个拐点的位置         switch(result){         case ONE_CORNER_FIRSTX:           p[2] = r1;           p[3] = c2;           break;         case ONE_CORNER_FIRSTY:           p[2] = r2;           p[3] = col;           break;         }         return true;       }     }     //块1向右     for(int col = c1 + 1;col < MAX_COL;col++){       //如果有数据不为空,则直接结束该方向的尝试       if(map[r1][col] != 0){         break;       }       //存储第一个拐点的坐标       p[0] = r1;       p[1] = col;       //每次都尝试转折,则变成一个转点的操作       result = isOneCornerLink(r1,col,r2,c2);       //如果可以连接       if(result != NO_LINK){         //存储第二个拐点的位置         switch(result){         case ONE_CORNER_FIRSTX:           p[2] = r1;           p[3] = c2;           break;         case ONE_CORNER_FIRSTY:           p[2] = r2;           p[3] = col;           break;         }         return true;       }     }     //四个特例,也就是超出地图区域的连接     //实现地图区域上侧的连接,也就是到上侧是一个空列     if((isEmptyCol(r1,c1,-1,c1)) & (isEmptyCol(r2,c2,-1,c2))){       p[0] = -1;       p[1] = c1;       p[2] = -1;       p[3] = c2;       return true;     }     //左侧     if((isEmptyRow(r1,c1,r1,-1)) & (isEmptyRow(r2,c2,r2,-1))){       p[0] = r1;       p[1] = -1;       p[2] = r2;       p[3] = -1;       return true;     }     //下侧     if((isEmptyCol(r1,c1,MAX_ROW,c1)) & (isEmptyCol(r2,c2,MAX_ROW,c2))){       p[0] = MAX_ROW;       p[1] = c1;       p[2] = MAX_ROW;       p[3] = c2;       return true;     }     //右侧     if((isEmptyRow(r1,c1,r1,MAX_COL)) & (isEmptyRow(r2,c2,r2,MAX_COL))){       p[0] = r1;       p[1] = MAX_COL;       p[2] = r2;       p[3] = MAX_COL;       return true;     }         return false;   }   /**   * 逻辑判断是否有连线   * @return NO_LINK代表无连线,其它数据代表有连线   */   private int logic(){     //如果数值不同     if(map[firstRow][firstCol] != map[secondRow][secondCol]){       return NO_LINK;     }     //判断连接方式     if(isEmptyRow(firstRow,firstCol,secondRow,secondCol)){ //水平连线       return H_LINK;     }     if(isEmptyCol(firstRow,firstCol,secondRow,secondCol)){//垂直连线       return V_LINK;     }     //一个转点的连接     int result = isOneCornerLink(firstRow,firstCol,secondRow,secondCol);     if(result != NO_LINK){       return result;     }     //两个转点的连接     if(isTwoCornerLink(firstRow,firstCol,secondRow,secondCol)){       return TWO_CORNER;     }         //返回无连接     return NO_LINK;   }   /**   * 逻辑判别和逻辑处理   */   public boolean action(){     //判断是否选择两个方块     if(selectTileNum != 2){       return false;     }     boolean b = false;     //判断是否有连线     linkType = logic();     //如果有连线,则消失     if(linkType != NO_LINK){       map[firstRow][firstCol] = 0;       map[secondRow][secondCol] = 0;       b = true;     }     //选择的块数初始化     selectTileNum = 0;     return b;   }}

文章来源:http://blog.csdn.net/Mailbomb/archive/2008/04/25/2328133.aspx

生活若剥去了理想梦想幻想,那生命便只是一堆空架子

J2ME连连看基础功能源代码(含详细注释)

相关文章:

你感兴趣的文章:

标签云: