?? grids.java
字號:
import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.TiledLayer;
public class Grids {
//定義一組格子狀態值,這些值對應圖片中的索引
public static final int GRID_UP = 1; //未挖開的方格
public static final int GRID_MINE = 2; //地雷方格
public static final int GRID_NUMBER0 = 3; //0號數字方格的位置
public TiledLayer m_GridTL; //格子圖像
public Random m_Random; //隨機數對象
public int m_nCurCol = 0; //當前所選格子的列數
public int m_nCurRow = 0; //當前所選格子的行數
public boolean m_aMines[][]; //格子中有無地雷的標志
//構造方法,
//參數nCols,nRows分別是格子的列數和行數
//參數scrWidth,scrHeight分別是屏幕的寬和高
public Grids( int nCols, int nRows, int ScrWidth, int ScrHeight ){
m_Random = new Random();
try{
Image img = Image.createImage("/demo/TL.png");
m_GridTL = new TiledLayer( nCols, nRows, img, 16, 16 );
int nX = ScrWidth / 2 - m_GridTL.getWidth() / 2;
int nY = ScrHeight / 2 - m_GridTL.getHeight() / 2;
m_GridTL.setPosition( nX, nY );
m_aMines = new boolean[nCols][nRows];
}
catch (IOException e){}
}
//重新設置格子,num是規定的格子中的地雷數
public void ResetMines( int num ){
for( int col = 0; col < m_GridTL.getColumns(); col ++ ){
for( int row = 0; row < m_GridTL.getRows(); row ++ ){
m_GridTL.setCell( col, row, GRID_UP );
m_aMines[col][row] = false;
}
}
//設置地雷
while( num > 0 ){
int total = m_GridTL.getColumns() * m_GridTL.getRows();
int n = Math.abs(m_Random.nextInt() % total);
int col = n % m_GridTL.getColumns();
int row = n / m_GridTL.getColumns();
while( true ){
if( m_aMines[col][row] == false ){
m_aMines[col][row] = true;
break;
}
n ++;
if( n >= total )
n = 0;
col = n % m_GridTL.getColumns();
row = n / m_GridTL.getColumns();
}
num --;
}
}
//顯示所有地雷
public void ShowAllMines(){
for( int col = 0; col < m_aMines.length; col ++ ){
for( int row = 0; row < m_aMines[0].length; row ++ ){
if( m_aMines[col][row] ){
m_GridTL.setCell( col, row, GRID_MINE );
}
}
}
}
//挖雷,并炸開周圍沒有地雷的格子
//返回true表示挖到地雷,false表示沒有挖到地雷
public boolean DigMine( int col, int row ){
/***第(1)步,檢測當前方塊是否已被挖開**********/
if( m_GridTL.getCell( col, row ) != GRID_UP )
{//當前方塊已被挖開,結束操作
return false;
}
/***第(2)步,檢測當前方塊中是否有地雷**********/
if( m_aMines[col][row] )
{//當前方塊中有地雷,結束操作
return true;
}
/***第(3)步,挖開當前方塊并顯示周圍的地雷總數***/
//計算當前方塊周圍地雷的總數量
int num = 0;
for( int c = -1; c <= 1; c ++ ){
for( int r = -1; r <= 1; r ++ ){
if( CheckMine( col + c, row + r ) ) //檢測是否有地雷
num ++;
}
}
//挖開方塊并顯地雷總數
m_GridTL.setCell( col, row, GRID_NUMBER0 + num );
if( num > 0 )
return false;
/***第(4)步,處理周圍的方塊*******************/
for( int c = -1; c <= 1; c ++ ){
if( col + c < 0 || col + c >= m_aMines.length )
continue;
for( int r = -1; r <= 1; r ++ ){
if( row + r < 0 || row + r >= m_aMines[0].length )
continue;
DigMine( col + c, row + r ); //重新執行第(1)步操作
}
}
return false;
}
//檢測指定格子中是否有地雷,參數col,row分別是指定格子的列數和行數
private boolean CheckMine( int col, int row ){
if( col < 0 || col >= m_aMines.length )
return false;
if( row < 0 || row >= m_aMines[0].length )
return false;
return m_aMines[col][row];
}
//顯示所有格子及當前選擇的位置
public void Paint( Graphics g ){
m_GridTL.paint( g );
int x = m_GridTL.getX() + m_nCurCol * m_GridTL.getCellWidth();
int y = m_GridTL.getY() + m_nCurRow * m_GridTL.getCellHeight();
g.drawRect(x, y, m_GridTL.getCellWidth(), m_GridTL.getCellHeight());
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -