码迷,mamicode.com
首页 > 其他好文 > 详细

codecombat安息之云山峰32-36关及森林49关代码分享

时间:2015-09-11 14:32:40      阅读:730      评论:0      收藏:0      [点我收藏+]

标签:

codecombat中国游戏网址: http://www.codecombat.cn/
所有代码为javascript代码分享

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

32、Sowing Fire

// Goal: build three rows of nine fire-traps.
// Returns "retreat", "attack", "start-next-trap-column", or "build-next-trap-in-column"
this.chooseStrategy = function() {
    var enemies = this.findEnemies();
    // If there are overwhelming ogre forces, return the "retreat" strategy.
    if(enemies.length > 20) {
        return "retreat";
    } 
    // If there are some ogres, return the "attack" strategy.
    else if (enemies.length > 0 && enemies.length <= 20) {
        return "attack";
    }
    // Use x % 9 === 0 to see if x is divisible by 9.
    // Use this.built.length to see how many traps you have built.
    // If you have finished a column of 9 traps, return "start-next-trap-column"
    else if (this.built.length % 9 === 0) {
        return "start-next-trap-column";
    }
    // Otherwise, return "build-next-trap-in-column"
    else {
        return "build-next-trap-in-column";
    }
};
var trapsInColumn = 9;
var startX = 40;
var columnX = startX;
// Build the next trap in a column in the correct place.
this.buildNextTrapInColumn = function(columnX,numTraps) {
    // Change newY to use % to wrap around and only build trapsInColumn (9) traps per column
    var newY = numTraps % trapsInColumn * 7 + 10;
    if (this.pos.y < newY) {
        this.move({"x": columnX - 5, "y": newY});
    } else {
        this.buildTrap(columnX,newY);
    }
};
// Start a new column of traps.
this.startNextTrapColumn = function(columnX, numTraps) {
    var newX = startX - (Math.floor(numTraps / trapsInColumn) * 10);
    if (this.pos.y > 10) {
        this.move({"x": newX - 5, "y": 10});
        return columnX;
    } else {
        this.buildTrap(newX,10);
        return newX;
    }
};
this.buildTrap = function(x, y) {
    this.buildXY("fire-trap", x, y);
};
this.commandAttack = function() {
    // Have your griffin riders fend off the attackers.
    var friends = this.findFriends();
    for(var i = 0; i< friends.length; i++){
        var friend = friends[i];
        var enemy = friend.findNearestEnemy();
        if (enemy) {
            this.command(friend, "attack", enemy);
        }
    }
};
this.commandRetreat = function() {
    // You and your griffin riders retreat to safety behind the traps.
    var friends = this.findFriends();
    for(var i = 0; i< friends.length; i++){
        var friend = friends[i];
            this.command(friend, "move", {x:4,y:42});
    }
    this.moveXY(4, 42);
};

loop {
    var strategy = this.chooseStrategy();
    if(strategy == "attack") {
       this.commandAttack();
    } else if(strategy == "build-next-trap-in-column") {
        this.buildNextTrapInColumn(columnX, this.built.length);
    } else if(strategy == "start-next-trap-column") {
        columnX = this.startNextTrapColumn(columnX, this.built.length);
    } else if(strategy == "retreat") {
        this.commandRetreat();
    }
}


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

33、Reaping Fire

// The goal is to survive for 30 seconds, and keep the mines intact for at least 30 seconds.
this.chooseStrategy = function() {
    var fang = this.findByType("fangrider");
    if (this.gold > this.costOf("griffin-rider")) {
        return "griffin-rider";
    }
    // If you can summon a griffin-rider, return "griffin-rider"
    // If there is a fangrider on your side of the mines, return "fight-back"
    else if (fang) {
        for(var i = 0; i < fang.length; i++){
            var fa = fang[i];
            if (fa.pos.x <= 38) {
                return "fight-back";
            }
        }
    }
    // Otherwise, return "collect-coins"
    else {
        return "collect-coins";
    }
};

this.commandAttack = function() {
    // Command your griffin riders to attack ogres.
    var friends = this.findFriends();
    if (friends) {
         for(var i = 0; i< friends.length; i++){
            var friend = friends[i];
            var enemy = friend.findNearestEnemy();
            if (enemy) {
                this.command(friend, "attack", enemy);
            }
        }    
    }
   
};

this.pickUpCoin = function() {
    // Collect coins
    var coin = this.findNearest(this.findItems());
    if (coin) {
       this.move(coin.pos); 
    }
    
};

