📄 computer.java
字号:
package demo;
import javax.microedition.lcdui.game.TiledLayer;
public class Computer {
public static final int N = 0; //空位置
public static final int S = 1; //需要下子的位置
public static final int B = 2; //有黑色棋子(人的棋)
public static final int W = 3; //有白色棋子(电脑的棋)
//数组Chess存储棋谱
//数组中行数越高,表明该行棋谱中S位置越重要,电脑走最重要的位置。
private int Chess[][] =
{ //一个棋子的情况
{ N, N, N, S, B },
{ B, S, N, N, N },
{ N, N, N, S, B },
{ N, B, S, N, N },
{ N, N, S, B, N },
{ N, N, B, S, N },
{ N, N, N, S, W },
{ W, S, N, N, N },
{ N, N, N, S, W },
{ N, W, S, N, N },
{ N, N, S, W, N },
{ N, N, W, S, N },
//两个棋子的情况
{ B, B, S, N, N },
{ N, N, S, B, B },
{ B, S, B, N, N },
{ N, N, B, S, B },
{ N, B, S, B, N },
{ N, B, B, S, N },
{ N, S, B, B, N },
{ W, W, S, N, N },
{ N, N, S, W, W },
{ W, S, W, N, N },
{ N, N, W, S, W },
{ N, W, S, W, N },
{ N, W, W, S, N },
{ N, S, W, W, N },
//三个棋子的情况
{ N, S, B, B, B },
{ B, B, B, S, N },
{ N, B, B, B, S },
{ N, B, S, B, B },
{ B, B, S, B, N },
{ N, S, W, W, W },
{ W, W, W, S, N },
{ N, W, W, W, S },
{ N, W, S, W, W },
{ W, W, S, W, N },
//四个棋子的情况
{ S, B, B, B, B },
{ B, S, B, B, B },
{ B, B, S, B, B },
{ B, B, B, S, B },
{ B, B, B, B, S },
{ S, W, W, W, W },
{ W, S, W, W, W },
{ W, W, S, W, W },
{ W, W, W, S, W },
{ W, W, W, W, S } };
public int m_nCurCol = -1; //电脑最近一次下棋位置的列号
public int m_nCurRow = -1; //电脑最近一次下棋位置的行号
//Computer类的构造方法
public Computer( ){
}
//电脑的输入,参数grid为棋盘格子
public boolean Input( TiledLayer grid ){
int rowSel, colSel; //存储临时的选择位置
m_nCurCol = -1;
m_nCurRow = -1;
int nLevel = -1; //存储临时选择的棋谱级别
boolean bFind; //是否符合棋谱的标志
for( int row = 0; row < grid.getRows(); row ++ )
{//遍历棋盘的所有行
for( int col = 0; col < grid.getColumns(); col ++ )
{//遍历棋盘的所有列
for( int i = Chess.length - 1; i >= 0; i -- )
{//遍历所有级别的棋谱
if( col + 4 < grid.getColumns() )
{//查看从当前棋子开始的横向五个棋子是否符合该级别的棋谱
rowSel = -1;
colSel = -1;
bFind = true;
for( int j = 0; j < 5; j ++ )
{
int index = grid.getCell(col + j, row);
if( index == Grids.GRID_NONE )
{//如果该位置没有棋子,对应的棋谱位置上只能是S或N
if( Chess[i][j] == S )
{//如果是S,则保存位置
rowSel = row; colSel = col + j;
}
else if( Chess[i][j] != N )
{//不是S也不是N,则不符合这个棋谱,结束循环
bFind = false;
break;
}
}
if( index == Grids.GRID_BLACK && Chess[i][j] != B )
{//如果是黑色棋,对应的棋谱位置上应是B,否则结束循环
bFind = false;
break;
}
if( index == Grids.GRID_WHITE && Chess[i][j] != W )
{//如果是白色棋,对应的棋谱位置上应是W,否则结束循环
bFind = false;
break;
}
}
if( bFind && i > nLevel )
{//如果符合此棋谱,且该棋谱比上次找到棋谱的级别高
nLevel = i; //保存级别
m_nCurCol = colSel; //保存位置
m_nCurRow = rowSel;
break; //遍历其他级别的棋谱
}
}
if( row + 4 < grid.getRows() )
{//查看从当前棋子开始的纵向五个棋子是否符合该级别的棋谱
rowSel = -1;
colSel = -1;
bFind = true;
for( int j = 0; j < 5; j ++ )
{
int index = grid.getCell(col, row + j);
if( index == Grids.GRID_NONE )
{//如果该位置没有棋子,对应的棋谱位置上只能是S或N
if( Chess[i][j] == S )
{//如果是S,则保存位置
rowSel = row + j; colSel = col;
}
else if( Chess[i][j] != N )
{//不是S也不是N,则不符合这个棋谱,结束循环
bFind = false;
break;
}
}
if( index == Grids.GRID_BLACK )
{//如果是黑色棋,对应的棋谱位置上应是B,否则结束循环
if( Chess[i][j] != B )
{
bFind = false;
break;
}
}
if( index == Grids.GRID_WHITE && Chess[i][j] != W )
{//如果是白色棋,对应的棋谱位置上应是W,否则结束循环
bFind = false;
break;
}
}
if( bFind && i > nLevel )
{//如果符合此棋谱,且该棋谱比上次找到棋谱的级别高
nLevel = i; //保存级别
m_nCurCol = colSel; //保存位置
m_nCurRow = rowSel;
break; //遍历其他级别的棋谱
}
}
if( col - 4 >= 0 && row + 4 < grid.getRows() )
{//查看从当前棋子开始的斜45度向下的五个棋子是否符合该级别的棋谱
rowSel = -1;
colSel = -1;
bFind = true;
for( int j = 0; j < 5; j ++ )
{
int index = grid.getCell(col - j, row + j);
if( index == Grids.GRID_NONE )
{//如果该位置没有棋子,对应的棋谱位置上只能是S或N
if( Chess[i][j] == S )
{//如果是S,则保存位置
rowSel = row + j; colSel = col - j;
}
else if( Chess[i][j] != N )
{//不是S也不是N,则不符合这个棋谱,结束循环
bFind = false;
break;
}
}
if( index == Grids.GRID_BLACK && Chess[i][j] != B )
{//如果是黑色棋,对应的棋谱位置上应是B,否则结束循环
bFind = false;
break;
}
if( index == Grids.GRID_WHITE && Chess[i][j] != W )
{//如果是白色棋,对应的棋谱位置上应是W,否则结束循环
bFind = false;
break;
}
}
if( bFind && i > nLevel )
{//如果符合此棋谱,且该棋谱比上次找到棋谱的级别高
nLevel = i; //保存级别
m_nCurCol = colSel; //保存位置
m_nCurRow = rowSel;
break; //遍历其他级别的棋谱
}
}
//斜135度的五个棋子
if( col + 4 < grid.getColumns() && row + 4 < grid.getRows() )
{//查看从当前棋子开始的斜135度向下的五个棋子是否符合该级别的棋谱
rowSel = -1;
colSel = -1;
bFind = true;
for( int j = 0; j < 5; j ++ )
{
int index = grid.getCell(col + j, row + j);
if( index == Grids.GRID_NONE )
{//如果该位置没有棋子,对应的棋谱位置上只能是S或N
if( Chess[i][j] == S )
{//如果是S,则保存位置
rowSel = row + j; colSel = col + j;
}
else if( Chess[i][j] != N )
{//不是S也不是N,则不符合这个棋谱,结束循环
bFind = false;
break;
}
}
if( index == Grids.GRID_BLACK && Chess[i][j] != B )
{//如果是黑色棋,对应的棋谱位置上应是B,否则结束循环
bFind = false;
break;
}
if( index == Grids.GRID_WHITE && Chess[i][j] != W )
{//如果是白色棋,对应的棋谱位置上应是W,否则结束循环
bFind = false;
break;
}
}
if( bFind && i > nLevel )
{//如果符合此棋谱,且该棋谱比上次找到棋谱的级别高
nLevel = i; //保存级别
m_nCurCol = colSel; //保存位置
m_nCurRow = rowSel;
break; //遍历其他级别的棋谱
}
}
}
}
}
if( m_nCurRow != -1 ){ //如果选择了一个最佳位置
grid.setCell( m_nCurCol, m_nCurRow, Grids.GRID_WHITE );
return true;
}
//如果所有棋谱都不符合,则随便找一个空位置
for( int col = 0; col < grid.getColumns(); col ++ ){
for( int row = 0; row < grid.getRows(); row ++ ){
if( grid.getCell( col, row ) == Grids.GRID_NONE ){
grid.setCell( col, row, Grids.GRID_WHITE );
m_nCurCol = col;
m_nCurRow = row;
return true;
}
}
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -