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

📄 bitarray.java~79~

📁 手机上一个坦克游戏
💻 JAVA~79~
字号:
package demo;

/*
 * Created on 2004-1-14
 *
 */

/**
 * @author Dabao
 */
public class BitArray {
    private int _width, _height;
    private byte[] content = null;
    public int m_nX = 0, m_nY = 0;

    /*public static void main(String[] args) {
        //*
        BitArray ba = new BitArray(10, 10, 0, 0);
        for(int i = 0; i < 10; i++) {
            for(int j = 0; j < 10; j++) {
                System.out.println("(" + i + "," + j + ")");
                ba.set(i,j);
                int a = ba.get(i,j);
                System.out.println("a = " + a);
                ba.printArray();
                ba.reSet(i,j);
            }
        }
        //ba.printArray();
        /**/
        /*
                         BitArray ba = new BitArray(264,70,0,138);
                         ba.reSet(150,144);
                         byte a = ba.get(150,144);
                         System.out.println(a);
                         ba.set(150,144);
                         a = ba.get(150,144);
                         System.out.println(a);
         /**/
         /*int t = (byte)0x80;
                    System.out.println(t>>7);
                    /**/

    //}

    /**
     * 设置数组的宽度和高度
     * @param width
     * @param height
     */
    public BitArray(int w, int h, int cx, int cy) {
        this._width = w;
        this._height = h;
        this.m_nX = cx;
        this.m_nY = cy;
        //System.out.println("w:" + w + " h:" + h + " cx:" + cx + " cy:" + cy);
        if (w > 0 && h > 0) {
            int temp = w * h;
            //System.out.println("temp  = " + temp);
            if (temp % 7 != 0) {
                temp = temp / 7 + 1;
            }
            else {
                temp = temp / 7;
            }
            //System.out.println(temp);
            content = new byte[temp];
            for (int i = 0; i < content.length; i++) {
                content[i] = 0;
            }
        }
    }

    /**
     * 参数是按照数组的坐标来计算的,都是从0 到width-1
     * 从0 到height -1
     * @param col
     * @param row
     * @return
     */
    public byte get(int x, int y) {
        int tranlatedX = x - m_nX;
        int tranlatedY = y - m_nY;
        if ( (tranlatedX * tranlatedY) >= (_width * _height)) {
            System.out.println(tranlatedX);
            System.out.println(tranlatedY);
            return -1;
        }

        if (tranlatedX >= 0
            && tranlatedX < _width
            && tranlatedY >= 0
            && tranlatedY < _height) {
            byte temp = content[getCursor(tranlatedX, tranlatedY)];
            //System.out.println("temp:" + temp);
            return (byte)
                ( (temp >> getOffset(tranlatedX, tranlatedY)) & 1);
        }
        return 0;
    }

    public void set(int x, int y) {
        int tranlatedX = x - m_nX;
        int tranlatedY = y - m_nY;

        if (tranlatedX >= 0
            && tranlatedX < _width
            && tranlatedY >= 0
            && tranlatedY < _height) {
            //int temp = (tranlatedX * _height + tranlatedY + 1) / 7;
            int temp = getCursor(tranlatedX, tranlatedY);
            try {
                content[temp] =
                    (byte) (content[temp]
                            | (1 << getOffset(tranlatedX, tranlatedY)));
            }
            catch (Exception ex) {
                System.out.println("set temp = " + temp);
                System.out.println(
                    "tranlatedX :"
                    + tranlatedX
                    + " tranlatedY: "
                    + tranlatedY
                    + " x: "
                    + x
                    + " y:"
                    + y);
                ex.printStackTrace();
            }

        }

    }

    public void reSet(int x, int y) {
        int tranlatedX = x - m_nX;
        int tranlatedY = y - m_nY;
        if (tranlatedX >= 0
            && tranlatedX < _width
            && tranlatedY >= 0
            && tranlatedY < _height) {
            int temp = getCursor(tranlatedX, tranlatedY);

            //System.out.println("temp:" + temp);
            byte max = Byte.MAX_VALUE;
            max =
                (byte) (max
                        ^ (1 << getOffset(tranlatedX, tranlatedY)));
            content[temp] = (byte) (content[temp] & max);
        }
    }

    public int getWidth() {
        return _width + m_nX;
    }

    public int getHeight() {
        return _height + m_nY;
    }

    private int getCursor(int tx, int ty) {
        if (tx < 0 || ty < 0 || tx >= _width || ty >= _height) {
            return -1;
        }
        int cur = tx * _height + ty + 1;
        if (cur % 7 == 0) {
            return cur / 7 - 1;
        }
        else {
            return cur / 7;
        }
    }

    private int getOffset(int tx, int ty) {
        if (tx < 0 || ty < 0 || tx >= _width || ty >= _height) {
            return -1;
        }
        int cur = tx * _height + ty + 1;
        if (cur % 7 == 0) {
            return 6;
        }
        else {
            return cur % 7 - 1;
        }

    }

    public void printArray() {
        System.out.println("printArray:");
        for (int i = 0; i < _width; i++) {
            for (int j = 0; j < _height; j++) {
                System.out.print(get(i, j));
                System.out.print(" ");
            }
            System.out.println();
        }
    }
    /*public static void main(String[] args) {
            BitArray ba = new BitArray(264,70,0,138);
            ba.set(263,207);
             }*/
}

⌨️ 快捷键说明

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