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

📄 board.java

📁 J2ME程序设计实例教程的源码
💻 JAVA
字号:
import java.util.Random;

/**
 * 该类描述了游戏的基本功能,游戏规则。
 */
public class Board {
    public static final int DOWN = 0;
    public static final int LEFT = 1;
    public static final int RIGHT = 2;
    
    private int width;          //游戏区的宽度
    private int height;         //游戏区的高度
    private int[] state;        //游戏区域的状态
    private int[] blockNumOfRow;    //每一行的方块数
    private int[][] blockSet;   //方块图形初始状态集合
    
    private int[] block;        //当前的方块图形
    private int x0, y0;         //当前方块的旋转点
    private int[] blockTemp;    //当前方块图形移动、旋转时用的中间结果数组
    private boolean down;
    
    //构造方法,创建一个width * height大小的游戏区域。
    public Board(int width, int height) {
        this.width = width;
        this.height = height;
        state = new int[width*height];
        blockNumOfRow = new int[height];
        blockNumOfRow[0] = -1;
        createFigure();
        block = new int[4];
        blockTemp = new int[4];
    }
    
    //创建游戏方块图形
    private void createFigure() {
        blockSet = new int[7][4];
        blockSet[0][0] = getIndex(width/2, 0);
        blockSet[0][1] = getIndex(width/2, 1);
        blockSet[0][2] = getIndex(width/2, 2);
        blockSet[0][3] = getIndex(width/2, 3);
        
        blockSet[1][0] = getIndex(width/2, 0);
        blockSet[1][1] = getIndex(width/2, 1);
        blockSet[1][2] = getIndex(width/2+1, 2);
        blockSet[1][3] = getIndex(width/2, 2);
        
        blockSet[2][0] = getIndex(width/2, 0);
        blockSet[2][1] = getIndex(width/2, 1);
        blockSet[2][2] = getIndex(width/2, 2);
        blockSet[2][3] = getIndex(width/2-1, 2);
        
        blockSet[3][0] = getIndex(width/2, 0);
        blockSet[3][1] = getIndex(width/2, 1);
        blockSet[3][2] = getIndex(width/2+1, 1);
        blockSet[3][3] = getIndex(width/2+1, 2);
        
        blockSet[4][0] = getIndex(width/2, 0);
        blockSet[4][1] = getIndex(width/2-1, 1);
        blockSet[4][2] = getIndex(width/2, 1);
        blockSet[4][3] = getIndex(width/2-1, 2);
        
        blockSet[5][0] = getIndex(width/2, 0);
        blockSet[5][1] = getIndex(width/2+1, 0);
        blockSet[5][2] = getIndex(width/2+1, 1);
        blockSet[5][3] = getIndex(width/2, 1);
        
        blockSet[6][0] = getIndex(width/2, 0);
        blockSet[6][1] = getIndex(width/2-1, 1);
        blockSet[6][2] = getIndex(width/2, 1);
        blockSet[6][3] = getIndex(width/2+1, 1);
    }
    
    //初始化游戏
    public void init() {
        for(int i=0; i<state.length; i++) {
            state[i] = 0;
        }
        for(int i=0; i<blockNumOfRow.length; i++) {
            blockNumOfRow[i] = 0;
        }
        down = true;
        nextFigure();
    }
    
    //随机产生一个当前的方块图形
    public void nextFigure() {
        Random rand = new Random();
        int index = Math.abs(rand.nextInt())%7;
        copyArray(blockSet[index], block);
        x0 = getX(block[2]);
        y0 = getY(block[2]);
    }
    
    //获取(x,y)在一维数组中的位置
    private int getIndex(int x, int y) {
        return width*y + x;
    }
    
    public int getX(int index) {
        return index%width;
    }
    
    public int getY(int index) {
        return index/width;
    }
    
    //拷贝数组
    private void copyArray(int[] src, int[] des) {
        for(int i=0; i<src.length; i++) {
            des[i] = src[i];
        }
    }
    
    //向下移动当前方块图形
    public void down() {
        boolean canMove = true;
        for(int i=0; i<block.length; i++) {
            blockTemp[i] = block[i] + width;
            if(blockTemp[i] >= state.length || state[blockTemp[i]] != 0) {
                canMove = false;
                break;
            }
        }
        
        if(canMove) {
            int[] temp = block;
            block = blockTemp;
            blockTemp = temp;
            y0 += 1;
        }
        down = canMove;
    }
    
    //向左移动当前方块图形
    public void left() {
        boolean canMove = true;
        int x=0;
        int y=0;
        
        for(int i=0; i<block.length; i++) {
            x = getX(block[i])-1;
            y = getY(block[i]);
            blockTemp[i] = getIndex(x, y);
            if(x < 0 || state[blockTemp[i]] != 0) {
                canMove = false;
                break;
            }
        }
        
        if(canMove) {
            int[] temp = block;
            block = blockTemp;
            blockTemp = temp;
            x0 -= 1;
        }
    }
    
    //向右移动当前方块图形
    public void right() {
        boolean canMove = true;
        int x = 0;
        int y = 0;
        
        for(int i=0; i<block.length; i++) {
            x = getX(block[i])+1;
            y = getY(block[i]);
            blockTemp[i] = getIndex(x, y);
            if(x >= width || state[blockTemp[i]] != 0) {
                canMove = false;
                break;
            }
        }
        
        if(canMove) {
            int[] temp = block;
            block = blockTemp;
            blockTemp = temp;
            x0 += 1;
        }
    }
    
    //逆时针方向旋转当前的方块图形90度
    public void circumrotate() {
        boolean canCircu = true;
        int x = 0;
        int y = 0;
        for(int i=0; i<block.length; i++) {
            x = (2*x0+2*getY(block[i])+1-2*y0);
            y = (2*y0-2*getX(block[i])-1+2*x0);
            blockTemp[i] = getIndex(x/2, y/2);
            if(x<0 || x/2>=width || y<0 || y/2>=height || state[blockTemp[i]] != 0) {
                canCircu = false;
                break;
            }
        }

        if(canCircu) {
            int[] temp = block;
            block = blockTemp;
            blockTemp = temp;
        }
    }
    
    //清除
    public int clear() {
        if(down) {
            return -1;
        }
        
        for(int i=0; i<block.length; i++) {
            state[block[i]] = 1;
            blockNumOfRow[getY(block[i])] += 1;
        }
        int i = blockNumOfRow.length-1;
        int total = 0;
        while(i>=0 && blockNumOfRow[i] != 0) {
            if(blockNumOfRow[i] == width) {
                total += 1;
                clearLine(i);
            }
            else {
                i--;
            }
        }
        
        if(!gameOver()) {
            nextFigure();
        }
        
        return total;
    }
    
    //清除行
    private void clearLine(int row) {
        int index = getIndex(0, row)-1;
        for(int i=index; i>=0; i--) {
            state[i+width] = state[i];
        }
        for(int i=0; i<width; i++) {
            state[i] = 0;
        }
        
        for(int i=row-1; i>=0; i--) {
            blockNumOfRow[i+1] = blockNumOfRow[i];
        }
    }

    public int getValue(int x, int y) {
        return state[getIndex(x, y)];
    }
    
    public int[] getCurrentBlock() {
        return block;
    }
    
    public boolean gameOver() {
        return blockNumOfRow[0] != 0;
    }
}

⌨️ 快捷键说明

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