//复制要移动的区域,因为这块区域可能会被覆盖掉
for (int i = 0; i < this.SelectArea[2]; i++) {
for (int j = 0; j < this.SelectArea[3]; j++) {
temp[j][i] = this.MyMap.Grid[this.SelectArea[1] +j][this.SelectArea[0] + i];
}
}
//将要移动的区域移动到刚选中的区域(即要移动到的区域)
for (int i = 0; i < this.SelectArea[2]; i++) {
for (int j = 0; j < this.SelectArea[3]; j++) {
this.MyMap.Grid[this.MoveArea[1] + j][this.MoveArea[0] + i] = temp[j][i];
}
}
//将要移动的区域中无用内容置成空白
for (int i = 0; i < this.SelectArea[3]; i++) {
for (int j = 0; j < this.SelectArea[2]; j++) {
if (!isInRange2(this.SelectArea[0] + j,this.SelectArea[1] + i)) {
//该点是不在要移动到的区域之内,需置空
this.MyMap.Grid[this.SelectArea[1] + i][this.SelectArea[0] + j] = Images.BLANK;
}
}
}
//交换SelectArea和MoveArea
byte tempbyte;
tempbyte= SelectArea[0];
SelectArea[0]=MoveArea[0];
MoveArea[0]=tempbyte;
tempbyte= SelectArea[1];
SelectArea[1]=MoveArea[1];
MoveArea[1]=tempbyte;
this.selected = false;
repaint();
}
增加处理悔棋的按钮:
HuaRongDaoMidlet.java:
private final static Command CMD_UNDO = new Command("上一步", Command.SCREEN, 1);
......
else if (c == CMD_UNDO) {//处理“上一步”
logic.unMove();
}
......
注意:A.在NetBeans当中,有许多方便的按钮,当编辑代码的时候,代码编辑区上面的最右边有两个注释和反注释的按钮,和VS的功能一样,只是没有/* */形式的注释,还有缩进反缩进等按钮,编辑很方便,而且当函数参数输入完成后,直接按";"就可以自动在行尾加入分号。同样,可以加入标签: BookMark,使得快速回到上一个位置成为可能。
B.NetBeans把搜索也加到这个工具栏里面,可以搜索,标记,非常方便。
(2).改变移动方式,程序提供的移动方块的方式非常难操作,我希望能够点一下方块他就智能地自己寻找能够移动的位置。这里还有一点需要注意,就是
不能绕弯,也就是A-B-A-B这样来回走,如果还有其他走法,因此算法中加入了许多判断,但是比原来的代码要简单清晰易懂,操作也比原来简单多了。
代码如下:
public class ControlLogic extends Canvas implements CommandListener {
public static final byte DIRECTION_UP = (byte) '1'; //方向常量
public static final byte DIRECTION_DOWN = (byte) '2'; //方向常量
public static final byte DIRECTION_LEFT = (byte) '3'; //方向常量
public static final byte DIRECTION_RIGHT = (byte) '4'; //方向常量
private byte[] currentCursor = new byte[4]; //当前光标所在位置,四个参数分别是X,Y,width,height.
private byte[] nextCursor = new byte[4]; //要移动到的位置的光标区域,参数同上.
private Map MyMap = new Map();//地图类
private int level;//当前的关
public int moves=0;//所用的步数.
private History history = new History();
public boolean isWin=false;
public ControlLogic(int gameLevel) {//构造函数
try {
this.level = gameLevel;