Sarven Treasure Question About My Code


#1

Hello!

First of all, I would like to thank the creators of CodeCombat and all people who contributed to it. It’s an amazing experience and it’s my motivation to become better at programming!

Okay, now, here’s my problem. I tried to write a code, that makes my hero go to the nearest teleport as soon as an enemy approaches him. For some reason my code doesn’t work properly (hero goes to A all the time). I have no idea why.

Please help me fix my code.

Here it is:

A = {'x':5, 'y':49}
B = {'x':5, 'y':19}
C = {'x':77, 'y':51}
D = {'x':77, 'y':19}

dA = hero.distanceTo(A)
dB = hero.distanceTo(B)
dC = hero.distanceTo(C)

def safeTeleport():
    if all([dA < hero.distanceTo(point) for point in [B, C, D]]):
        hero.move(A)
    elif all([dB < hero.distanceTo(point) for point in [A, C, D]]):
    	hero.move(B)
    elif all([dC < hero.distanceTo(point) for point in [B, A, D]]):
        hero.move(C)
    else:
        hero.move(D)

while True:
    enemy = hero.findNearestEnemy()
    item = hero.findNearestItem()
    if enemy and hero.distanceTo(enemy) < 20:
        safeTeleport()
    elif item:
        hero.move(item.pos)

#2

And here’s the other question. Why does my hero gets stuck? He literally stops at some point.

while True:
    teleports = [{"x": 5.27, "y": 19.23}, {"x": 4.71, "y": 49.19}, {"x": 76.67, "y": 19.02}, {"x": 75.95, "y": 51.00}]
    teleport = hero.findNearest(teleports)
    coin = hero.findNearest(hero.findItems())
    enemy = hero.findNearest(hero.findEnemies())
    if enemy and hero.distanceTo(enemy) < 30:
        if hero.canCast("chain-lightning", enemy):
            hero.cast("chain-lightning", enemy)
    elif enemy and hero.distanceTo(enemy) < 8:
        if hero.canCast("invisibility", hero): 
            hero.cast("invisibility", hero)
            hero.move(coin.pos)
        else:
            hero.move(teleport)
    elif coin:
        hero.move(coin.pos)

#3

These are most likely only being calculated once at the beginning. Try doing the calculation in the function or recalculating them each loop.


#4

Does this code actually work the way you expect? Have you just tested moving to the teleports using this piece of code by itself?


#5

(1) Thank you, I’ll try that.

(2) teleport = hero.findNearest(teleports)
Yes, I tried and it works exactly as I wanted it to. I tried to implement the same using the code in (1), but it didnt’ work as I expected.


#6

Interesting, findNearest allows for a unit or a vector. I didn’t know that myself. Good to remember.

You are casting one spell only after you determine if it can be cast. Are you doing that for the other?


#7

Thanks a lot for help. I’m new to programming, started less than a month ago, right after I found the CodeCombat website. Your advice helped, I placed the statements into the function and everything worked smoothly.

As for (2), yes, I use “if hero.canCast()” for both spells I cast. I have found and fixed the problem with my code. It was the incorrect usage of if and elif statements. Anyway, thank you very much for help!

I don’t really understand what a vector is. I haven’t gotten Programmaticon V yet (seems like I need it to start understanding vectors), because I’m more into replayable levels and improving my code, than moving forward through the game. What I want to implement now is avoiding projectiles and arrows etc., but I have no idea how to do that (seems like it has to do with the vector stuff you mentioned). Do you know where I can get more info about vectors? I tried Googling it, but for some reason I didn’t find anything useful.


#8

@Serenity13 You are welcome.

Vectors
The glacier level opens up working with Vectors as well as the library of Vector functions. I would encourage you to keep progressing and return to levels like Sarven Treasure to continue leveling up.

Here is a brief introduction to vectors, but I do not know if you can use them without the Programmaticon V.

If you wanted to however, you could write your own vector functions. Bascially in the game a vector is considered a POS or position. {‘x’:53 , ‘y’:31} The direction, I believe, is considered from the origin to this point. ( vector definition )

class VectorLibrary:
    x = 0
    y = 0
    
    def __init__(self, newX, newY):
        if newX:
            self.x = newX
        if newY:
            self.y = newY
        return True

myVector = VectorLibrary(4,5)

hero.say(myVector.x + " " + myVector.y)

loop:
    hero.move( myVector )

#9

Thank you for your help! I will follow your advice and continue advancing through the levels. Have a good day and thanks again!