九方格游戏的代码(此逻辑算法适用于任意方格数)

package view
{
    /**
     * @author zoe
     *
     */
    import flash.display.Sprite;
    import flash.text.TextField;
    
    public class Rect extends Sprite
    {
        public var position:int;    
        private var txt:TextField;
        
        public function Rect()
        {
            super();
            init();
        }
        
        private function init():void
        {
            this.buttonMode = true;
            
            this.graphics.lineStyle(2,0x220089);
            this.graphics.beginFill(0xff00ff);
            this.graphics.drawRect(0,0,50,50);
            this.graphics.endFill();        
            
            txt = new TextField();
            addChild(txt);            
        }
        
        public function setInfo(temp:String):void
        {
            txt.text = temp;
        }
        
    }
}

然后主类

package
{
    /**
     * @author zoe
     *
     */
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    
    import view.Rect;
    
    [SWF(width = 700,height = 500)]
    public class Main extends Sprite
    {
        private var lie:int = 3;
        private var hang:int = 3;
        
        private var crtEmptyPosition:int;
        
        private var container:Sprite;
        
        private var lieTxt:TextField;
        private var hangTxt:TextField;
        private var inputHangTip:TextField;
        private var inputLieTip:TextField;
        
        private var txtContainer:Sprite;
        
        private var reStartBtn:TextField;
        
        private var arr:Array = ["a","b","c","d","e","f","g","h"];
        
        public function Main()
        {
            
            init();
        }
        
        private function init():void
        {
            crtEmptyPosition = hang*lie-1;
            
            txtContainer = new Sprite();
            addChild(txtContainer);
            txtContainer.x = 50;
            txtContainer.y = 50;
            
            creatTxt();
            creatRect();
            creatBtn();
        }
        
        private function creatBtn():void
        {
            reStartBtn = new TextField();
            reStartBtn.background = true;
            reStartBtn.backgroundColor = 0x771159;
            reStartBtn.border = true;
            reStartBtn.borderColor = 0x00ff00;
            reStartBtn.type = TextFieldType.DYNAMIC;
            reStartBtn.text = "点击此按钮重新生成";
            reStartBtn.width = 130;
            reStartBtn.height =25;
            reStartBtn.mouseEnabled = true;
            reStartBtn.selectable = false;
            addChild(reStartBtn);
            reStartBtn.addEventListener(MouseEvent.CLICK,btnClickHandler);
        }
        
        protected function btnClickHandler(event:MouseEvent):void
        {
            removeChild(container);
            
            lie = int(lieTxt.text);
            hang = int(hangTxt.text);
            crtEmptyPosition = hang*lie-1;
            
            creatRect();
            
        }
        
        private function creatTxt():void
        {
            inputHangTip = new TextField();
            inputHangTip.type = TextFieldType.DYNAMIC;
            inputHangTip.background = true;
            inputHangTip.backgroundColor = 0x225533;
            inputHangTip.mouseEnabled = false;
            inputHangTip.selectable = false;
            inputHangTip.text = "请输入行数";
            inputHangTip.width = 70;
            inputHangTip.height = 25;
            inputHangTip.x = 20;
            inputHangTip.y = 20;
            txtContainer.addChild(inputHangTip);
            
            inputLieTip = new TextField();
            inputLieTip.type = TextFieldType.DYNAMIC;
            inputLieTip.background = true;
            inputLieTip.backgroundColor = 0x225533;
            inputLieTip.mouseEnabled = false;
            inputLieTip.selectable = false;
            inputLieTip.text = "请输入列数";
            inputLieTip.width = 70;
            inputLieTip.height = 25;
            inputLieTip.x = 20;
            inputLieTip.y = 60;
            txtContainer.addChild(inputLieTip);
            
            hangTxt = new TextField();
            hangTxt.border = true;
            hangTxt.text = "3";
            hangTxt.borderColor = 0x000000;
            hangTxt.height = 25;
            hangTxt.x = 100;
            hangTxt.y = 20;
            hangTxt.type = TextFieldType.INPUT;
            txtContainer.addChild(hangTxt);
            
            lieTxt = new TextField();
            lieTxt.border = true;
            lieTxt.text = "3";
            lieTxt.borderColor = 0x000000;
            lieTxt.height = 25;
            lieTxt.x = 100;
            lieTxt.y = 60;
            lieTxt.type = TextFieldType.INPUT;
            txtContainer.addChild(lieTxt);
        }
        
        private function creatRect():void
        {
            var len:int = hang*lie-1;
            container = new Sprite();
            addChild(container);
            container.x = 100;
            container.y = 180;
            for(var i:int = 0;i<len;i++)
            {
                var rect:Rect = new Rect();                
                rect.position = i;
                rect.x = (i%lie)*rect.width;
                rect.y = int(i/lie)*rect.height;
                container.addChild(rect);
                rect.setInfo(arr[i]);
                rect.addEventListener(MouseEvent.CLICK,rectClickHandler);
            }
        }
        
        protected function rectClickHandler(event:MouseEvent):void
        {
            var crtRect:Rect = Rect(event.currentTarget);
            if(checkUp(crtRect))
            {
                return;
            }
            if(checkDown(crtRect))
            {
                return;
            }
            if(checkLeft(crtRect))
            {
                return;
            }
            if(checkRight(crtRect))
            {
                return;
            }
        }
        
        private function checkUp(tempRect:Rect):Boolean
        {
            var crtRectPosition:int = tempRect.position;
            if(crtRectPosition >lie-1)
            {
                if(crtEmptyPosition == crtRectPosition-lie)
                {
                    crtEmptyPosition = crtRectPosition;
                    tempRect.y -= tempRect.height;
                    tempRect.position = crtRectPosition -lie;
                    return true;
                }
            }
            
            return false;
        }
        
        private function checkDown(tempRect:Rect):Boolean
        {
            
            var crtRectPosition:int = tempRect.position;
            if(crtRectPosition <(hang*lie)-lie)
            {                
                if(crtEmptyPosition == crtRectPosition +lie)
                {
                    crtEmptyPosition = crtRectPosition;
                    tempRect.y += tempRect.height;
                    tempRect.position = crtRectPosition + lie;
                    return true;
                }
            }
            
            return false;
        }
        
        private function checkLeft(tempRect:Rect):Boolean
        {
            var crtRectPosition:int = tempRect.position;
            if(crtRectPosition%lie !=0)
            {
                if(crtEmptyPosition == crtRectPosition-1)
                {
                    crtEmptyPosition = crtRectPosition;
                    tempRect.x -= tempRect.width;
                    tempRect.position = crtRectPosition-1;
                    return true;
                }
            }
            return false;
        }
        
        private function checkRight(tempRect:Rect):Boolean
        {
            var crtRectPosition:int = tempRect.position;
            if(crtRectPosition%lie != lie-1)
            {
                if(crtEmptyPosition == crtRectPosition +1)
                {
                    crtEmptyPosition = crtRectPosition;
                    tempRect.x += tempRect.width;
                    tempRect.position = crtRectPosition+1;
                    return true;
                }
            }
            
            return false;
        }
    }
}
原文地址:https://www.cnblogs.com/kuailezoe/p/2832101.html