codecombat之Sarven沙漠25-37关代码分享

来源:互联网 发布:淘宝最红女模特 编辑:程序博客网 时间:2024/04/19 21:31
codecombat中国游戏网址:http://www.codecombat.cn/
所有代码为javascript代码分享

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

25、捡闪亮东西的人

// 很快的获取最多的金币

loop {
    var coins = this.findItems();
    var coinIndex = 0;
    while (coinIndex < coins.length) {
        // 把这个封装进循环里枚举所有的硬币
        var coin = coins[coinIndex];
        // 金币价值3点。
        if (coin.value == 3) {
            // 只捡金币。
            var x = coin.pos.x ;
            var y = coin.pos.y ;
            this.moveXY(x, y);
        }
        coinIndex ++ ;
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

26、审判

//法师+恶龙法杖
loop {
    var flag = this.findFlag();
    if (flag) {
        this.pickUpFlag(flag);
    }
    if (this.canCast("summon-fangrider")) {
        this.cast("summon-fangrider");
    }
    var enemy = this.findNearest(this.findEnemies());
    if (enemy) {
        if (this.canCast("regen")) {
            this.cast("regen", this);
        }
        else {
            this.attack(enemy);
        }
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

27、疯狂的 Maxer

//战士注意强化装备
// 优先杀掉最远的敌人。
loop {
    var farthest = null;
    var maxDistance = 0;
    var enemyIndex = 0;
    var enemies = this.findEnemies();
    // 查看全部敌人,找出最远的那个。
    while (enemyIndex < enemies.length) {
        var target = enemies[enemyIndex];
        // 是不是有敌人比我们能看到的最远的敌人还要远?
        var distance = this.distanceTo(target);
        if (distance > maxDistance) {
            maxDistance = distance;
            farthest = target;
        }
        enemyIndex += 1;
    }
    if (farthest) {
        // 干掉最远的敌人!
        // 如果敌人血量大于0就保持攻击。
        while (farthest.health > 0) {
            if (this.isReady("cleave")) {
                this.cleave(farthest);
            }
            else if (this.isReady("bash")) {
                this.bash(farthest);
            }
            else {
                this.attack(farthest);
            }
        }
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

28、Sand Snakes

// This field is covered in firetraps.  Thankfully we've sent a scout ahead to find a path.  He left coins along the path so that if we always stick to the nearest coin, we'll avoid the traps.

// This canyon seems to interfere with your findNearest glasses!
// You'll need to find the nearest coins on your own.
loop {
    var coins = this.findItems();
    var coinIndex = 0;
    var nearest = null;
    var nearestDistance = 9999;
    // Loop through all the coins to find the nearest one.
    while(coinIndex < coins.length) {
        var coin = coins[coinIndex];
        coinIndex++;
        var distance = this.distanceTo(coin);
        // If this coin's distance is less than the nearestDistance
            // Set nearest to coin
            // Set nearestDistance to distance
        if (distance < nearestDistance) {
            nearset = coin ;
            nearestDistance = distance ;
        }
    }
    if (nearset) {
        var x = nearset.pos.x ;
        var y = nearset.pos.y ;
        this.moveXY(x, y);
    }
    // If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

29、疯狂MAXER反击

NO.1
//更换更好的装备
// 小一点的食人魔会造成更多的伤害!
// 优先攻击最少健康的食人魔
loop {
    var weakest = null;
    var leastHealth = 99999;
    var enemyIndex = 0;
    var enemies = this.findEnemies();
    // 循环攻击所有敌人。
    // 如果一个敌人的生命小于最小生命,
    // 让它成为最弱的,并设置它的生命为 leastHealth
    while (enemyIndex < enemies.length) {
        var enemy = enemies[enemyIndex] ;
        if (enemy.health < leastHealth) {
            weakest = enemy ;
            leastHealth = enemy.health ; 
        }
        enemyIndex ++ ;
    }
    if (weakest) {
        // 攻击最弱的食人魔。
        while (weakest.health > 0) {
            if (this.isReady("bash")) {
                this.bash(weakest);
            }
            else {
                this.attack(weakest);
            }           
        }
    }
}
NO.2使用巫师(拥有恶龙爪子法杖)
// 小一点的食人魔会造成更多的伤害!
// 优先攻击最少健康的食人魔
loop {
    var weakest = null;
    var leastHealth = 99999;
    var enemyIndex = 0;
    var enemies = this.findEnemies();
    // 循环攻击所有敌人。
    // 如果一个敌人的生命小于最小生命,
    // 让它成为最弱的,并设置它的生命为 leastHealth
    while (enemyIndex < enemies.length) {
        var enemy = enemies[enemyIndex] ;
        if (enemy.health < leastHealth) {
            weakest = enemy ;
            leastHealth = enemy.health ; 
        }
        enemyIndex ++ ;
    }
    if (weakest) {
        if (this.canCast("summon-fangrider")) {
            this.cast("summon-fangrider");
        }
        if (this.canCast("regen")) {
            this.cast("regen", this);
        }
        // 攻击最弱的食人魔。
        while (weakest.health > 0) {
            this.attack(weakest);           
        }
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

30、疯狂 Maxer 卖光了

// 金币会在几秒钟之后消失!
// 在他们消失前,收集所有的金币。

loop {
    var closestGold = null;
    var minGoldDist = Infinity;
    var coinIndex = 0;
    var coins = this.findItems();
    // 找到最近的金币
    // 记住,金币价值3点。
    while (coinIndex < coins.length){
        var coin = coins[coinIndex] ;
        var distance = this.distanceTo(coin);
        if (distance < minGoldDist && coin.value == 3) {
            closestGold = coin ;
            minGoldDist = distance ;
        }
        coinIndex ++ ;
    }
    if (closestGold) {
        var x = closestGold.pos.x ;
        var y = closestGold.pos.y ;
        this.moveXY(x, y);
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

31、疯狂的 Maxer 变得贪婪

// 比你的分身收集的金币多。
// 你只有几秒钟来收集金币,聪明的选择你的路线!
loop {
    var bestCoin = null;
    var maxRating = 0;
    var coinIndex = 0;
    var coins = this.findItems();
    // 试着计算"价值/距离"来决定你要收集哪个金币。
    while (coinIndex < coins.length){
        var coin = coins[coinIndex] ;
        var distance = this.distanceTo(coin) ;
        var values = coin.value/distance ;
        if (values > maxRating) {
            maxRating = values ;
            bestCoin = coin ;
        }
        coinIndex ++ ;
    }
    if (bestCoin) {
        var x = bestCoin.pos.x ;
        var y = bestCoin.pos.y ;
        this.moveXY(x, y);
    }
    
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

32、疯狂的 Maxer:兑现

// 你不能到你朋友那边去保卫他们!
// 告诉他们回家,弓箭手会帮助他们

loop {
    var weakestFriend = null;
    var leastHealth = 9999;
    var friendIndex = 0;
    var friends = this.findFriends();
    // 告诉最弱的朋友先回家。
    while (friendIndex < friends.length) {
        var friend = friends[friendIndex] ;
        var yourhealth = friend.health ;        
        if (yourhealth < leastHealth) {
            weakestFriend = friend ;
            leastHealth = yourhealth ;
        }
        friendIndex ++ ;
    }
    if (weakestFriend) {
        this.say("Hey " + weakestFriend.id + ", go home!");
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

33、Harrowland

// 使用你最聪明的编程技术来胜过你的对手!
loop {
    var weakestenemy = null ;
    var leasthealth = 999999 ;
    var enemys = this.findEnemies();
    var enemyindex = 0 ;
    while (enemyindex < enemys.length) {
        var enemy = enemys[enemyindex] ;
        var health = enemy.health;
        if (health < leasthealth) {
            leasthealth = health ;
            weakestenemy = enemy ;
        }
        enemyindex ++ ;
    }
    if (weakestenemy) {
        while (weakestenemy.health > 0) {
            if (this.isReady("cleave")) {
                this.cleave(weakestenemy);
            }
            else if (this.isReady("bash")){
                this.bash(weakestenemy);
            }
            else {
                this.attack(weakestenemy);
            }
        }
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

34、克隆冲突

// 你需要更好的策略和好的装备来赢得这关。
NO.1战士
loop {
    var weakestenemy = null ;
    var leasthealth = 999999 ;
    var enemys = this.findEnemies();
    var enemyindex = 0 ;
    while (enemyindex < enemys.length) {
        var enemy = enemys[enemyindex] ;
        var health = enemy.health;
        if (health < leasthealth) {
            leasthealth = health ;
            weakestenemy = enemy ;
        }
        enemyindex ++ ;
    }
    if (weakestenemy && this.distanceTo(weakestenemy) < 40) {
        while (weakestenemy.health > 0) {
            if (this.isReady("cleave")) {
                this.cleave(weakestenemy);
            }
            else if (this.isReady("bash")){
                this.bash(weakestenemy);
            }
            else {
                this.attack(weakestenemy);
            }
        }
    }
}
NO.2法师

// 你需要更好的策略和好的装备来赢得这关。
loop {
    var flag = this.findFlag();
    if (flag) {
        this.pickUpFlag(flag);
    }
    var weakf = null;
    var heal = 99999 ;
    var frindex = 0 ;
    var friends = this.findFriends();
    while (frindex < friends.length) {
        var friend = friends[frindex] ;
        var health = friend.health ;
        if (health < heal) {
            weakf = friend ;            
            heal = health;
        }
        frindex ++ ;
    }
    var weakenemy = null ;
    var heals = 99999 ;
    var enemyindex = 0 ;
    var enemys = this.findEnemies();
    while (enemyindex < enemys.length) {
        var enemy = enemys[enemyindex] ;
        var healths = enemy.health ;
        if (healths < heals) {
            weakenemy = enemy ;
            heals = healths ;
        }
        enemyindex ++ ;
    }
    if (this.canCast("summon-fangrider")) {
        this.cast("summon-fangrider");
    }
    if (weakf) {
        if (this.canCast("regen")) {
            this.cast("regen", weakf);
        }
        else {
            if (this.distanceTo(weakenemy) < 40) {
                this.attack(weakenemy);
            }
            
        }
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

35、Sarven 斗殴

//巫师
// 活两分钟。
// 如果你赢了,本关会变得更难,更多奖励。
// 如果你输了,你必须等一天之后再提交。
// 记住,每次提交都会有新的随机种子。
loop {
    var flag = this.findFlag();
    if (flag) {
        this.pickUpFlag(flag);
    }
    var enemy = this.findNearest(this.findEnemies());
    if (this.canCast("summon-fangrider")) {
        this.cast("summon-fangrider");
    }
    else if ( this.canCast("regen")) {
        this.cast("regen", this);
    }
    else {
        if (enemy && this.distanceTo(enemy) < 20) {
            this.attack(enemy); 
        }     
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

36、Sarven的宝藏

//巫师
// 用传输机躲避食人魔收集150个金币
// 如果你赢了,会变得更难(并且有更多奖励)
// 如果你输了,需要等待一天再次挑战
// 记住,每次提交都会得到一个新的随机种子。
loop {
    var flag = this.findFlag();
    if (flag) {
        this.pickUpFlag(flag);
    }
    if (this.canCast("summon-fangrider")) {
        this.cast("summon-fangrider");
    }
    else if (this.canCast("regen")) {
        this.cast("regen", this);
    }
    else {
        var golds = this.findNearest(this.findItems());
        var x = golds.pos.x ;
        var y = golds.pos.y ;
        this.moveXY(x, y);
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

37、Sarven 围困

// Defend your towers in this replayable challenge level!
// Step on an X if you have 20 gold to build a soldier.
loop {
    var item = this.findNearest(this.findItems());
    var flag = this.findFlag();
    if (flag) {
        this.pickUpFlag(flag);
    }
    else if (this.canCast("summon-fangrider")) {
        this.cast("summon-fangrider");
    }
    else if (item) {
        var x = item.pos.x ;
        var y = item.pos.y ;
        this.moveXY(x, y);
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 0
原创粉丝点击