Pirates' gold

by andyshang
♥0 | Line 104 | Modified 2010-09-07 08:24:57 | MIT License
play

ActionScript3 source code

/**
 * Copyright andyshang ( http://wonderfl.net/user/andyshang )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/jONN
 */

package
{
    import flash.display.Sprite;

    import org.osmf.net.StreamingURLResource;

    public class Main extends Sprite
    {
        private var g:int = 20;
        private var robbers:Vector.<Robber>;

        private var COUNT:int = 15;
        private var DIVIDER:Number = 4/3;

        public function Main()
        {
            robbers = new Vector.<Robber>;
            for(var i:int=0;i<COUNT;i++)
            {
                robbers.push(new Robber(i))
            }
            getSolution(COUNT);
            sortOnIndex();
            trace(robbers);
        }

        public function getSolution(i:int):void
        {
            if(i > 200)
            {
                getSolution2();
            }
            if(i == 3)
            {
                if(DIVIDER < 2)
                {
                    robbers[0].weight = 0;
                    robbers[1].weight = 0;
                    robbers[2].weight = g;
                }
                else
                {
                    robbers[0].weight = 1;
                    robbers[1].weight = 0;
                    robbers[2].weight = g-1;
                }
            }
            else
            {
                getSolution(--i);
                sort(i);
                var count:int = 0;
                var upper:int = int(i/DIVIDER)
                for(var k:int=0;k<i;k++)
                {
                    if((count < upper && DIVIDER < 2) || (count <= upper && DIVIDER >= 2))
                    {
                        robbers[k].weight ++;
                        count ++;
                    }
                    else
                    {
                        robbers[k].weight = 0;
                    }
                }
                robbers[i].weight += g - count;
            }
        }

        public function getSolution2():void
        {
        }

        private function sortOnIndex():void
        {
            robbers.sort(function(a:Robber,b:Robber):int
            {
                return a.index > b.index?1:-1;
            })
        }

        private function sort(len:int):void
        {
            var l:int = len;
            for(;l > 1;l--)
            {
                for(var i:int=0;i<l-1;i++)
                {
                    if(robbers[i].weight > robbers[i+1].weight)
                    {
                        var tmp:Robber = robbers[i];
                        robbers[i] = robbers[i+1]
                        robbers[i+1] = tmp;
                    }
                }
            }
        }
    }
}
class Robber
{
    public var index:int = -1;
    public var weight:int = 0;
    public function Robber(i:int)
    {
        index = i;
    }
    public function toString():String
    {
        return weight + "";
    }
}