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();
}
}
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?
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
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()
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.
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()
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()