// Ogre Witches have some unpleasant surprises ready for you.
int main() {
// 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.
hero.commandArcher = function(archer) {
auto enemy = archer.findNearestEnemy();
if (enemy) {
if (archer.distanceTo(enemy) < 10) {
hero.command(archer, "attack", enemy);
}
}
};
hero.commandSoldier = function(soldier) {
// Soldiers should attack enemies.
auto enemies = hero.findEnemies();
auto ogre = hero.findByType("ogre");
auto thrower = hero.findByType("thrower");
auto witch = hero.findByType("witch");
auto nearestEnemy = hero.findNearest(witch);
if (enemies) {
if (witch) {
hero.command(soldier, "attack", witch);
}
}
};
while(true) {
auto friends = hero.findFriends();
auto enemies = hero.findEnemies();
auto witch = hero.findByType("witch");
for(int i=0; i < friends.size(); i++) {
if (friends.type == "soldier") {
hero.commandSoldier(friend);
}
if (friends.type == "archer") {
hero.commandArcher(friend);
}
}
}
return 0;
};
What is the problem with your code? Is there an error that comes up?
error =
expected a : line 7
only witch survive and its keep healing
There are issues with the logic inside your hero.commandSoldier
and hero.commandArcher
functions. You should be checking the type of your friends and enemies correctly.
void chooseTarget(const Unit &friend) {
if (friend.getType() == "soldier") {
# Soldiers should attack witches if any exist.
auto witches = hero.findByType("witch");
if (!witches.empty()) {
hero.command(friend, "attack", witches[0]);
}
} else if (friend.getType() == "archer") {
# Archers should attack the nearest enemy.
}
}
}
int main() {
while (true) {
auto friends = hero.findFriends();
for (int i = 0; i < friends.size(); i++) {
chooseTarget( );
# You should pass friend as your argument
}
}
return 0;
}
Changes made:
- The
chooseTarget
function is defined with proper conditional statements to check the type of friend and command them accordingly. - The
for
loop iterates through thefriends
array, and thechooseTarget
function is called for each friend. - The logic for selecting targets is improved to follow the game’s requirements.
You can complete the remaining portions of your code by following the logic presented in the sample code. Please disregard the comments that are formatted for Python; these comments are included for the purpose of enhancing code clarity, as the formatting exclusively accommodates Python.
PS. I don’t do C++, so I used an language translator to do this, there might be some bugs that you will have to debug yourself
i sucess i attack the witch first