For the level Hunters and Prey in Cloudrip Mountain the sample code is in Javascript while I use Python. When I put a python code my whole code ends up being wrong. Does anyone have any solutions???
Hunters and Prey serious bug
I did and it shows that I am using python. This is my code:
# Ogres are trying to kill your reindeer!
# Keep your archers back while summoning soldiers to attack.
def pickUpCoin():
coin = self.findNearest(self.findItems())
if coin:
self.move(coin.pos)
pass
def summonTroops():
if self.gold > self.costOf("soldier"):
self.summon("soldier")
# Summon soldiers if you have the gold.
pass
# This function has an argument named soldier.
# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldier(soldier):
enemy = soldier.findNearestEnemy
if enemy:
self.command(soldier, "attack", enemy)
# Soldiers should attack enemies.
pass
# Write a commandArcher function to tell your archers what to do!
# It should take one argument that will represent the archer passed to the function when it's called.
# Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
def commandarcher(archer):
enemy = archer.findNearest(archer.findenemies)
if enemy and archer.distanceTo(enemy) < 25:
self.command(archer, "attack", enemy)
else:
self.command(archer, "move", archer.pos)
loop:
pickUpCoin()
summonTroops()
friends = self.findFriends()
for friend in friends:
if friend.type == "soldier":
# This friend will be assigned to the variable soldier in commandSoldier
commandSoldier(friend)
elif friend.type == "archer":
commandarcher(friend)
Whatβs your CodeCombat username? Iβll check it out. It sounds like if you go to the hero selection screen and pick both a different hero and a different programming language, you might be able to switch it back to Python.
If nothing works @nick can someone just translate this to Javascript and give me the translated code!Thx
My code:
loop:
coin = self.findNearest(self.findItems())
enemy = self.findNearest(self.findEnemies())
friends = self.findFriends()
if coin:
self.move(coin.pos)
if self.gold >=20:
self.summon("soldier")
for friend in friends:
if friend.type is "soldier":
self.command(friend, "move", {'x':25, 'y':70})
elif enemy:
if friends:
for friend in friends:
if friend.type is "soldier":
self.command(friend, "attack", enemy)
for friend in friends:
if friend.type is "archer":
self.command(friend, "move", {'x':25, 'y':25})
elif enemy:
if friends:
for friend in friends:
if friend.type is "archer":
self.command(friend, "attack", enemy)
I need a answer in at least 2 days because I am falling behind everyone in my class!
try exploiting it for now i found a exploit by accident and the ogre attacked the soldiers instead
so i built summoned archers around the deers and the ogres did not spawn around them because they cant spawn near archer (summon not build and dont command)
How to translate the code into javascript:
- put β;β after each instruction. If you forget, the compiler will tell you to do it EASY.
- put a βvarβ to declare the variable each time you write a new variable. If you put too many var or not enough the compiler will tell you EASY
friends = self.findFriends()
becomes:
var friends = self.findFriends();
- Put the if or for in the parentheses
- Remove the β:β after if or for
- Put brackets β{β around the instructions inside the if or for
DIFFICULTY: STRAIGHT FORWARD
if friends:
for friend in friends:
if friend.type is "soldier":
self.command(friend, "attack", enemy)
becomes
if (friends){
for (friend in friends){
if (friend.type is "soldier"){
self.command(friend, "attack", enemy);
}
}
}
- Inside the βifβ argument python uses βisβ for equal, βnotβ for negation,
βis notβ for not equal. Javascript uses β==β, β!β and "!="
DIFFICULTY: EASY
Python JavaScript
is ==
not !
is not !=
so
if friend.type is "archer":
becomes:
if (friend.type == "archer"){
- In JavaScript the for in works different: it will give you the index of
an element instead of the object element.
You need to add this line immediately after the for:
friend=friends[i];
DIFFICULTY: YOU"LL FIGURE IT OUT
for friend in friends:
if friend.type is "archer":
self.command(friend, "attack", enemy)
becomes:
for (i in friends){
friend=friends[i];
if (friend.type == "archer"){
self.command(friend, "attack", enemy);
}
}
- Replace all self with this
self.command(friend, "attack", enemy)
becomes
this.command(friend, "attack", enemy);
Congratulations, you have now learned how to program in JavaScript !!
- Learn to program in both Python and Javascript, ask your schoolmates for help. Do not rely on code copied from internet.
DIFFICULTY: HARD BUT WORTH IT.
You know what my mind is getting messed up because I have many problems going on. So can someone just send me the translated code in my gmail!!Thx
PLEASE PLEASE PLEASE PLEASE
My gmail:
crummyface126@gmail.com