4.改进程序 (1)记录历史步骤,以便可以悔棋: 记录历史步骤的方法是实现一个History类,这个类实际上是一个Vector的封装,用来保存每一步的走法,走法被定义为一个包含5个元素的数组,分别是 X,Y,width,height,direction. 这里需要注重的是,Java当中实际上是没有局部变量的,每一个局部变量都需要new出来,所以在使用Vector的addElement()函数时,由于它是传引用, 我们必须要新创建一个element,而不能使用全局的,因为假如使用全局的,下一次addElement时,会因为该变了变量的值使得刚才加到Vector中的值也改 变了。 import java.util.Vector; /** * * @author lin */ public class History { private static Vector steps = new Vector(); /** Creates a new instance of History */ public History() { clear(); } public static void addStep(Object step){ steps.addElement(step); } public static void removeLastStep(){ steps.removeElement(steps.lastElement()); } public static Object getLastStep(){ return steps.lastElement(); } public static Object getStepAt(int index){ return steps.elementAt(index); } public static int getSize(){ return steps.size(); } private void clear(){ if (!steps.isEmpty()) steps.removeAllElements(); } } 在每一步移动结束后,记录这一步的信息: ContorlLogic.java: Move() ...... moves++;// 增加移动的步骤 byte[] step = new byte[5]; //五个参数分别为,前四个和SelectArea一样,最后一个表示上1,下2,左3,右4。 //将此次移动记录到历史记录当中; step[0]= this.SelectArea[0]; step[1]= this.SelectArea[1]; step[2]= this.SelectArea[2]; step[3]= this.SelectArea[3]; step[4]= this.getMoveDirection(); history.addStep(step); ...... 增加一个悔棋的按钮,增加一个unMove()函数: public void unMove(){ if ( moves == 0 ) return; byte[] step = new byte[5]; //五个参数分别为,前四个和SelectArea一样,最后一个表示上1,下2,左3,右4。 step = (byte []) history.getLastStep();//取得上一步移动 history.removeLastStep();//减少一步; moves--; for (int i= 0; i< 4;i++){ this.MoveArea[i] = step[i];//重设MoveArea this.SelectArea[i] = step[i];//重设SelectArea } if (step[4] == 1){ this.SelectArea[1] = (byte) (step[1]-1); this.loc[1]++; } else if (step[4] == 2){ this.SelectArea[1] = (byte) (step[1]+1); this.loc[1]--; } else if (step[4] == 3){ this.SelectArea[0] = (byte) (step[0]-1); this.loc[0]++; } else if (step[4] == 4){ this.SelectArea[0] = (byte) (step[0]+1); this.loc[0]--; } //移动回来. byte[][] temp = new byte[this.SelectArea[3]][this.SelectArea[2]]; //复制要移动的区域,因为这块区域可能会被覆盖掉 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; isWin=false; nbInit();//NetBeans定义的初始化函数 }catch (Exception e) { e.printStackTrace(); } } private void Init_game(){ //初始化游戏,读取地图,设置选择区域,清空要移动到的区域 this.currentCursor = MyMap.read_map(this.level);//读取地图文件,并返回光标的初始位置 //0为水平位置,1为竖直位置, 2为宽,3为高. nextCursor[0]=currentCursor[0]; //初始化要移动到的区域 nextCursor[1]=currentCursor[1]; nextCursor[2]=currentCursor[2]; nextCursor[3]=currentCursor[3]; } private void nbInit() throws Exception {//NetBeans定义的初始化函数 //初始化实例变量 Images.init();//初始化图片常量 Init_game();//初始化游戏,读取地图,设置选择区域,清空要移
|