this.heroAttack = function() {
    // Your hero should attack fang riders that cross the minefield.
    var en = this.findNearest(this.findByType("fangrider"));
    if (en) {
        this.attack(en);   
    }
    
};

loop {
    this.commandAttack();
    var strategy = this.chooseStrategy();
    // Call a function, depending on what the current strategy is.
    if (strategy == "griffin-rider") {
        this.summon("griffin-rider");
    }
    else if (strategy == "fight-back") {
        this.heroAttack();
    }
    else if ("collect-coins") {
        this.pickUpCoin();
    }
}

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

34、Toil and Trouble

// Ogre Witches have some unpleasant surprises ready for you.
// Define a chooseTarget function which takes a friend argument
// Returns the a target to attack, depending on the type of friend.
// Soldiers should attack the witches, archers should attack nearest enemy.
this.chooseTarget = function(soldier){
if (soldier.type == "soldier") {
    return "witches";
}
if (soldier.type == "archers") {
    return "enemy";
}
};
this.attackEnemy = function(solider){
    var enemy = solider.findNearestEnemy();
    if (enemy) {
        this.command(solider, "attack", enemy);
    }
};

loop {
    var friends = this.findFriends();
    for(var i=0; i < friends.length; i++) {
        // Use your chooseTarget function to decide what to attack.
        var friend = friends[i];
        var choose = this.chooseTarget(friend);
        if (choose == "witches") {
            var enemy = this.findNearest(this.findByType("witches"));
            if (enemy) {
                this.command(friend, "attack", enemy);
            }
            else {
                this.attackEnemy(friend);
            }
        }
        if (choose == "enemy") {
            this.attackEnemy(friend);
        }
    }
}

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

35、Raiders of the Long Dark

//需多提交几次才可能通关
// Your goal is to protect the peasant and move to the right.
// Arryn Stonewall will defend the front, and command the soldiers.
// You need to cover the rear and command the peasant.
var arryn = this.findByType("raider")[0];
var peasant = this.findByType("peasant")[0];
this.chooseHeroStrategy = function() {
    // Return either "fight" or "advance".
    if (this.distanceTo(peasant) > 15) {
        return "advance";
    }
    var enemy = this.findNearest(this.findEnemies());
    if (enemy && this.distanceTo(enemy) < 10) {
        return "fight";
    }
    else {
        return "advance";
    }
    // Try to stay 5m behind the peasant when not fighting.
    // Don‘t get more than 15m away from the peasant.
};

this.heroFight = function() {
    // Stop the ogres from rushing past you to get to the peasant!
    // Hint: try to slow them down if you can
    var enemy = this.findNearest(this.findEnemies());
    if (enemy) {
        if (this.isReady("bash")) {
            this.bash(enemy);
        }
        else {
            this.attack(enemy);
        }
    }
};

this.heroAdvance = function() {
    // Stay behind the peasant
    this.moveXY(peasant.pos.x - 5 , peasant.pos.y);
};

this.choosePeasantStrategy = function() {
    // Return "follow", "build-above", or "build-below"
    // Hint: use isPathClear() to determine where the hallways are
    var above = this.isPathClear(peasant.pos, {x:peasant.pos.x, y:peasant.pos.y + 20});
    var below = this.isPathClear(peasant.pos, {x:peasant.pos.x, y:peasant.pos.y - 20});
    if (above) {
        return "build-above";
    }
    else if (below) {
        return "build-below";
    }
    else {
        return "follow";
    }
};

this.peasantAdvance = function() {
    // Keep the peasant behind Arryn and her soldiers.
    this.command(peasant, "move", {x:arryn.pos.x - 5, y:arryn.pos.y});
};

this.peasantBuild = function(x,y) {
    // Command the peasant to build a palisade.
    this.command(peasant, "buildXY", "palisade",x,y);
};

loop {
    var heroStrategy = this.chooseHeroStrategy();
    if(heroStrategy == "fight") {
        this.heroFight();
    } else if(heroStrategy == "advance") {
        this.heroAdvance();
    }
    
    var peasantStrategy = this.choosePeasantStrategy();
    if(peasantStrategy == "build-above") {
        this.peasantBuild(peasant.pos.x, peasant.pos.y + 5);
    } else if(peasantStrategy == "build-below") {
        this.peasantBuild(peasant.pos.x, peasant.pos.y - 5);
    } else if(peasantStrategy == "follow") {
        this.peasantAdvance();
    }
}

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

36、Grim Determination

