刚用 j2ME 写的扫雷游戏, 哥调c库太麻烦了...
import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author blueguy
*/
public class GameLogic {
MainCanvas mc;
public final int WIDTH = 240;
public final int HEIGHT = 360;
//总区域的列数和行数
public int cols = 13;
public int rows = 16;
public int rectWH = 16;
public int nowCol;
public int nowRow;
public Image[] img;
Random rd = new Random();
public int num = 30;//雷的总数
public short[][] mapData = new short[cols][rows];//地图数组 0: 原始 1: 地雷 2: 空地 3-10: 1-8数字 11:标记
public boolean[][] isLei = new boolean[cols][rows];//地雷位置
public boolean[][] isTest = new boolean[cols][rows];//已探测区域
public boolean[][] isWrong=new boolean[cols][rows];
public int markNum = 0;//标记数量
public int leiCount;//标记正确的地雷数目
public int srcX, srcY;//雷区在屏幕的起始坐标
public GameLogic(MainCanvas gc) {
mc = gc;
init();
}
public void init() {
srcX = (WIDTH - cols * rectWH) >> 1;
srcY = rectWH * 3 - 5;
initImg();
initLei(num);
}
//初始化图片数组
public void initImg() {
try {
int tx = 0;
int ty = 0;
Image img1 = Image.createImage("/TL.png");
img = new Image[13];
for (int i = 0; i < img.length-1; i++) {
if (i >= 6) {
ty = rectWH;
tx = (i - 6) * rectWH;
} else {
tx = i * rectWH;
}
Image image = Image.createImage(16, 16);
Graphics g = image.getGraphics();
g.setColor(0);
g.fillRect(0, 0, 240, 320);
g.drawImage(img1, -tx, -ty, 0);
img[i] = image;
}
img[12]=Image.createImage("/2.png");
} catch (Exception e) {
e.printStackTrace();
}
}
public void drawMap(Graphics g) {
int mapNum = 0;
// System.out.println(mapData.length);
for (int j = 0; j < mapData[0].length; j++) {
for (int i = 0; i < mapData.length; i++) {
mapNum = mapData[i][j];
// System.out.println("**********************:"+mapNum);
g.drawImage(img[mapNum], srcX + i * rectWH, srcY + j * rectWH, 0);
if(isWrong[i][j]&&mc.isEnd){
drawX(g,0xff0000,srcX + i * rectWH,srcY + j * rectWH,rectWH);
}
}
}
g.setColor(0);
// System.out.println(nowCol + "," + nowRow);
g.drawRect(srcX + nowCol * rectWH, srcY + nowRow * rectWH, rectWH, rectWH);
}
public void drawX(Graphics g, int color, int x, int y, int width) {
g.setColor(color);
g.drawLine(x, y, x + width, y + width);
g.drawLine(x, y + width, x + width, y);
}
public void drawF(Graphics g,int color,int x,int y,int width,int height ){
g.setColor(color);
g.drawRect(x, y, width, height);
}
//初始化地雷
public void initLei(int num) {
while (num > 0) {
int max = cols * rows;
int rand = Math.abs(rd.nextInt() % max);
int col = rand % cols;
int row = rand / cols;
while (true) {
if (isLei[col][row] == false) {
isLei[col][row] = true;
// mapData[col][row]=1;
num--;
break;
}
rand++;
if (rand >= max) {
rand = 0;
}
col = rand % cols;
row = rand / cols;
}
}
for (int j = 0; j < isLei[0].length; j++) {
for (int i = 0; i < isLei.length; i++) {
System.out.print(isLei[i][j] + ",");
}
System.out.println();
}
// for(int j=0;j<isLei[0].length;j++){
// for(int i=0;i<isLei.length;i++){
//
// System.out.print(mapData[i][j]+",");
// }
// System.out.println();
// }
}
//检测指定位置是否有雷
public boolean isLei(int col, int row) {
if (col < 0 || col >= cols) {
return false;
}
if (row < 0 || row >= rows) {
return false;
}
return isLei[col][row];
}
// public boolean isTest(int col, int row) {
// if (col < 0 || col >= cols) {
// return false;
// }
// if (row < 0 || row >= rows) {
// return false;
// }
// return isTest[col][row];
// }
//打开所有雷
public void findAll() {
for (int col = 0; col < isLei.length; col++) {
for (int row = 0; row < isLei[0].length; row++) {
if (isLei[col][row] && !isTest[col][row]&&mapData[col][row]!=11) {
mapData[col][row] = 1;//显示地雷
}
if(mapData[col][row]==11&&isWrong[col][row]){
mapData[col][row] = 1;
}
}
}
}
//标记雷
public void markLei() {
if (!isTest[nowCol][nowRow]) {
if (mapData[nowCol][nowRow] != 11) {
markNum++;
mapData[nowCol][nowRow] = 11;
isWrong[nowCol][nowRow]=true;
if (isLei[nowCol][nowRow]) {
isWrong[nowCol][nowRow]=false;
leiCount++;
}
} else {
mapData[nowCol][nowRow] = 0;
markNum--;
isWrong[nowCol][nowRow]=false;
if (isLei[nowCol][nowRow]) {
leiCount--;
}
}
}
}
//判断是否是雷
public boolean checkLei(int col, int row) {
if (isLei[col][row]) {
return true;
}
if (mapData[col][row] != 0) {
return false;
}
int tpCount = 0;
for (int c = -1; c <= 1; c++) {
for (int r = -1; r <= 1; r++) {
if (isLei(col + c, row + r)) {
tpCount++;
}
}
}
mapData[col][row] = (short) (2 + tpCount);
isTest[col][row] = true;
// System.out.println(" mapData[col][row]:"+ mapData[col][row]+" col:"+ col+ " row:"+row);
// System.out.println("tpCount:"+tpCount);
if (tpCount == 0) {
for (int c = -1; c <= 1; c++) {
if (col + c < 0 || col + c >= isLei.length) {
continue; //边缘情况
}
for (int r = -1; r <= 1; r++) {
if (row + r < 0 || row + r >= isLei[0].length) {
continue;//边缘情况
}
checkLei(col + c, row + r);
}
}
}
return false;
}
public void initGame() {
mapData = new short[cols][rows];
isLei = new boolean[cols][rows];
isTest = new boolean[cols][rows];
isWrong=new boolean[cols][rows];
initLei(num);
mc.isEnd = false;
mc.isWin = false;
nowCol = 0;
nowRow = 0;
markNum = 0;//标记数量
leiCount = 0;
mc.tempFlag=false;
mc.timeCount=0;
}
}