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

📄 grids.java

📁 这是一款益智类经典游戏
💻 JAVA
字号:
/*
 * Grids.java
 *
 * Created on 2005年12月13日, 下午4:00
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */
import javax.microedition.lcdui.*;
import java.util.*;
import java.io.IOException;
/**
 *
 * @author YBGame
 */
public class Grids {
    public static byte[][] grids={
        {0,0,1,1},{1,1,0,0},{0,0,1,1},{1,1,0,0},
        {1,0,1,1},{1,1,0,1},{0,1,1,1},{1,1,1,0},
        {1,0,1,0},{1,0,0,1},{0,1,0,1},{0,1,1,0},
        {1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}
    };
    public static final int gridW=7;
    public static final int bgX=6,bgY=0;
    //主坐标格子类型(对应grids里的哪一种格子) 值0---15,,-1代表没有填充管道单元格子,-2
    public static byte[] mainGrids=new byte[gridW*gridW];
    //四类管道生成的随机概率10000|||||0--429 A类,430--619 B,620--920 C,921--1000 D类
    public static int[] mainGridsCreateRnd={400,600,921,1001};
    //颜色定义:靠上边---0,中间的---1,下面的---2,接通的---3,-1代表没有填充管道单元格子,-2代表爆炸的格子
    public static byte[] mainGridsColor=new byte[gridW*gridW];
    private Image[][] gridImage=new Image[4][16];
    private String[] imgPng={"/res/grid0.png","/res/grid1.png","/res/grid2.png","/res/grid3.png"};
    final int blastImgNum=4;
    int curBlastFrame=0;
    private Image[] blastImg=new Image[blastImgNum];
    public Image result=null;
    public int curx=0,cury=0;
    int tx,ty;
    private int[] iDx = {0, 0, -1, 1}; //四种移动方向对x和y坐标的影响
    private int[] iDy = {-1, 1, 0, 0};
    private int Act; //现在的移动方向
    private int MaxAct = 4; //移动方向总数
    public byte[] closeTable=new byte[gridW*gridW];
    public byte[] openTable=new byte[gridW*gridW];
    public byte[] gt,gt1;//临时变量
    public static long lastConnTime=0;
    public static int totalWaitTime=0;
    public static int passWaitTime=0;
    //boolean haveConn=false;
    /** Creates a new instance of Grids */
    public Grids() {
        init();
        for (int i=0;i<gridW*gridW;i++){
            mainGrids[i]=(byte)getGridType();
        }
        changeColor();
    }
   public int getGridType(){
       int r=0;
       int rnd=Tools.getRand(0, 1000);
       for (int i=0;i<4;i++){
           if (rnd<mainGridsCreateRnd[i]){
               r=i;
               break;
           }
       }
       r=r*4+Tools.getRand(0,3);
       return r;
   }
    public void init(){
        for(int i=0;i<gridImage.length;i++){
            try{
                gridImage[i]=ImageSet.extractFrames(Image.createImage(imgPng[i]), 0, 0, 4, 4, 16, 16);
            }catch(Exception e){
                System.out.println("ddddd"+e.toString());
            }
        }
        try{
            blastImg=ImageSet.extractFrames(Image.createImage("/res/blast.png"), 0,0, blastImgNum, 1, 16, 16);
        }catch(IOException e){}
        totalWaitTime=getTotalWaitTime();
    }
    public void clearTable(byte[] tbArg,byte tArg){
        for(int i=0;i<tbArg.length;i++){
            tbArg[i]=tArg;
        }
    }
    //返回Open表中的第一个"1"的位置,如果没有"1",则返回-1
    public int getOpenFirstXy(){
        int r=-1;
        for(int i=0;i<openTable.length;i++){
            if (openTable[i]==(byte)1){
                r=i;
                break;
            }
        }
        return r;
    }
    private boolean ActOK() {
        tx = curx + iDx[Act]; //将到点的x坐标
        ty = cury + iDy[Act]; //将到点的y坐标
        int tmp=cury*gridW+curx;
        int tmp1=ty*gridW+tx;
        if ((tx >gridW-1) || (tx < 0)) //x坐标出界?
            return false;
        if ((ty >gridW-1) || (ty < 0)) //y坐标出界?
            return false;
        if (closeTable[tmp1] == 1) //已到过?
            return false;
        if(mainGrids[tmp1]==-1 || mainGrids[tmp1]==-2)//此处没有格子
            return false;
        gt=grids[mainGrids[tmp]];
        gt1=grids[mainGrids[tmp1]];
        if (Act==0){//向上
            if (gt[0]==1 && gt1[1]==1){
                return true;
            }else{
                return false;
            }
        } else if(Act==1){//向下
            if (gt[1]==1 && gt1[0]==1){
                return true;
            }else{
                return false;
            }
        } else if(Act==2){//向左
            if (gt[2]==1 && gt1[3]==1){
                return true;
            }else{
                return false;
            }
        } else if(Act==3){//向右
            if (gt[3]==1 && gt1[2]==1){
                return true;
            }else{
                return false;
            }
        }
        return true;
    }
    public void findArea(byte tArg,byte[] tarArg){
        clearTable(closeTable,(byte)0);
        int temp=getOpenFirstXy();
        while(temp!=-1){
            openTable[temp]=0;
            curx=temp%gridW;
            cury=temp/gridW;
            closeTable[temp]=1;
            tarArg[temp]=tArg;
            for (Act=0;Act<MaxAct;Act++){
                if (ActOK()){
                    openTable[tx+ty*gridW]=1;
                }
            }
            temp=getOpenFirstXy();
        }
    }
    //根据mainGrids中位置 上或下 将OPEN表中的相应位置置1
    public void fillOpenTop(){
        for (int i=0;i<gridW;i++){
            if (mainGrids[i]!=(byte)-1 && mainGrids[i]!=(byte)-2){
                if (grids[mainGrids[i]][0]==(byte)1){
                    openTable[i]=(byte)1;
                } else{
                    openTable[i]=(byte)0;
                }
            }
        }
    }
    public void fillOpenBtm(){
        for (int i=gridW*gridW-gridW;i<gridW*gridW;i++){
            if (mainGrids[i]!=(byte)-1 && mainGrids[i]!=(byte)-2){
                if (grids[mainGrids[i]][1]==(byte)1){
                    openTable[i]=(byte)1;
                } else{
                    openTable[i]=(byte)0;
                }
            }
        }
    }
    public void fillOpenConn(){
        for (int i=gridW*gridW-gridW;i<gridW*gridW;i++){
            if (mainGridsColor[i]==(byte)0 && grids[mainGrids[i]][1]==(byte)1){
                openTable[i]=(byte)1;
            } else{
                openTable[i]=(byte)0;
            }
        }
    }
    //loc是指在mainGrids中的位置
    public void turn(int loc){
        int gridtype=mainGrids[loc];//-2--15
        if (gridtype!=-1 && gridtype!=-2){
            if (gridtype%4==3){
                gridtype=gridtype-3;
            } else{
                gridtype++;
            }
            mainGrids[loc]=(byte)gridtype;
        }
    }
    //根据当前mainGrids数组中的值算出相应的颜色
    public void changeColor(){
        //首先将所有的格子颜色设为都不相通(1)
        for (int i=0;i<gridW*gridW;i++){
            if (mainGrids[i]!=-1 && mainGrids[i]!=-2){
                mainGridsColor[i]=1;
            }
            else{
                mainGridsColor[i]=mainGrids[i];
            }
        }
        fillOpenBtm();
        findArea((byte)2, mainGridsColor);
        fillOpenTop();
        findArea((byte)0, mainGridsColor);
        fillOpenConn();
        findArea((byte)3, mainGridsColor);
    }
    //找到最右一个有空余位值的列坐标的列值(返回值-1代表已经满)
    public int findRightFallRow(){
        int r=-1;
        boolean b=false;
        int i,j;
        for (j=gridW-2;j>=0;j--){//X
            for (i=0;i<gridW;i++){//Y
                if (mainGrids[i*gridW+j]!=-1 && mainGrids[i*gridW+j]!=-2 &&  mainGrids[i*gridW+j+1]==-1){//如果这个位置是空的
                    r=j;
                    b=true;
                    break;
                }
            }
            if(b){
                break;
            }
        }
        return r;
    }
    //判断是否有空余的位置可以移动
    /************************************************************
     *{//无法右移
     * 0,0,0,1
     * 0,0,1,1
     * 0,0,0,1
     * 0,1,1,1
     * }
     *{//可以移动
     * 0,1,"0",1
     * 0,0,1,1
     * 0,0,0,1
     * 0,1,"0",1
     * }
     */
    public boolean isHaveToMove(){
        boolean b=false;
        int x,y,t,t1;
        for (int i=0;i<gridW*gridW;i++){
            if (i%gridW+1<gridW-1){
                t=mainGrids[i];
                t1=mainGrids[i+1];
                if(t!=-1 && t!=-2 && t1==-1){
                    b=true;
                    break;
                }
            }
        }
        return b;
    }
    //在没有格子可移动时,mainGrids最左边一列空位置用随机数填充
    public void fillMainGridsLeftRow(){
        for (int j=0;j<gridW;j++){
            if (mainGrids[j*gridW]==-1){
                mainGrids[j*gridW]=(byte)Tools.getRand(2,12);
            }
        }
    }
    //将当前的MainGrids中的数据为-1的最右一列填入前左一列
    public void fillMainGridsOneRow(){
        int fi=findRightFallRow();
        if (fi!=-1){//有落的位置
            for (int j=0;j<gridW;j++){
                if (mainGrids[j*gridW+fi]!=-1 && mainGrids[j*gridW+fi]!=-2 && mainGrids[j*gridW+fi+1]==-1){
                    mainGrids[j*gridW+fi+1]=mainGrids[j*gridW+fi];
                    mainGrids[j*gridW+fi]=-1;
                }
            }
        }
    }
    public boolean isHaveConnGrids(){
        boolean b=false;
        for (int i=0;i<gridW*gridW;i++){
            if (mainGridsColor[i]==3){
                b=true;
                break;
            }
        }
        return b;
    }
   public boolean isHaveBlastGrids(){
        boolean b=false;
        for (int i=0;i<gridW*gridW;i++){
            if (mainGridsColor[i]==-2){
                b=true;
                break;
            }
        }
        return b;
    }
    //将所有接通管道的去除
    public void clearConnGrids(){
        for (int i=0;i<gridW*gridW;i++){
            if (mainGridsColor[i]==3){
                mainGrids[i]=-2;
                mainGridsColor[i]=-2;
            }
        }
    }
    public void clearBlastGrids(){
        for (int i=0;i<gridW*gridW;i++){
            if (mainGridsColor[i]==-2){
                mainGrids[i]=-1;
                mainGridsColor[i]=-1;
            }
        }
    }
    //画图
    public void draw(Graphics g){
        int x,y,t,c;
        for (int i=0;i<gridW*gridW;i++){
            x=i%gridW;
            y=i/gridW;
            t=mainGrids[i];//图片类型
            c=mainGridsColor[i];//图片颜色
            if (t>=0){
                if (gridImage[c][t]!=null)  g.drawImage(gridImage[c][t], bgX+x*16, bgY+y*16, Tools.GRAPHCS_TOP_LEFT);
            }
            else if(t==-2 && curBlastFrame<4){
                if (blastImg[curBlastFrame]!=null)  g.drawImage(blastImg[curBlastFrame], bgX+x*16, bgY+y*16, Tools.GRAPHCS_TOP_LEFT);
            }
        }
        if(isHaveBlastGrids()){
            curBlastFrame++;
        }
    }
    //改变mainGrids[]的内容
    public void cycle(){
        if (!isHaveConnGrids()){//如果没有连通的情况
            lastConnTime=System.currentTimeMillis();
        }else{
            if(System.currentTimeMillis()-lastConnTime>1200){
                curBlastFrame=0;
                GameCanvas.totalCursor+=getScore();
                totalWaitTime=getTotalWaitTime();
                passWaitTime=-400;
                clearConnGrids();
            }
        }
        if (curBlastFrame>blastImgNum){
            curBlastFrame=0;
            clearBlastGrids();
        }
        if (isHaveToMove()){
            fillMainGridsOneRow();
        } else{
            fillMainGridsLeftRow();
            passWaitTime+=GameCanvas.rate;
        }
    }
    //当管道通时得到分数
    public int getScore(){
        int t=0;
        int r=0;
        int s=0;
        for(int i=0;i<gridW*gridW;i++){
            if (mainGridsColor[i]==3){
                s+=100;
                if (i<gridW && grids[mainGrids[i]][0]==1){
                    t++;
                }
            }
        }
        switch(t){
            case 1://一个管道
                r=100;
                break;
            case 2:
                r=300;
                break;
            case 3:
                r=500;
                break;
            case 4:
                r=800;
                break;
            case 5:
                r=1200;
                break;
            case 6:
                r=3000;
                break;
            case 7:
                r=10000;
                break;
        }
        return r+s;
    }
    public int getTotalWaitTime(){
        int r=0;
        int totalScore=GameCanvas.totalCursor;
        if (totalScore<=5000){
            r=10000;
        }else if(totalScore<=15000){
            r=9000;
        }else if(totalScore<=30000){
            r=8000;
        }else if(totalScore<=60000){
            r=7000;
        }else if(totalScore<=100000){
            r=6000;
        }else{
            r=5000;
        }
        return r+50000;
    }
    
}

⌨️ 快捷键说明

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