CodeCombat多人游戏Greed

题目的意思在于,更高效的Collect Gold;然后合理的安排生产出来的士兵;

// This code runs once per frame. Build units and command peasants!
// Destroy the ogre base within 180 seconds.
// Run over 4000 statements per call and chooseAction will run less often.
// Check out the green Guide button at the top for more info.

var base = this;

/////// 1. Command peasants to grab coins and gems. ///////
// You can only command peasants, not fighting units.
// You win by gathering gold more efficiently to make a larger army.
// Click on a unit to see its API.
var i1=0,i2=0,i3=0,i4=0;var n;
var j=0;//对所有的钱分类排序的时候使用的
var items = base.getItems();
var item=null;
var peasants = base.getByType('peasant');
var tempItems=items;//缓存,用来记录宝石和金银币铜币
    for(var i=0;i<items.length;i++)//宝石放在最前面
    {
        if(items[i].type=='gem')
        {
            i1++;
            tempItems[j++]=items[i];
        }
    }
    for(i=0;i<items.length;i++)//金币第二
    {
        if(items[i].type=='gold-coin')
        {
            i2++;
           tempItems[j++]=items[i];
        }
    }
    for(i=0;i<items.length;i++)//银币第三
    {
        if(items[i].type=='coin')
        {
            if(items[i].value===2)
            {
                i3++;
                tempItems[j++]=items[i];
            }
        }
    }   
    for(i=0;i<items.length;i++)//铜币第三
    {
        if(items[i].type=='coin')
        {
            if(items[i].value===1)
            {
                i4++;
                tempItems[j++]=items[i];
            }
        }
    }
    items=[];//清空数组
    if(i1>=peasants.length)//宝石的数量>=捡钱兵种的数量
    {
        for(i=0;i<i1;i++)
        {
            items[i]=tempItems[i];
        }
    }
    else//宝石的数量<捡钱兵种的数量
    {
        if(i1+i2>=peasants.length)//宝石的数量+金币的数量>=捡钱兵种的数量
        { 
            for(i=0;i<i1+i2;i++)
            {
                items[i]=tempItems[i];
            }
        }
        else//宝石的数量+金币的数量<捡钱兵种的数量
        {   
            if(i1+i2+i3>=peasants.length)//宝石、金币、银币的数量>=捡钱兵种的数量
            {
                for(i=0;i<i1+i2+i3;i++)
                {
                    items[i]=tempItems[i];
                }
            }
            else//宝石、金币、银币的数量<捡钱兵种的数量
            {
                    for(i=0;i<i1+i2+i3+i4;i++)
                    {
                        items[i]=tempItems[i];
                    }
                
            }
        }
    }
for (var peasantIndex = 0; peasantIndex < peasants.length; peasantIndex++)
{
    var peasant = peasants[peasantIndex];
    item = peasant.getNearest(items);
    for(i=0,n=0;i<items.length;i++)
    {
        if(item!==items[i])
        {
            items[n++]=items[i];
        }
    }
    if (item)
    {
        base.command(peasant, 'move', item.pos);
    }
}


/////// 2. Decide which unit to build this frame. ///////
// Peasants can gather gold; other units auto-attack the enemy base.
// You can only build one unit per frame, if you have enough gold.
var type;
if (base.built.length === 0)
    type = 'peasant';
else
    type = 'knight';
var knights = base.getByType('knight');
        if(peasants.length<=2)
        {
            type='peasant';      
        }

if (base.gold >= base.buildables[type].goldCost)
    base.build(type);


// 'peasant': Peasants gather gold and do not fight.
// 'soldier': Light melee unit.
// 'knight': Heavy melee unit.
// 'librarian': Support spellcaster.
// 'griffin-rider': High-damage ranged attacker.
// 'captain': Mythically expensive super melee unit.
// See the buildables documentation below for costs and the guide for stats.

 

posted @ 2014-09-14 10:13  ChuckLu  阅读(634)  评论(0编辑  收藏  举报