手机上一个坦克游戏
源代码在线查看: bitarray.java~71~
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, 2);
int row = 9;
int col = 1;
ba.set(row, col);
ba.set(2, 1);
byte a = ba.get(row, col);
System.out.println(a);
ba.reSet(row, col);
a = ba.get(row, col);
System.out.println(a);
a = ba.get(2, 1);
System.out.println(a);
}/**/
/**
* 设置数组的宽度和高度
* @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 % 8 != 0) {
temp = temp / 8 + 1;
}
else {
temp = temp / 8;
}
//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 (x >= 0 && x < _width && y >= 0 && y < _height) {
byte temp = content[ (x * _height + y + 1) / 7];
//System.out.println("temp:" + temp);
return (byte) ( (temp >> (7 - (x * _height + y + 1) % 7)) & 1);
}
return 0;
}
public void set(int x, int y) {
int tranlatedX = x - m_nX;
int tranlatedY = y - m_nY;
if (x >= 0 && x < _width && y >= 0 && y < _height) {
int temp = (x * _height + y + 1) / 7;
content[temp] =
(byte) (content[temp]
| (1 }
}
public void reSet(int x, int y) {
int tranlatedX = x - m_nX;
int tranlatedY = y - m_nY;
if (x >= 0 && x < _width && y >= 0 && y < _height) {
int temp = (x * _height + y + 1) / 7;
//System.out.println("temp:" + temp);
byte max = Byte.MAX_VALUE;
max = (byte) (max ^ (1 content[temp] =
(byte) (content[temp]
& max);
}
}
public int getWidth() {
return _width + m_nX;
}
public int getHeight() {
return _height + m_nY;
}
}