Help! reaping fire


#1

My hero doesn`t move but stands and it has no target until those fangriders come to kill him! What did I do wrong?

My code is this

this.chooseStrategy = function() {
    // If you can summon a griffin-rider, return "griffin-rider"
    if (this.gold>this.costOf("griffin-rider")){
        return "griffin-rider";
    }
    // If there is a fangrider on your side of the mines, return "fight-back"
    else if (this.findByType("fangrider")!==null){
        return "fight-back";
    }
    // Otherwise, return "collect-coins"
    else{
        return "collect-coins";
    }
};

this.commandAttack = function() {
    // Command your griffin riders to attack ogres.
    var friends=this.findByType("griffin-rider");
    for(var i=0;i<friends.length;i++){
        var friend=friends[i];
        var enemy=friend.findNearest(friend.findEnemies());
        if (enemy && enemy.type!="fangrider"){
            this.command(friend, "attack", enemy);
        }
    }
};

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

this.heroAttack = function() {
    // Your hero should attack fang riders that cross the minefield.
    var target = this.findNearest(this.findEnemies());
    if (target&&target.type=="fangrider"&&target.pos<38){
        if (this.distanceTo(target)<20){
            if (this.isReady("bash")){
                this.bash(target);
            }
            else {
                this.attack(target);
            }
        }
    }
};

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

#2

Hey there.
I’m not sure, but it seems to me like it should be !== “null” with quote marks.


#3

Incorrect null if for python and != is for javaScript


#4

Actually, python also uses != for not equal. But it is far easier to just test if [variable]: … as null is read as False in Python.


#5

friends is an array, not a variable.


#6

JavaScript uses var to initialize a new variable. Yoon is using friends to store an array of his friends. Are you saying you’ve never used friends = self.findFriends() in your time coding?


#7

Ah, my bad. I have not used Javascript much at all, but I thought I remembered something that now appears to be incorrect. My apologies. goes back to Python corner


#8

Well it is a variable its is just an array type variable


#9

It gives me an error saying that it is assigning to rvalue:

def chooseStrategy():
    enemies = self.findEnemies()

    # If you can summon a griffin-rider, return "griffin-rider"
    if self.gold >= self.costOf('griffin-rider'):
        return "griffin-rider"
    
        
    # If there is a fangrider on your side of the mines, return "fight-back"
    if self.distanceTo(self.findByType("fangrider")) <= 50:
        return "fight-back"
        
    # Otherwise, return "collect-coins"
    else:
        return "collect-coins"
def commandAttack():
    # Command your griffin riders to attack ogres.
    if self.findByType("ogre"):
        friends=self.findFriends()
        for self.findByType("griffin-rider") in friends:
            self.command(friend, "attack", ogre)
    pass
    
def pickUpCoin():
    coin=self.findNearest(self.findItems())
    self.move(coin.pos)
    pass
    
def heroAttack():
    if self.distanceTo(self.findByType("fangrider")) < 15:
        self.attack(self.findByType("fangrider"))
    # Your hero should attack fang riders that cross the minefield.
    pass
    
loop:
    commandAttack();
    strategy = chooseStrategy()
    # Call a function, depending on what the current strategy is.
    if strategy=='griffin-rider':
        self.summon('griffin-rider')
    elif strategy=='fight-back':
        heroAttack()
    else:
        pickUpCoin()

#10

In heroAttack, you try to find the distanceTo and attack a list of all the fang riders. You can’t do that. Instead, you should attack the nearest fangrider.


#11

i just made the griffins not attack the fangriders and it worked very well


#12

Why does it say that I can’t define it?


#13

@Bw55555 Looks like you are missing a closing ) in the previous line.


#14

I tried it, but the griffins still don’t attack the enemies. This is my code:


def chooseStrategy():
enemies = self.findEnemies()
enemy=self.findNearest(enemies)
# If you can summon a griffin-rider, return "griffin-rider"
if self.gold >= self.costOf(‘griffin-rider’):
return “griffin-rider”
# If there is a fangrider on your side of the mines, return "fight-back"
if self.findByType(“fangrider”)==True:
if self.distanceTo(self.findNearest(self.findByType(“fangrider”))) <= 50:
return "fight-back"
else:
return “collect-coins”
# Otherwise, return "collect-coins"
def commandAttack():
# Command your griffin riders to attack ogres.
friends=self.findFriends()
if enemy:
for friend in friends:
enemies = self.findEnemies()
enemy=friend.findNearest(enemies)
if enemy.type==“munchkin”:
self.command(friend, “attack”, self.findNearest(self.findByType(“munchkin”)))
elif enemy.type==“ogre”:
self.command(friend, “attack”, self.findNearest(self.findByType(“ogre”)))
elif enemy.type==“thrower”:
self.command(friend, “attack”, self.findNearest(self.findByType(“thrower”)))
elif enemy.type==“scout”:
self.command(friend, “attack”, self.findNearest(self.findByType(“scout”)))

def pickUpCoin():
# Collect coins
coin=self.findNearest(self.findItems())
self.move(coin.pos)
pass
def heroAttack():
# Your hero should attack fang riders that cross the minefield.
if self.distanceTo(self.findNearest(self.findByType(“fangrider”))) < 15:
self.attack(self.findNearest(self.findByType(“fangrider”)))
pass

loop:
    commandAttack()
    strategy = chooseStrategy()
    # Call a function, depending on what the current strategy is.
    if self.findFriends():
        commandAttack()
    strategy = chooseStrategy()
    # Call a function, depending on what the current strategy is.
    if strategy=='griffin-rider':
        self.summon('griffin-rider')
    elif strategy=='fight-back':
        heroAttack()
    else:
        pickUpCoin()

#15

Get of the first if enemy in your def commandAttack() function.
And format your code by surounding it by 3 backquotes (top-left on your keyboard)

code


#16

YAY!!! I passed the level! Thanks, people!


#17

I have no idea why I can’t pass this level.

def chooseStrategy():
    enemy=hero.findNearest(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"
    if enemy and enemy.type=="fangrider" and hero.distanceTo(enemy)<34:
        return "fight-back"
    else:
        return "collect-coins"
    

def commandAttack():
    for griffin in hero.findByType("griffin-rider"):
        if griffin:
            enemy=griffin.findNearest(griffin.findEnemies())
            if enemy and enemy.type!="fangrider":
                hero.command(griffin, "attack", enemy)


def pickUpCoin():
    # Collect coins
    coin=hero.findNearest(hero.findItems())
    if coin:
        hero.move(coin.pos)
    pass
    
def heroAttack():
    enemy=hero.findNearest(hero.findEnemies())
    if enemy:
    # Your hero should attack fang riders that cross the minefield.
        hero.attack(enemy)
    pass
    
    
while True:
    commandAttack()
    strategy = chooseStrategy()
    # Call a function, depending on what the current strategy is.
    if strategy=="griffin-rider":
        hero.summon("griffin-rider")
    elif strategy=="fight-back":
        heroAttack()
    elif strategy=="collect-coins":
        pickUpCoin()