It has been two years since the last guide I made, that one was for beginners, and this one is more advanced! In this guide, I will list out some more advanced code/functions for the game, as other aspects (working on hero choosing/what to buy) are just secondary.
Programming languages: I would only recommend Python or JavaScript as they are easy to learn and are actually complete for the CodeCombat environment. Any other languages in the game, are either incomplete, or just a mixture of multiple different others (e.g. Java).
For Loop
A for loop is a way to repeat a block of code for each item in a list or sequence. It’s like a recipe that tells the computer to do something over and over again, once for each item in the list. For example, you could use a for loop to print each name in a list of names, or to calculate the total of a list of numbers. For loops are handy for automating tasks that need to be done repeatedly.
Here are some types of for-loops.
Normal for-loop
The loop uses the range()
function to iterate over numbers, it consists of four parts, an index, starting number, ending number, and the increment. The for-loop starts with the starting number, and will increment until it’s greater or equal to the ending number.
for index in range(startNum, endNum, increment):
Sometimes you can just set the argument to the endNum. This way, the startNum will default to 0, and the increment will default to 1. Here is a piece of code we are more familiar with:
enemies = hero.findEnemies()
for index in range(len(enemies)):
# Which is actually the same as
for index in range(0, len(enemies), 1):
for-each loop
As it says, it iterates for each item in the array. You will need to set up a varible standing for the current object in the array.
for enemy in enemies:
while enemy.health > 0: # Need to check health or else the hero will just
# attack each enemy ONCE in the list, then moving on
hero.attack(enemy)
You can enhance for loops in many ways. Here are some examples:
def findBestEnemy():
enemies = hero.findEnemies()
bestValue = 0
best = None
for enemy in enemies:
if enemy.id == "Hero Placeholder 1":
continue
if enemy.type == "fake-peasant" or enemy.type == "decoy":
continue
distance = hero.distanceTo(enemy)
if distance == 0: # Avoid division by 0
value = float('inf') # Infinity
else:
value = enemy.health / distance ** 2
if value > bestValue:
bestValue = value # Assigning the new best value
best = enemy
return best
In the example above, the code will iterate and find the best enemy, but will ignore decoy types or the enemy hero. Notice the usage of powers, the enemies will get stronger by going by a curve, so a enemy gets increasingly more threatful when it comes closer.
def findBlinkPos(target):
maxDistance = 0
bestPos = None
gridStep = 5
for x in range(0, 101, gridStep): # x position on x axis
for y in range(0, 101, gridStep): # y position on y axis
# Euclidean method
distanceToTarget = ((target.pos.x - x) ** 2 + (target.pos.y - y) ** 2) ** 0.5
distanceToHero = hero.distanceTo(Vector(x, y))
if distanceToTarget > maxDistance:
maxDistance = distanceToTarget
bestPos = Vector(x, y)
return bestPos
In this case, we make a grid using a nested for-loop, and we calculate the point on the grid that is the farthest to the target.
Both examples deliver a function of finding the best object by assigning values to a better case, a for-loop is very helpful in these scenarios.
Vectors
Complex Math Functions
Here are examples where you can use math to solve some common problems.
(1) GCD
Calculates the greatest common factor of two numbers.
def gcd(a, b):
while b:
a, b = b, a % b
return a
# Example usage
hero.say(gcd(48, 18)) # We will get 6
Posting now, but not yet finished, keep in look for more updates