📄 grids.java
字号:
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.TiledLayer;
public class Grids {
//格子图像种类,数值对应Tile图像中的编号
public static final int GRID_NONE = 1; //没有任何棋子
public static final int GRID_BLACK = 2; //黑色棋子(游戏者棋子)
public static final int GRID_WHITE = 3; //白色棋子(电脑棋子)
private TiledLayer m_GridTL; //棋盘Tile
private int m_nCurCol; //最近一次黑方下棋位置的列号
private int m_nCurRow; //最近一次黑方下棋位置的行号
public Grids( int scrWidth, int scrHeight ){
try{
Image img = Image.createImage("/demo/grid.png");
m_GridTL = new TiledLayer( 8, 8, img, 15, 15 );
//设置棋盘的位置
int x = ( scrWidth - m_GridTL.getWidth() ) / 2;
int y = ( scrHeight - m_GridTL.getHeight() ) / 2;
m_GridTL.setPosition(x, y);
}
catch(Exception exception){}
}
//重新开局
public void Reset(){
//将棋盘的所有格子都设置为空
for( int col = 0; col < m_GridTL.getColumns(); col ++ ){
for( int row = 0; row < m_GridTL.getRows(); row ++ ){
m_GridTL.setCell(col, row, GRID_NONE);
}
}
//设置初始的几个棋子
m_GridTL.setCell( 3, 3, Grids.GRID_WHITE );
m_GridTL.setCell( 3, 4, Grids.GRID_BLACK );
m_GridTL.setCell( 4, 3, Grids.GRID_BLACK );
m_GridTL.setCell( 4, 4, Grids.GRID_WHITE );
//设置当前的选择位置
m_nCurCol = m_GridTL.getColumns() / 2;
m_nCurRow = m_GridTL.getRows() / 2;
}
//处理按键操作,参数keyStates为按键的状态
//返回1表示游戏者(黑方)获胜,返回-1表示电脑(白方)获胜
public int Input(int keyStates){
//移动当前的选择位置
if( ( keyStates & GameCanvas.LEFT_PRESSED ) != 0 )
m_nCurCol --;
if( ( keyStates & GameCanvas.RIGHT_PRESSED ) != 0 )
m_nCurCol ++;
if( ( keyStates & GameCanvas.UP_PRESSED ) != 0 )
m_nCurRow --;
if( ( keyStates & GameCanvas.DOWN_PRESSED ) != 0 )
m_nCurRow ++;
//限定选择位置的范围
if( m_nCurCol < 0 )
m_nCurCol = 0;
else if( m_nCurCol >= m_GridTL.getColumns() )
m_nCurCol = m_GridTL.getColumns() - 1;
if( m_nCurRow < 0 )
m_nCurRow = 0;
else if( m_nCurRow >= m_GridTL.getRows() )
m_nCurRow = m_GridTL.getRows() - 1;
if( ( keyStates & GameCanvas.FIRE_PRESSED ) != 0 )
{//按下中心键,准备下棋
if( canChessDown( m_nCurCol, m_nCurRow, false ) )
{//如果该位置可以下棋
m_GridTL.setCell( m_nCurCol, m_nCurRow, GRID_BLACK);
//改变两个黑棋之前的白棋
ChangeChess( m_nCurCol, m_nCurRow );
//电脑下棋
ComputerInput();
}
//检查是否可以继续下棋
for( int col = 0; col < m_GridTL.getColumns(); col ++ )
{//遍历棋盘的所有列
for( int row = 0; row < m_GridTL.getRows(); row ++ )
{//遍历棋盘的所有行
if( canChessDown( col, row, true ) )
{//存在可以继续下棋的位置
return 0;
}
}
}
//没有可以下棋的位置,则返回输赢
return WinLost();
}
return 0;
}
//显示图像
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());
}
//计算输赢
//返回1表示游戏者(黑方)获胜,返回-1表示电脑(白方)获胜
private int WinLost(){
int nWhite = 0; //白子的个数
int nBlack = 0; //黑子的个数
//遍历棋盘,计算白子和黑子的个数
for( int col = 0; col < m_GridTL.getColumns(); col ++ )
{
for( int row = 0; row < m_GridTL.getRows(); row ++ )
{
if( m_GridTL.getCell(col, row) == Grids.GRID_WHITE )
nWhite ++;
else if( m_GridTL.getCell(col, row) == Grids.GRID_BLACK )
nBlack ++;
}
}
//黑子多,则黑方或胜
if( nWhite < nBlack )
return 1;
return -1;
}
//电脑下棋
public void ComputerInput( ){
int rowNum = m_GridTL.getRows(); //棋盘行总数
int colNum = m_GridTL.getColumns(); //棋盘列总数
//先查看四个端点是否可以下棋
if( canChessDown( 0, 0, true ) )
{//如果左上角可以下棋
m_GridTL.setCell(0, 0, Grids.GRID_WHITE);
ChangeChess(0, 0);
return;
}
if( canChessDown( colNum-1, 0, true ) )
{//如果右上角可以下棋
m_GridTL.setCell(colNum - 1, 0, Grids.GRID_WHITE);
ChangeChess(colNum - 1, 0);
return;
}
if( canChessDown( 0, rowNum-1, true ) )
{//如果左下角可以下棋
m_GridTL.setCell(0, rowNum - 1, Grids.GRID_WHITE);
ChangeChess(0, rowNum - 1);
return;
}
if( canChessDown( colNum-1, rowNum-1, true ) )
{//如果右下角可以下棋
m_GridTL.setCell(colNum-1, rowNum - 1, Grids.GRID_WHITE);
ChangeChess(colNum-1, rowNum - 1);
return;
}
//再查看上下两边是否可以下棋
for( int col = 1; col < colNum - 1; col ++ ){
if( canChessDown( col, 0, true ) )
{//如果上边可以下棋
m_GridTL.setCell(col, 0, Grids.GRID_WHITE);
ChangeChess(col, 0);
return;
}
else if( canChessDown( col, rowNum-1, true ) )
{//如果下边可以下棋
m_GridTL.setCell(col, rowNum - 1, Grids.GRID_WHITE);
ChangeChess(col, rowNum - 1);
return;
}
}
//再查看左右两边是否可以下棋
for( int row = 1; row < rowNum - 1; row ++ ){
if( canChessDown( 0, row, true ) )
{//如果左边可以下棋
m_GridTL.setCell(0, row, Grids.GRID_WHITE);
ChangeChess(0, row);
return;
}
else if( canChessDown( colNum-1, row, true ) ){
m_GridTL.setCell(colNum - 1, row, Grids.GRID_WHITE);
ChangeChess(colNum - 1, row);
return;
}
}
for( int col = 1; col < colNum - 1; col ++ )
{//遍历棋盘的所有列
for( int row = 1; row < rowNum - 1; row ++ )
{//遍历棋盘的所有行
if( canChessDown( col, row, true ) )
{
m_GridTL.setCell(col, row, Grids.GRID_WHITE);
ChangeChess(col, row);
return;
}
}
}
}
//判断某位置是否可以下棋
//参数col、row指定位置
//参数bComputer为true表示电脑(白方)下棋,为false表示游戏者(黑方)下棋
private boolean canChessDown( int col, int row, boolean bComputer ){
if( m_GridTL.getCell( col , row) != Grids.GRID_NONE )
return false;
int type = Grids.GRID_WHITE; //白方下棋
if( !bComputer )
type = Grids.GRID_BLACK; //黑方下棋
int total = 0; //可消除对方棋子的个数
//找出左方最近的同色棋子,计算两颗棋子中间的黑棋数
int num = 0;
int x = col - 1;
while( x >= 0 )
{
//如果是空位,则不能消除对方棋子,则退出循环
if( m_GridTL.getCell(x, row) == Grids.GRID_NONE )
break;
if( m_GridTL.getCell(x, row) == type )
{//遇到同色棋子
total = total + num;
break;
}
x --;
num ++;
}
//找出右方最近的同色棋子,改变两颗棋子中间的棋子
num = 0;
x = col + 1;
while( x < m_GridTL.getColumns() )
{
//如果是空位,则不能消除对方棋子,则退出循环
if( m_GridTL.getCell(x, row) == Grids.GRID_NONE )
break;
if( m_GridTL.getCell(x, row) == type )
{//遇到同色棋子
total = total + num;
break;
}
x ++;
num ++;
}
//找出上方最近的同色棋子,改变两颗棋子中间的棋子
num = 0;
int y = row - 1;
while( y >= 0 )
{
//如果是空位,则不能消除对方棋子,则退出循环
if( m_GridTL.getCell(col, y) == Grids.GRID_NONE )
break;
if( m_GridTL.getCell(col, y) == type )
{//遇到同色棋子
total = total + num;
break;
}
y --;
num ++;
}
//找出下方最近的同色棋子,改变两颗棋子中间的棋子
num = 0;
y = row + 1;
while( y < m_GridTL.getRows() )
{
//如果是空位,则不能消除对方棋子,则退出循环
if( m_GridTL.getCell(col, y) == Grids.GRID_NONE )
break;
if( m_GridTL.getCell(col, y) == type )
{//遇到同色棋子
total = total + num;
break;
}
y ++;
num ++;
}
//找出斜45度向上的最近的同色棋子,改变两颗棋子中间的棋子
num = 0;
x = col + 1;
y = row - 1;
while( x < m_GridTL.getColumns() && y >= 0 )
{
//如果是空位,则不能消除对方棋子,则退出循环
if( m_GridTL.getCell(x, y) == Grids.GRID_NONE )
break;
if( m_GridTL.getCell(x, y) == type )
{//遇到同色棋子
total = total + num;
break;
}
x ++;
y --;
num ++;
}
//找出斜45度向下的最近的同色棋子,改变两颗棋子中间的棋子
num = 0;
x = col - 1;
y = row + 1;
while( x >= 0 && y < m_GridTL.getRows() )
{
//如果是空位,则不能消除对方棋子,则退出循环
if( m_GridTL.getCell(x, y) == Grids.GRID_NONE )
break;
if( m_GridTL.getCell(x, y) == type )
{//遇到同色棋子
total = total + num;
break;
}
x --;
y ++;
num ++;
}
//找出斜135度向上的最近的同色棋子,改变两颗棋子中间的棋子
num = 0;
x = col - 1;
y = row - 1;
while( x >= 0 && y >= 0 )
{
//如果是空位,则不能消除对方棋子,则退出循环
if( m_GridTL.getCell(x, y) == Grids.GRID_NONE )
break;
if( m_GridTL.getCell(x, y) == type )
{//遇到同色棋子
total = total + num;
break;
}
x --;
y --;
num ++;
}
//找出斜135度向下的最近的同色棋子,改变两颗棋子中间的棋子
num = 0;
x = col + 1;
y = row + 1;
while( x < m_GridTL.getColumns() && y < m_GridTL.getRows() )
{
//如果是空位,则不能消除对方棋子,则退出循环
if( m_GridTL.getCell(x, y) == Grids.GRID_NONE )
break;
if( m_GridTL.getCell(x, y) == type )
{//遇到同色棋子
total = total + num;
break;
}
x ++;
y ++;
num ++;
}
if( total > 0 )
return true;
return false;
}
//改变当前棋子周围的对方棋子
private void ChangeChess( int col, int row ){
int type = m_GridTL.getCell(col, row); //当前棋子的类型
int changeType; //对方棋子的类型
if( type == Grids.GRID_WHITE )
changeType = Grids.GRID_BLACK;
else if( type == Grids.GRID_BLACK )
changeType = Grids.GRID_WHITE;
else
return;
//找出左方最近的同类型的棋子,改变两颗棋子中间的棋子
int x = col - 1;
while( x >= 0 )
{
int index = m_GridTL.getCell(x, row);
if( index != changeType )
{
if( index == type )
{//如果遇到同类棋子,改变之间的对方棋子
for( int i = x + 1; i < col; i ++ )
{
m_GridTL.setCell(i, row, type);
}
}
break;
}
x --;
}
//找出右方最近的同类型的棋子,改变两颗棋子中间的棋子
x = col + 1;
while( x < m_GridTL.getColumns() )
{
int index = m_GridTL.getCell(x, row);
if( index != changeType )
{
if( index == type )
{//如果遇到同类棋子,改变之间的对方棋子
for( int i = x - 1; i > col; i -- )
{
m_GridTL.setCell(i, row, type);
}
}
break;
}
x ++;
}
//找出上方最近的同类型的棋子,改变两颗棋子中间的棋子
int y = row - 1;
while( y >= 0 )
{
int index = m_GridTL.getCell(col, y);
if( index != changeType )
{
if( index == type )
{//如果遇到同类棋子,改变之间的对方棋子
for( int i = y + 1; i < row; i ++ )
{
m_GridTL.setCell(col, i, type);
}
}
break;
}
y --;
}
//找出下方最近的同类型的棋子,改变两颗棋子中间的棋子
y = row + 1;
while( y < m_GridTL.getRows() )
{
int index = m_GridTL.getCell(col, y);
if( index != changeType )
{
if( index == type )
{//如果遇到同类棋子,改变之间的对方棋子
for( int i = y - 1; i > row; i -- )
{
m_GridTL.setCell(col, i, type);
}
}
break;
}
y ++;
}
//找出斜45度向上的最近的同类型的棋子,改变两颗棋子中间的棋子
x = col + 1;
y = row - 1;
while( x < m_GridTL.getColumns() && y >= 0 )
{
int index = m_GridTL.getCell(x, y);
if( index != changeType )
{
if( index == type )
{//如果遇到同类棋子,改变之间的对方棋子
for( int i = 1; i < x - col; i ++ )
{
m_GridTL.setCell( col + i, row - i, type);
}
}
break;
}
x ++;
y --;
}
//找出斜45度向下的最近的同类型的棋子,改变两颗棋子中间的棋子
x = col - 1;
y = row + 1;
while( x >= 0 && y < m_GridTL.getRows() )
{
int index = m_GridTL.getCell(x, y);
if( index != changeType )
{
if( index == type )
{//如果遇到同类棋子,改变之间的对方棋子
for( int i = 1; i < col - x; i ++ )
{
m_GridTL.setCell( col - i, row + i, type);
}
}
break;
}
x --;
y ++;
}
//找出斜135度向上的最近的同类型的棋子,改变两颗棋子中间的棋子
x = col - 1;
y = row - 1;
while( x >= 0 && y >= 0 )
{
int index = m_GridTL.getCell(x, y);
if( index != changeType )
{
if( index == type )
{//如果遇到同类棋子,改变之间的对方棋子
for( int i = 1; i < col - x; i ++ )
{
m_GridTL.setCell( col - i, row - i, type);
}
}
break;
}
x --;
y --;
}
//找出斜135度向下的最近的同类型的棋子,改变两颗棋子中间的棋子
x = col + 1;
y = row + 1;
while( x < m_GridTL.getColumns() && y < m_GridTL.getRows() )
{
int index = m_GridTL.getCell(x, y);
if( index != changeType )
{
if( index == type )
{//如果遇到同类棋子,改变之间的对方棋子
for( int i = 1; i < x - col; i ++ )
{
m_GridTL.setCell( col + i, row + i, type);
}
}
break;
}
x ++;
y ++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -