[GAME] Floodit Remake

Hey, I downloaded a fun little game on my G1 today that I thought I’d remake. I don’t think the idea is originally that of LabPixies, who make Floodit, but it’s the first one I played.

[CENTER]Play
[/CENTER]

Interface done in flex, but game in pure ActionScript (with the exception that the game class extends UIComponent).

Here’s the game class:

package floodit
{
    import flash.events.Event;
    
    import floodit.floodlitClasses.FlooditTile;
    
    import mx.core.UIComponent;

    [Event(name="draw", type="flash.events.Event")]
    [Event(name="win", type="flash.events.Event")]
    
    public class Floodit extends UIComponent
    {
        /*
         * ===========================================
         * GAME SIZING
         */
        
        [Bindable] public var tileWidth:int = 20;
        [Bindable] public var tileHeight:int = 20;
        [Bindable] public var mapWidth:int = 20;
        [Bindable] public var mapHeight:int = 20;
        
        /*
         * ===========================================
         * GAME VARIABLES
         */
         
        [Bindable] public var steps:int = 0;
        [Bindable] public var status:String = "(icio)";
        
        /*
         * Colours are Tee Time! by whirledpeace
         * http://www.colourlovers.com/palette/865084/Tee_Time!
         */
        protected var _selectedIndex:int = 0;
        protected var _colors:Array = [0xFFFFFF, 0x87E8DE, 0x393753, 0x3E9125, 0xB4E061, 0xCC003C];
        
        protected var map:Array = [];
        protected var updated:Array = [];
        
        private var usedMap:Array = [];
        
        /*
         * ===========================================
         * GAME LOGIC
         */
        
        public function newGame():void
        {
            this.clean();
            this.create();
            this.draw();
        }
        
        public function selectColor(index:int):void
        {
            if (index == this._selectedIndex) return;
            
            this.fill(index);
            this.draw();
            this._selectedIndex = index;
            
            this.tried();
        }
        
        protected function tried():void
        {
            this.status = (++this.steps) + " Steps";
            
            if (hasWon())
                this.dispatchEvent(new Event("win"));
        }
        
        public function hasWon():Boolean
        {
            for (var y:int = 0; y < this.mapWidth; y++)
                for (var x:int = 0; x < this.mapWidth; x++)
                    if ((map[y][x] as FlooditTile).index != this._selectedIndex)
                        return false;
            return true;
        }
        
        /*
         * ===========================================
         * GAME WORKINGS
         */
         
        protected function clean():void
        {
            this.steps = 0;
            this.status = "(icio)";
            
            this.updated = [];
            this.map = [];
            while (numChildren) this.removeChildAt(0);
        }
        
        protected function create():void
        {
            this.updated = [];
            
            for (var y:int = 0; y < this.mapHeight; y++)
            {
                if (!this.map[y]) this.map[y] = [];
                for (var x:int = 0; x < this.mapWidth; x++)
                {
                    var i:int = Math.floor(Math.random() * this._colors.length);
                    if (x + y == 1 && i == map[0][0].index) {
                        x--;
                        continue;
                    }
                    
                    var tile:FlooditTile = new FlooditTile(x, y, this._colors*, i);
                    this.addChild(tile);
                    this.map[y][x] = tile;
                    this.updated.push(tile);
                }
            }
            
            this._selectedIndex = map[0][0].index;
        }
        
        protected function draw():void
        {
            for each (var tile:FlooditTile in this.updated)
            {
                tile.draw(this.tileHeight, this.tileWidth);
            }
            
            this.updated = []
            this.dispatchEvent(new Event("draw"));
        }
        
        protected function fill(index:int, x:int = 0, y:int = 0):void
        {
            if (hasFilled(x, y)) return;
            var tile:FlooditTile = this.map[y][x] as FlooditTile;
            
            if (tile.index == this._selectedIndex)
            {
                this.hasFilled(x, y, true);
                tile.index = index;
                tile.color = this._colors[index];
                this.updated.push(tile);

                this.fill(index, x-1, y);
                this.fill(index, x+1, y);
                this.fill(index, x, y-1);
                this.fill(index, x, y+1);
            }
            
            if (x + y == 0) {
                usedMap = [];
            }
        }
        
        private function hasFilled(x:int, y:int, set:Boolean = false):Boolean
        {
            if (set) {
                if (!usedMap[y]) usedMap[y] = [];
                usedMap[y][x] = true;
            } 
            return (!map[y] || !map[y][x]) || (usedMap[y] && usedMap[y][x]);
        }
        
        /*
         * ===========================================
         * GETTERS / SETTERS
         */
        
        public function set colors(c:Array):void
        {
            this._colors = c;
            this.draw();
        }
        
        
        [Bindable] public function get colors():Array
        {
            return this._colors;
        }
        
        public function set selectedIndex(c:int):void
        {
            this.selectColor(c);
        }
        
        [Bindable] public function get selectedIndex():int
        {
            return this._selectedIndex;
        }
    }
}

Source attached, plus game has “View Source” enabled.

Let me know what you think :thumb: