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 2011-5-17

Get Adobe Flash player
by kihon 17 May 2011
    Embed
package
{
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFormat;
 
    public class Main extends Sprite
    {
        public static const WIDTH:int = 8;
        public static const HEIGHT:int = 8;
 
        public function Main()
        {
            var container:Sprite = new Sprite();
            container.x = 50;
            container.y = 115;
            addChild(container);
 
            for (var y:int = 0; y < HEIGHT; y++)
            {
                Tile.group[y] = [];
                var tx:int = (y % 2) ? Tile.WIDTH / 2 : 0;
                var width:int = (y % 2) ? WIDTH - 1 : WIDTH;
 
                for (var x:int = 0; x < width; x++)
                {
                    Tile.group[y][x] = container.addChild(new Tile(x * Tile.WIDTH + tx, y * Tile.HEIGHT)) as Tile;
                }
            }
 
            addChild(Status.valueText = new TextField());
            addChild(Status.messageText = new TextField());
            Status.messageText.y = 35;
            Status.valueText.defaultTextFormat = Status.messageText.defaultTextFormat = new TextFormat("_typeWriter", 30, 0x0, true);
            Status.valueText.text = "RED:000 BLUE:000";
            Status.valueText.width = Status.messageText.width = stage.stageWidth;
            Status.valueText.autoSize = Status.messageText.autoSize = "center";
            Status.setNextValue();
 
            const DIR:Array = 
            [
                [[-1, 0], [1, 0], [0, -1], [0, 1], [-1, -1], [1, -1]],
                [[-1, 0], [1, 0], [0, -1], [0, 1], [-1, 1], [1, 1]]
            ];
 
            for (y = 0; y < Tile.group.length; y++)
            {
                for (x = 0; x < Tile.group[y].length; x++)
                {
                    for each (var dir:Array in DIR[y % 2])
                    {
                        var dy:int = dir[0], dx:int = dir[1];
                        if (y + dy < 0 || Tile.group.length <= y + dy ||
                            x + dx < 0 || Tile.group[0].length <= x + dx) continue;
                        var tile:Tile = Tile.group[y + dy][x + dx];
                        if (tile) Tile(Tile.group[y][x]).adjacentTiles.push(tile);
                    }
                }
            }
 
            if (AI.available && AI.color == Status.turn)
            {
                Tile.clickable = false;
                AI.think();
            }
        }
    }
}
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;

class Tile extends Sprite
{
    public static const RADIUS:Number = 30;
    public static const WIDTH:Number = Math.cos(Math.PI / 6) * RADIUS * 2;
    public static const HEIGHT:Number = (2 - Math.sin(Math.PI / 6)) * RADIUS;
    public static var group:Array = [];
    public static var clickable:Boolean = true;

    public var adjacentTiles/*Tile*/:Array = [];
    private var points/*Point*/:Array = [];
    private var tf:TextField;
    private var _value:int = 0;
    private var _color:int = Color.NONE;

    public function Tile(x:Number, y:Number)
    {
        this.x = x;
        this.y = y;

        for (var radian:Number = -Math.PI / 2; radian < Math.PI * 1.5; radian += Math.PI / 3)
        {
            points.push(new Point(Math.cos(radian) * RADIUS, Math.sin(radian) * RADIUS));
        }

        tf = new TextField();
        tf.width = RADIUS * 2;
        tf.defaultTextFormat = new TextFormat("_typeWriter", 30, 0x0, true);
        tf.autoSize = "center";
        tf.selectable = false;
        tf.text = "0";
        tf.visible = false;
        tf.x = -tf.width / 2;
        tf.y = -tf.height / 2;
        addChild(tf);

        changeColor(color);

        this.buttonMode = true;
        this.mouseChildren = false;
        addEventListener(MouseEvent.CLICK, onMouseClick);
    }

    public function onMouseClick(event:MouseEvent = null):void 
    {
        if (!clickable && event) return;
        if (color != Color.NONE) return;
        value = Status.nextValue;
        color = Status.turn;
        if (color == Color.RED) Status.redValue += value;
        else Status.blueValue += value;
        Status.turnChange();
        tf.visible = true;

        for each (var tile:Tile in adjacentTiles)
        {
            if (tile.color == Color.NONE) continue;
            if (tile.color == this.color)
            {
                tile.value++;
                if (tile.color == Color.RED) Status.redValue++;
                else Status.blueValue++;
            }
            else if (tile.value < this.value)
            {
                tile.color = this.color;
                if (tile.color == Color.RED) Status.redValue += tile.value, Status.blueValue -= tile.value;
                else Status.blueValue += tile.value, Status.redValue -= tile.value;
            }
        }

        Status.setNextValue();
        if (AI.available && Status.turn == AI.color)
        {
            clickable = false;
            AI.think();
        }
        else clickable = true;
    }

