In this level, my hero just stands still and does nothing. Can someone point out where I messed up? Thanks!
function chooseStrategy() {
var enemies = hero.findEnemies();
// If you can summon a griffin-rider, return "griffin-rider"
if(hero.gold >= hero.costOf("griffin-rider")){
return "griffin-rider";
}
// If there is a fangrider on your side of the mines, return "fight-back"
for(var i=0;i<enemies.length;i++){
var distance = hero.distanceTo(enemy);
var flyer= enemies[i];
if(flyer && distance < 30 ){
return "fight-back";
}
// Otherwise, return "collect-coins"
else{
return "collect-coins";
}
}
}
function commandAttack() {
// Command your griffin riders to attack ogres.
var enemy= hero.findNearest(hero.findEnemies());
if(enemy && enemy.type == "ogre"){
hero.command(griffin-riders,'attack',enemy);
}
}
function pickUpCoin() {
// Collect coins
var coin= hero.findNearest(hero.findItems());
if(coin){
hero.move(coin.pos);
}
}
function heroAttack() {
// Your hero should attack fang riders that cross the minefield.
var enemy = hero.findNearest(hero.findEnemies());
if (enemy && enemy.type==fangriders){
hero.attack(enemy);
}
}
while(true) {
commandAttack();
var strategy = chooseStrategy();
// Call a function, depending on what the current strategy is.
if (strategy == "griffin-rider"){
commandAttack();
}
if(strategy == "fight-back"){
heroAttack();
}
if(strategy =="collect-coins"){
pickUpCoin();
}
}
when the strategy returned âgriffin-riderâ you were supposed to summon one griffin-rider. You also didnât define griffin-riders. And, if they were going to attack,you had to show that a commandable unit was standing there.
I changed my code and tweaked it a bit and got this:
function chooseStrategy() {
var enemy = hero.findNearest(hero.findEnemies());
// If you can summon a griffin-rider, return "griffin-rider"
if(hero.gold >= hero.costOf("griffin-rider")){
hero.summon("griffin-rider");
}
// If there is a fangrider on your side of the mines, return "fight-back"
else if(enemy){
var distance = hero.distanceTo(enemy);
if(enemy && distance < 10 ){
return "fight-back";
}
}
// Otherwise, return "collect-coins"
else{
return "collect-coins";
}
}
function commandAttack() {
// Command your griffin riders to attack ogres.
var enemy= hero.findNearest(hero.findEnemies());
var friend = hero.findByType('griffin-rider');
if(enemy && enemy.type == "ogre"){
hero.command(friend,'attack',enemy);
}
}
function pickUpCoin() {
// Collect coins
var coin= hero.findNearest(hero.findItems());
if(coin){
var coinPos={'x':coin.pos.x,'y':coin.pos.y};
hero.move(coinPos);
}
}
function heroAttack() {
// Your hero should attack fang riders that cross the minefield.
var enemy = hero.findNearest(hero.findEnemies());
if (enemy && enemy.type=='fang-riders'){
hero.attack(enemy);
}
}
while(true) {
commandAttack();
var strategy = chooseStrategy();
// Call a function, depending on what the current strategy is.
if (strategy == "griffin-rider"){
commandAttack();
}
else if(strategy == "fight-back"){
heroAttack();
}
else if(strategy =="collect-coins"){
pickUpCoin();
}
}
This time my hero moves and picks up coins, summons 1 griffin rider, and stops moving when an enemy comes
good question lol. it was already there, and i guess i forgot to delete it. as for the weird indentations, when I hit âenterâ thatâs where the curser went to i guess. I"ll try to fix everything =]