// Your goal is to protect Reynaldo
// Find the paladin with the lowest health.
this.lowestHealthPaladin = function() {
    var lowestHealth = 99999;
    var lowestFriend = null;
    var friends = this.findFriends();
    for(var f=0; f < friends.length; f++) {
        var friend = friends[f];
        if(friend.type != "paladin") { continue; }
        if(friend.health < lowestHealth && friend.health < friend.maxHealth) {
            lowestHealth = friend.health;
            lowestFriend = friend;
        }
    }
    return lowestFriend;
};
this.commandBuilt = function(){
    if (this.gold > this.costOf("griffin-rider")) {
        this.summon("griffin-rider");
    }
};
this.findbadEnemy = function(){
    var enemys = this.findEnemies();
    var bad = null;
    var health = 999999;
    for(var i = 0; i < enemys.length; i++){
        var enemy = enemys[i];
        if (enemy.health < health) {
            health = enemy.health;
            bad = enemy;
        }
    }
    if (bad) {
        return bad;
    }
    else {
        return null;
    }
};
this.commandPaladin = function(paladin) {
    // Heal the paladin with the lowest health using lowestHealthPaladin()
    var lowestF = this.lowestHealthPaladin();
    // You can use paladin.canCast("heal") and command(paladin, "cast", "heal", target)
    var enemy = this.findbadEnemy();
    if (paladin.canCast("heal")) {
        if (lowestF) {
         this.command(paladin, "cast", "heal", lowestF);
        }
    }
    else if (enemy) {
        this.command(paladin, "shield");
    }
    // Paladins can also shield: command(paladin, "shield")
};
this.commandPeasant = function(friend){
    var coin = friend.findNearestItem();
    if (coin) {
        this.command(friend, "move", coin.pos);
    }
};
this.commandGriffin = function(friend){
    var enemy = this.findbadEnemy();
    if (enemy) {
        this.command(friend, "attack", enemy);
    }
};
this.commandFriends = function() {
    // Command your friends.
    var friends = this.findFriends();
    for(var i=0; i < friends.length; i++) {
        var friend = friends[i];
        if(friend.type == "peasant") {
            this.commandPeasant(friend);
        } else if(friend.type == "griffin-rider") {
            this.commandGriffin(friend);
        } else if(friend.type == "paladin") {
            this.commandPaladin(friend);
        }
    }
};
this.helps = function(){
    if (this.canCast("summon-fangrider")) {
        this.cast("summon-fangrider");
    }
    if (this.isReady("reset-cooldown")) {
        this.resetCooldown("summon-fangrider");
    }
};
loop {
    this.commandBuilt();
    this.commandFriends();
    this.helps();
    // Summon griffin riders!
}

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

森林49关—Forest Flower Grove

// This level is a place for making flower art.
// The real goal is to experiment and have fun!
// If you draw something with at least 1000 flowers, you will "succeed" at the level.
this.drawCircle = function(x, y, size) {
    var angle = 0;
    this.toggleFlowers(false);
    while (angle <= Math.PI * 2) {
        var newX = x + (size * Math.cos(angle));
        var newY = y + (size * Math.sin(angle));
        this.moveXY(newX, newY);
        this.toggleFlowers(true);
        angle += 0.2;
    }
};
this.drawSquare = function(x, y, size) {
    this.toggleFlowers(false);
    cornerOffset = size / 2;
    this.moveXY(x - cornerOffset, y - cornerOffset);
    this.toggleFlowers(true);
    this.moveXY(x + cornerOffset, y - cornerOffset);
    this.moveXY(x + cornerOffset, y + cornerOffset);
    this.moveXY(x - cornerOffset, y + cornerOffset);
    this.moveXY(x - cornerOffset, y - cornerOffset);
};
var r = 10;
var X = [];
X[1] = {x: 30, y: 110};
X[2] = {x: 70, y: 110};
X[3] = {x: 100, y: 110};
X[4] = {x: 130, y: 110};
X[5] = {x: 30, y: 70};
X[6] = {x: 70, y: 70};
X[7] = {x: 100, y: 70};
X[8] = {x: 130, y: 70};
X[9] = {x: 30, y: 30};
X[10] = {x: 70, y: 30};
X[11] = {x: 100, y: 30};
X[12] = {x: 130, y: 30};
for(var i = 1; i <= X.length; i++){
    var m = X[i];
    if (m) {
    this.drawCircle(m.x,m.y,r);
    this.drawSquare(m.x,m.y,r);        
    }

}

codecombat安息之云山峰32-36关及森林49关代码分享

标签:

原文地址:http://my.oschina.net/comA/blog/504898

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!