In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

flash on 2012-3-7

Get Adobe Flash player
by tepe 09 Mar 2012
/**
 * Copyright tepe ( http://wonderfl.net/user/tepe )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/kAv0
 */

package {
    import flash.display.Sprite;
    import flash.geom.*;
    import flash.events.*;
    import flash.text.*;
    public class FlashTest extends Sprite {
        private var node:Array = new Array();
        private var tf:TextField;
        public function FlashTest() {
            // write as3 code here..
            init();
        }
        private function init():void{
            func2();
            tf = new TextField();
            addChild(tf);
            tf.height = 400;
            tf.width = 300;
            
            addEventListener(KeyboardEvent.KEY_DOWN,onKey);
            

        }
        
        //状態初期化
        private function func2():void{                         
                node = setNode(10);              
                select = null;
                select = new Array();
                for(var j:int=0;j<10;j++){
                    var ar3:Array = [0,1,2,3,4,5,6,7,8,9];
                    select[j] = ar3;
                    select[j] = shuffle(select[j]);
                }
                drawline(select[0],node);
        }

        
        private var select:Array;//選択肢
        private function onKey(e:KeyboardEvent):void{
            if(e.keyCode == 90){
                func2();
                drawline(select[0],node);
                select = null;
                select = new Array();
                //tf.text = ar.toString();
                
                for(var j:int=0;j<10;j++){
                    var ar3:Array = [0,1,2,3,4,5,6,7,8,9];
                    select[j] = ar3;
                    select[j] = shuffle(select[j]);
                    tf.appendText("\n"+select[j].toString());
                    tf.appendText(" "+String(Math.floor( func1(select[j]) )));
                }
            }
            if(e.keyCode ==65){
                act();

            }
            if(e.keyCode == 88){
                func3(select);
                //func5();
                act();
            }
            if(e.keyCode == 67){
                //func5();
                rankingSort();
                func4(select);
                act();
            }




        }
        
        //淘汰プロセス
        private function func3(select:Array):void{
            //劣る方を消す
            var n1:int = Math.random()*select.length;
            var n2:int = Math.random()*select.length;
            var s1:Number = func1(select[n1]);
            var s2:Number = func1(select[n2]);
            if(s1 >= s2)select = select.splice(n2,1);
            else if(s1 < s2)select = select.splice(n1,1);
            
        }
        


        
        //交叉プロセス
        private function func4(select:Array):void{
            var n1:int = Math.random()*select.length/4;
            var n2:int = Math.random()*select.length/2;
            var arr:Array = new Array();
            var A:Array = select[n1];
            var B:Array = select[n2];
            var genePosArr:Array = new Array();
            arr.push(0);
            if(n1 != n2){
                for(var i:int=1;i<node.length;i++){
                    if(Math.random()<0.8){//n1
                        if(arr.indexOf(A[i],0)==-1)arr[i] = A[i];
                        else genePosArr.push(i);    // 埋められなかった場所を記憶する
                        
                    }
                    else{//n2
                        if(arr.indexOf(B[i],0)==-1)arr[i] = B[i];
                        else genePosArr.push(i);    // 埋められなかった場所を記憶する
                    }
                }
                var nothingGene:Array = new Array();    // 欠けている遺伝子情報を格納する配列
        
                // 欠けている遺伝子情報を調べる(どの数字がないか調べる)
                for (var k:int = 0; k < node.length; k++) {
                    if(arr.indexOf(k, 0) == -1) nothingGene.push(k);
                }
        
                nothingGene = shuffle(nothingGene);    // 位置をシャッフル
        
                // 欠けている遺伝子を補う
                for (var l:int = 0; l < genePosArr.length; l++) {
                    arr[genePosArr[l]] = nothingGene[l];
                }
                if(Math.random()<0.2*nothingGene.length){
                    for (var m:int = 1; m < 2; m++){
                        var rundomNum:int = Math.random() * (node.length-1) + 1;
    
                        var temp:int = arr[rundomNum];
                        arr[rundomNum] = arr[m];
                        arr[m] = temp;
                    }
                }                
                select.push(arr);

            }

        }
        
        private function rankingSort():void{
            var total:Number = 0;
            var score:Array = new Array();
            var newArr:Array = new Array();
            for (var j:int = 0; j < select.length; j++) {
                total = func1(select[j]);
                score[j] = {score:total,id:j};
            }
            score.sortOn("score",Array.NUMERIC);
            for(var i:int = 0;i<select.length;i++){
                newArr.push(select[score[i].id]);
            }
            select = newArr; 
        }
        
 
        private function act():void{
            graphics.clear();
                for(var i:int=0;i<node.length;i++){
                
                    graphics.lineStyle(1,0x0);
                    graphics.beginFill(0x00ffff);
                    graphics.drawCircle(node[i].x,node[i].y,5);
                    graphics.endFill();
                    //var n:int = Math.floor(Math.random()*10);
                    //drawline(select[n]);
                    
                }
                var n:int = Math.random()*select.length;
                drawline(select[n],node);
                tf.text = select[n].toString();
                for(i=0;i<select.length;i++){
                    tf.appendText("\n"+select[i].toString());
                    tf.appendText(" "+String(Math.floor( func1(select[i]) )));
                }
        }


        //ノード描画
        //ノード配列作成
        private function setNode(n:int = 10):Array{
            var ar2:Array = new Array();
            graphics.clear();
            node = null;
            node = new Array();
             for(var i:int=0;i<n;i++){
                var pos:Point  = new Point(Math.random()*400,Math.random()*400);
                //node.push(pos)
                graphics.lineStyle(1,0x0);
                graphics.beginFill(0x00ffff);
                graphics.drawCircle(pos.x,pos.y,5);
                graphics.endFill();
                
                ar2.push(pos);
            }
            return ar2;
        }

        
        //遺伝子生成
        private function shuffle(arr:Array):Array{
            var num:int = arr.length;    // 配列の数

            for (var i:int = 1; i < num; i++)
            {
                var rundomNum:int = Math.random() * (num-1)+1;
    
                var temp:int = arr[rundomNum];
                arr[rundomNum] = arr[i];
                arr[i] = temp;
            }
        
            return arr;
        }
 
        private function drawline(arr:Array,node:Array):void{
            //graphics.clear();
            graphics.lineStyle(1,0xff0000);
            graphics.moveTo(node[arr[arr.length-1]].x,node[arr[arr.length-1]].y);
            for(var i:int=0;i<arr.length;i++){
                graphics.lineTo(node[arr[i]].x,node[arr[i]].y);
            }

        }
        //距離を測る
        private function func1(arr:Array):Number{
            var total:Number = 0;
            total += Point.distance( node[arr[9]], node[arr[0]] );
            for(var i:int=0;i<9;i++){
                total += Point.distance( node[arr[i]], node[arr[i+1]] );
            }

            return total;
        }




    }
}