在NetBeans平臺上開發J2ME遊戲實例講解(第三部分)

 在NetBeans平臺上開發J2ME遊戲實例講解(第三部分)

[email protected] 林剛 2005.07{ 引用需要註明出處作者}

4.改進程序

 

(1)記錄歷史步驟,以便可以悔棋:

記錄歷史步驟的方法是實現一個History類,這個類實際上是一個Vector的封裝,用來保存每一步的走法,走法被定義爲一個包含5個元素的數組,分別是

X,Y,width,height,direction.

這裏需要注意的是,Java當中實際上是沒有局部變量的,每一個局部變量都需要new出來,所以在使用VectoraddElement()函數時,由於它是傳引用,

我們必須要新創建一個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;

                }

            }

        }

       

        //交換SelectAreaMoveArea

        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();//初始化遊戲,讀取地圖,設置選擇區域,清空要移動到的區域

        //setCommandListener(this);//添加命令監聽,這是Displayable的實例方法

        //addCommand(CMD_PAUSE);//添加“暫停”按鈕

    }

 

    public void commandAction(Command command, Displayable displayable) {

        //命令處理函數

 

    }

 

    protected void paint(Graphics g) {       

        //畫圖函數,用於繪製用戶畫面,即顯示圖片,勾畫選中區域

        try {

            g.drawImage(Images.image_Frame, 0, 0,Graphics.TOP | Graphics.LEFT);//畫背景

            MyMap.draw_map(g);//按照地圖內容畫圖

           

            g.setColor(0, 255, 0);//綠色畫筆

            g.drawRect(this.currentCursor[0] * Images.UNIT + Images.LEFT,

                this.currentCursor[1] * Images.UNIT + Images.TOP,

                this.currentCursor[2] * Images.UNIT,

                this.currentCursor[3] * Images.UNIT);//畫出選擇區域,

            g.setColor(255,255,255);//恢復畫筆顏色

        }catch (Exception ex) {

        }

 

        //顯示步數

        Draw.paint(g,String.valueOf(moves), 60, 15, Graphics.BASELINE | Graphics.HCENTER);

        if ( win()) {

            isWin=true;

            Draw.paint(g,"你贏了!", 60, 70, Graphics.TOP | Graphics.HCENTER);

        }

    }

 

    private void setRange() {

        //設置移動後能夠選中的區域

        //調整當前光標位置到地圖的主位置,即記錄人物信息的位置

        if (this.MyMap.Grid[this.currentCursor[1]][this.currentCursor[0]] == Images.DLEFT) {

            this.currentCursor[0] -= 1;//向左調

        }else if (this.MyMap.Grid[this.currentCursor[1]][this.currentCursor[0]] == Images.DUP) {

            this.currentCursor[1] -= 1;//向上調

        }else if (this.MyMap.Grid[this.currentCursor[1]][this.currentCursor[0]] == Images.DLEFTUP) {

            this.currentCursor[0] -= 1;//向左調

            this.currentCursor[1] -= 1;//向上調

        }

       

        if (this.currentCursor[0] + 1 < Images.WIDTH) {

            this.currentCursor[2] = this.MyMap.Grid[this.currentCursor[1]][this.currentCursor[0] + 1] != (byte) '1' ?

            (byte)1 : (byte)2;//是橫向麼?

        }else {

            this.currentCursor[2] = 1;

        }

        //設置光標的高度

        if (this.currentCursor[1] + 1 < Images.HEIGHT) {

            this.currentCursor[3] = this.MyMap.Grid[this.currentCursor[1] + 1][this.currentCursor[0]] != (byte) '2' ?

            (byte)1 : (byte)2;//是縱向麼?

        }else {

            this.currentCursor[3] = 1;

        }

    }

 

    private boolean setMoveRange() {

        //設置要移動到的區域,能夠移動返回true,否則返回false

        for (int i = 0; i < this.nextCursor[2]; i++) {

            for (int j = 0; j < this.nextCursor[3]; j++) {

                if (this.nextCursor[1] + j >= Images.HEIGHT ||

                    this.nextCursor[0] + i >= Images.WIDTH ||

                    (!isInRange(this.nextCursor[0] + i, this.nextCursor[1] + j) &&

                    this.MyMap.Grid[this.nextCursor[1] + j][this.nextCursor[0] + i] !=

                    Images.BLANK)) {

                return false;

                }

            }

        }

        return true;

    }

 

    private boolean isInRange(int x, int y) {

        //判斷給定的(xy)點是否在選定區域之內,x是水平座標,y是豎直座標

        if (x >= this.currentCursor[0] &&

            x < this.currentCursor[0] + this.currentCursor[2] &&

            y >= this.currentCursor[1] &&

            y < this.currentCursor[1] + this.currentCursor[3]) {

            return true;

        }else {

            return false;

        }

    }

 

    private boolean isInRange2(int x, int y) {

        //判斷給定的(xy)點是否在要移動到的區域之內,x是水平座標,y是豎直座標

        if (x >= this.nextCursor[0] &&

            x <  this.nextCursor[0] + this.nextCursor[2] &&

            y >= this.nextCursor[1] &&

            y <  this.nextCursor[1] + this.nextCursor[3]) {

            return true;

        }else {

            return false;

        }

    }

   

    private boolean canMove( int direction ){

        nextCursor[0]=currentCursor[0];               

        nextCursor[1]=currentCursor[1];               

        nextCursor[2]=currentCursor[2];               

        nextCursor[3]=currentCursor[3];       

        switch (direction){

            case DIRECTION_UP:

                if (this.currentCursor[1] - 1 >= 0) {//向上還有移動空間

                    this.nextCursor[1]--;//向上移動一下

                    if (!setMoveRange()){ //不能移動

                        this.nextCursor[1]++;//退回來

                        return false;

                    }

                    else {

                        this.nextCursor[1]++;//退回來

                        return true;

                    }

                }

                else

                    return false;

            case DIRECTION_DOWN:

                if (this.currentCursor[1] + 1 < Images.HEIGHT) {//向下還有移動空間

                    this.nextCursor[1]++;//向下移動一下

                    if (!setMoveRange()){ //不能移動

                        this.nextCursor[1]--;//退回來

                        return false;

                    }

                    else {

                        this.nextCursor[1]--;//退回來

                        return true;

                    }

                }

                else

                    return false;

            case DIRECTION_LEFT:

                if (this.currentCursor[0] - 1 >= 0) {//向左還有移動空間

                    this.nextCursor[0]--;//向左移動一下

                    if (!setMoveRange()){ //不能移動

                        this.nextCursor[0]++;//退回來

                        return false;

                    }

                    else {

                        this.nextCursor[0]++;//退回來

                        return true;

                    }

                }

                else

                    return false;

            case DIRECTION_RIGHT:

                if (this.currentCursor[0] + 1 < Images.WIDTH) {//向右還有移動空間

                    this.nextCursor[0]++;//向右移動一下

                    if (!setMoveRange()){ //不能移動

                        this.nextCursor[0]--;//退回來

                        return false;

                    }

                    else {

                        this.nextCursor[0]--;//退回來

                        return true;

                    }

                }

                else

                    return false;

            default:

                return false;

        }

    }

 

    protected void keyPressed(int keyCode) {

        //處理按下鍵盤的事件,這是Canvas的實例方法

        switch (getGameAction(keyCode)) {//將按鍵的值轉化成方向常量

            case Canvas.UP://向上

                if (this.currentCursor[1] - 1 >= 0) {//向上還有移動空間

                    this.currentCursor[1]--;//向上移動一下

                    setRange();

                    repaint();//重新繪圖

                }

                break;

            case Canvas.DOWN://向下

                if (this.currentCursor[1] + 1 < Images.HEIGHT) {//向下還有移動空間

                    this.currentCursor[1]+=currentCursor[3];//向下移動一下

                    setRange();

                    repaint();//重新繪圖

                }

                break;

            case Canvas.LEFT://向左

                if (this.currentCursor[0] - 1 >= 0) {//向左還有移動空間

                    this.currentCursor[0]--;//向左移動方塊長度

                    setRange();

                    repaint();//重新繪圖

                }

            break;

            case Canvas.RIGHT://向右

                //看向右還能否移動.

                if (this.currentCursor[0] + 1 < Images.WIDTH) {//向右還有移動空間

                    this.currentCursor[0]+=currentCursor[2];//向右移動一下

                    setRange();

                    repaint();//重新繪圖

                }

                break;

            case Canvas.FIRE:

               

                nextCursor[0]=currentCursor[0];               

                nextCursor[1]=currentCursor[1];                

                nextCursor[2]=currentCursor[2];               

                nextCursor[3]=currentCursor[3]; 

                if( moves == 0 ){//第一步

                    if ( canMove(DIRECTION_UP) )

                        this.nextCursor[1]--;//向上移動一下

                    else if ( canMove(DIRECTION_DOWN) )

                        this.nextCursor[1]++;//向下移動一下

                    else if ( canMove(DIRECTION_LEFT) )

                        this.nextCursor[0]--;//向左移動一下

                    else if ( canMove(DIRECTION_RIGHT) )

                        this.nextCursor[0]++;//向右移動一下

                    else

                        break;

                    Move();

                    repaint();

                    break;

                }

                

                byte[] lastStep = new byte[5]; //五個參數分別爲,前四個和CurrentCursor一樣,最後一個表示上1,下2,左3,右4

                lastStep = (byte []) history.getLastStep();//取得上一步移動

             

                //向上

                if (canMove(DIRECTION_UP)) {

                    if ( ( nextCursor[0] == lastStep[0] && nextCursor[1]-1 == lastStep[1] && lastStep[4] == DIRECTION_DOWN) && //如果是走回頭路

                         ( !(canMove(DIRECTION_DOWN))&&!(canMove(DIRECTION_LEFT))&&!(canMove(DIRECTION_RIGHT))) || //而且只有這一種走法

                         !( nextCursor[0] == lastStep[0] && nextCursor[1]-1 == lastStep[1] && lastStep[4] == DIRECTION_DOWN) )//或者不是走原路

                        this.nextCursor[1]--;//向上移動一下

                    else if (canMove(DIRECTION_DOWN))

                        this.nextCursor[1]++;//向下移動一下

                    else if (canMove(DIRECTION_LEFT))

                        this.nextCursor[0]--;//向左移動一下

                    else if (canMove(DIRECTION_RIGHT))

                        this.nextCursor[0]++;//向右移動一下

                    Move();

                    repaint();

                    break;

                }

               

                 //向下

                if (canMove(DIRECTION_DOWN)) {

                    if ( ( nextCursor[0] == lastStep[0] && nextCursor[1]+1 == lastStep[1] && lastStep[4] == DIRECTION_UP) && //如果是走回頭路

                         ( !(canMove(DIRECTION_UP))&&!(canMove(DIRECTION_LEFT))&&!(canMove(DIRECTION_RIGHT)))||//而且只有這一種走法

                         !( nextCursor[0] == lastStep[0] && nextCursor[1]+1 == lastStep[1] && lastStep[4] == DIRECTION_UP) )//或者不是走原路

                        this.nextCursor[1]++;//向下移動一下

                    else if (canMove(DIRECTION_UP))

                        this.nextCursor[1]--;//向上移動一下

                    else if (canMove(DIRECTION_LEFT))

                        this.nextCursor[0]--;//向左移動一下

                    else if (canMove(DIRECTION_RIGHT))

                        this.nextCursor[0]++;//向右移動一下

 

                    Move();

                    repaint();

                    break;

                }

               

                 //向左

                if (canMove(DIRECTION_LEFT)) {

                    if ( ( nextCursor[0]-1 == lastStep[0] && nextCursor[1] == lastStep[1] && lastStep[4] == DIRECTION_RIGHT) && //如果是走回頭路

                         ( !(canMove(DIRECTION_UP))&&!(canMove(DIRECTION_DOWN))&&!(canMove(DIRECTION_RIGHT)))||//而且只有這一種走法

                         !( nextCursor[0]-1 == lastStep[0] && nextCursor[1] == lastStep[1] && lastStep[4] == DIRECTION_RIGHT) )//或者不是走原路

                        this.nextCursor[0]--;//向左移動一下

                    else if (canMove(DIRECTION_UP))

                        this.nextCursor[1]--;//向上移動一下

                    else if (canMove(DIRECTION_DOWN))

                        this.nextCursor[1]++;//向下移動一下

                    else if (canMove(DIRECTION_RIGHT))

                        this.nextCursor[0]++;//向右移動一下

                    Move();

                    repaint();

                    break;

                }

                 //向右

                if (canMove(DIRECTION_RIGHT)) {

                    if ( ( nextCursor[0]+1 == lastStep[0] && nextCursor[1] == lastStep[1] && lastStep[4] == DIRECTION_LEFT) && //如果是走回頭路

                         ( !(canMove(DIRECTION_UP))&&!(canMove(DIRECTION_DOWN))&&!(canMove(DIRECTION_LEFT))) ||//而且只有這一種走法

                         !( nextCursor[0]+1 == lastStep[0] && nextCursor[1] == lastStep[1] && lastStep[4] == DIRECTION_LEFT) )//或者不是走原路

                        this.nextCursor[0]++;//向右移動一下

                    else if (canMove(DIRECTION_UP))

                        this.nextCursor[1]--;//向上移動一下

                    else if (canMove(DIRECTION_DOWN))

                        this.nextCursor[1]++;//向下移動一下

                    else if (canMove(DIRECTION_LEFT))

                        this.nextCursor[0]--;//向左移動一下

                    Move();

                    repaint();

                    break;

                }

        }

    }

 

    private boolean win(){

        //判斷是否已經救出了曹操

        if ( this.MyMap.Grid[Images.HEIGHT - 2 ][Images.WIDTH - 3 ] == Images.CAOCAO )

        {

            return true;

        }

        else

            return false;

    }

 

    private void PrintGrid(String a) {

        //打印當前地圖的內容,用於調試

        System.out.println(a);

        for (int i = 0; i < Images.HEIGHT; i++) {

            for (int j = 0; j < Images.WIDTH; j++) {

                System.out.print( (char)this.MyMap.Grid[i][j]);

            }

            System.out.println("");

        }

    }

 

    private void PrintHistory(String a) {

        //打印當前地圖的內容,用於調試

        byte [] temp =new byte[5];

        System.out.println(a);

        for (int i = 0; i < this.history.getSize(); i++) {

            temp = (byte [])this.history.getStepAt(i);

            for (int j = 0; j < 5; j++) {

                System.out.print(String.valueOf(temp[j]));

            }

            System.out.println("");

        }

    }

   

    private byte getMoveDirection(){

        byte direction=0;

        if ( this.currentCursor[0]<this.nextCursor[0])

            direction=DIRECTION_RIGHT;//向右移動;

        if ( this.currentCursor[0]>this.nextCursor[0])

            direction=DIRECTION_LEFT;//向左移動;

        if ( this.currentCursor[1]<this.nextCursor[1])

            direction=DIRECTION_DOWN;//向下移動;

        if ( this.currentCursor[1]>this.nextCursor[1])

            direction=DIRECTION_UP;//向上移動;

       

        return direction;

    }

 

    public void unMove(){

        if ( moves == 0 )

            return;

       

        //因爲unMove()的上一步肯定是可行的,所以不用setMoveRange().

        byte[] step = new byte[5]; //五個參數分別爲,前四個和CurrentCursor一樣,最後一個表示上1,下2,左3,右4

        step = (byte []) history.getLastStep();//取得上一步移動

        history.removeLastStep();//減少一步;

        moves--;

       

        for (int i= 0; i< 4;i++){

            this.nextCursor[i] = step[i];//重設nextCursor  

            this.currentCursor[i] = step[i];//重設currentCursor

        }

 

        //根據上一步的方向,反方向移動

        if (step[4] == DIRECTION_UP)

            this.currentCursor[1] = (byte) (step[1]-1);

        else if (step[4] == DIRECTION_DOWN)

            this.currentCursor[1] = (byte) (step[1]+1);

        else if (step[4] == DIRECTION_LEFT)

            this.currentCursor[0] = (byte) (step[0]-1);

        else if (step[4] == DIRECTION_RIGHT)

            this.currentCursor[0] = (byte) (step[0]+1);

   

        //移動回來.

        byte[][] temp = new byte[this.currentCursor[3]][this.currentCursor[2]];

        //複製要移動的區域,因爲這塊區域可能會被覆蓋掉

        for (int i = 0; i < this.currentCursor[2]; i++) {

            for (int j = 0; j < this.currentCursor[3]; j++) {

                temp[j][i] = this.MyMap.Grid[this.currentCursor[1] +j][this.currentCursor[0] + i];

            }

        }

        //將要移動的區域移動到剛選中的區域(即要移動到的區域)

        for (int i = 0; i < this.currentCursor[2]; i++) {

            for (int j = 0; j < this.currentCursor[3]; j++) {

                this.MyMap.Grid[this.nextCursor[1] + j][this.nextCursor[0] + i] = temp[j][i];

            }

        }

        //將要移動的區域中無用內容置成空白

        for (int i = 0; i < this.currentCursor[3]; i++) {

            for (int j = 0; j < this.currentCursor[2]; j++) {

                if (!isInRange2(this.currentCursor[0] + j,this.currentCursor[1] + i)) {

                    //該點是不在要移動到的區域之內,需置空

                    this.MyMap.Grid[this.currentCursor[1] + i][this.currentCursor[0] + j] = Images.BLANK;

                }

            }

        }

       

        //交換currentCursornextCursor

        this.currentCursor[0]=nextCursor[0];

        this.currentCursor[1]=nextCursor[1];

        repaint();

    }

   

    private void Move() {

        if ( this.currentCursor[0]==this.nextCursor[0] && this.currentCursor[1]==this.nextCursor[1] )

            return;  //不需要移動就返回.

       

        //將要移動的區域移動到剛選中的區域

        if (this.nextCursor[0] == -1 || this.nextCursor[1] == -1 ||

            this.currentCursor[0] == -1 || this.currentCursor[1] == -1) {//沒有選中區域

        }

        else {//已經選中了要移動的區域和要移動到的區域

            byte[][] temp = new byte[this.currentCursor[3]][this.currentCursor[2]];

            //複製要移動的區域,因爲這塊區域可能會被覆蓋掉

            for (int i = 0; i < this.currentCursor[2]; i++) {

                for (int j = 0; j < this.currentCursor[3]; j++) {

                    temp[j][i] = this.MyMap.Grid[this.currentCursor[1] +j][this.currentCursor[0] + i];

                }

            }

            // 調試信息

            //將要移動的區域移動到剛選中的區域(即要移動到的區域)

            for (int i = 0; i < this.currentCursor[2]; i++) {

                for (int j = 0; j < this.currentCursor[3]; j++) {

                    this.MyMap.Grid[this.nextCursor[1] + j][this.nextCursor[0] + i] = temp[j][i];

                }

            }

            //PrintGrid("2");// 調試信息

            //將要移動的區域中無用內容置成空白

            for (int i = 0; i < this.currentCursor[3]; i++) {

                for (int j = 0; j < this.currentCursor[2]; j++) {

                    if (!isInRange2(this.currentCursor[0] + j,this.currentCursor[1] + i)) {

                        //該點是不在要移動到的區域之內,需置空

                        this.MyMap.Grid[this.currentCursor[1] + i][this.currentCursor[0] + j] = Images.BLANK;

                    }else {

                    }

                }

            }

           

            moves++;// 增加移動的步驟

           

            byte[] step = new byte[5]; //五個參數分別爲,前四個和SelectArea一樣,最後一個表示上1,下2,左3,右4

            //將此次移動記錄到歷史記錄當中;

            step[0]= this.currentCursor[0];

            step[1]= this.currentCursor[1];

            step[2]= this.currentCursor[2];

            step[3]= this.currentCursor[3];

            step[4]= this.getMoveDirection();

           

            history.addStep(step);

           

            //PrintHistory("record a step");// 調試信息

            //PrintGrid("3");// 調試信息

            this.currentCursor[0] = this.nextCursor[0];//重置選中位置的水平座標

            this.currentCursor[1] = this.nextCursor[1];//重置選中位置的豎直座標

            this.nextCursor[0] = -1;//清空要移動到的位置

            this.nextCursor[1] = -1;//清空要移動到的位置

            this.nextCursor[2] = 0;//清空要移動到的位置

            this.nextCursor[3] = 0;//清空要移動到的位置

        }

    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章