    private function changeColor(color:int):void
    {
        var canvas:Sprite = new Sprite();
        canvas.graphics.beginFill(Color.DATA[color]);
        canvas.graphics.drawRect(0, 0, WIDTH, HEIGHT);
        canvas.graphics.endFill();

        var bd:BitmapData = new BitmapData(WIDTH, HEIGHT, false);
        bd.noise(0, 220, 255, 7, true);
        bd.draw(canvas, null, null, BlendMode.MULTIPLY);

        graphics.clear();
        graphics.lineStyle(2.0, 0x0);
        graphics.beginBitmapFill(bd);
        graphics.moveTo(points[0].x, points[0].y);
        for each (var point:Point in points)
        {
            graphics.lineTo(point.x, point.y);
        }
    }

    public function set value(value:int):void 
    {
        _value = value;
        tf.text = _value.toString();
    }

    public function get value():int 
    {
        return _value;
    }

    public function get color():int 
    {
        return _color;
    }

    public function set color(value:int):void 
    {
        _color = value;
        changeColor(_color);
    }
}

class Color
{
    public static const NONE:int = 0;
    public static const RED:int = 1;
    public static const BLUE:int = 2;

    public static const DATA:Array = [0xFFFFFF, 0xED1A3D, 0x009AD6];
}

class AI
{
    public static var available:Boolean = true;
    public static var color:int = Color.BLUE;
    public static function think():void
    {
        var best:Point = new Point();
        var bestValue:int = -1;
        for (var y:int = 0; y < Tile.group.length; y++)
        {
            for (var x:int = 0; x < Tile.group[y].length; x++)
            {
                var tile:Tile = Tile.group[y][x];
                if (tile.color != Color.NONE) continue;
                var value:int = 0;
                for each (var aTile:Tile in tile.adjacentTiles)
                {
                    if (aTile.color == Color.NONE) continue;
                    if (aTile.color == color) value++;
                    else if (aTile.value < Status.nextValue) value += aTile.value;
                }

                if (bestValue < value)
                {
                    best.x = x, best.y = y;
                    bestValue = value;
                }
            }
        }

        var bestTile:Tile = Tile.group[best.y][best.x];
        if (bestTile)
        {
            var timer:Timer = new Timer(1000, 1);
            timer.addEventListener(TimerEvent.TIMER_COMPLETE, function():void { bestTile.onMouseClick(); } );
            timer.start();
        }
    }
}

class Status
{
    public static var maxTurn:int = Main.WIDTH * Main.HEIGHT - Main.HEIGHT / 2;
    public static var turnCount:int = 0;
    public static var turn:int = Color.RED;
    private static var _redValue:int = 0;
    private static var _blueValue:int = 0;
    public static var nextValue:int = 0;
    public static var valueText:TextField;
    public static var messageText:TextField;

    static public function setNextValue():void
    {
        if (turnCount++ < maxTurn)
        {
            nextValue = int(Math.random() * 20 + 1);
            messageText.text = "Next " + nextValue;
        }
        else messageText.text = (_redValue == _blueValue) ? "引き分け" : (_redValue > _blueValue) ? "赤の勝ち" : "青の勝ち";
    }

    static public function get redValue():int 
    {
        return _redValue;
    }

    static public function set redValue(value:int):void 
    {
        _redValue = value;
        valueTextUpdate();
    }

    static public function get blueValue():int 
    {
        return _blueValue;
    }

    static public function set blueValue(value:int):void 
    {
        _blueValue = value;
        valueTextUpdate();
    }

    static private function valueTextUpdate():void 
    {
        valueText.text = "RED:" + valueFormat(_redValue) + " BLUE:" + valueFormat(_blueValue);
    }

    static private function valueFormat(value:int):String
    {
        var str:String = value.toString();
        while (str.length < 3) str = "0" + str;
        return str;
    }

    public static function turnChange():void
    {
        turn = (turn == Color.RED) ? Color.BLUE : Color.RED;
    }
}