Why is my code working for this level? [Sarven Desert - Mad Maxer Gets Greedy

So, this is the level where you need to get more coins than your doppelganger before time runs out.

With the help of the AI query, I was able to write a code that results in success. However, I cannot clearly explain to myself how and why this code works, which I personally believe is something I need to do in my future in programming (if any).

hero.warcry()

while True:
    bestCoin = None
    maxRating = 0
    coinIndex = 0
    coins = hero.findItems()
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        rating = coin.value / hero.distanceTo(coin)
        if rating > maxRating:
            bestCoin = coin
            maxRating = rating
        
        coinIndex += 1

    if bestCoin:
        if hero.isReady("warcry"):
            hero.warcry()
        elif not hero.isPathClear(hero.pos, bestCoin.pos):
            hero.say("Can't get there")
        else:
            hero.move(bestCoin.pos)

I’m especially curious as to why the need to assign a “maxRating” variable, the “rating” variable, and then comparing them in an if statement.

Is it to stop the while coinIndex < len(coins) loop that keeps looking through the coins array?

Why the need to assign the “maxRating” as the new “rating”? What purpose does it serve afterward? Does it stop the while loop it’s currently in?

Why divide the coin.value by the hero.distanceTo(coin) to get the “rating”? What does this determine?

After the code block inside the if bestCoin: statement is executed, is the code running back to the beginning of the while True: statement? If yes, does the values of all the variables reset because it’s written that way?

    bestCoin = None
    maxRating = 0
    coinIndex = 0
    coins = hero.findItems()

I’m going crazy on this for two days now, and I don’t want to move on from this level until I can clearly explain to myself how and why this code works, and made sure I can explain it to others.

EDIT: Any insight or example on how can this be applied to actual Python projects or scenarios is welcome or how it’s useful in any way is welcome.

Help!

1 Like

Hi @theCrimsonViper! Welcome to the CodeCombat Discourse! This is a safe place to chat and ask for level help. Before you begin your journey, could you please go through our rules that make this discourse an awesome place? It helps us all :grin: We hope you enjoy your stay!!

Now, to answer your questions, the maxRating variable is a variable meant to keep track of the current best coin you have seen yet. It starts at 0.

    bestCoin = None
    maxRating = 0
    coinIndex = 0
    coins = hero.findItems()

Then you go a loop through all the coins currently spawned, and assign a rating it each one.

    while coinIndex < len(coins):
        coin = coins[coinIndex]
        rating = coin.value / hero.distanceTo(coin)

If the coins rating is better than the current best rating, the current best coin will equal that coin, and the current maxRating will be that coins rating.

        if rating > maxRating:
            bestCoin = coin
            maxRating = rating

You devide the coin.value by hero.distanceTo(coin) because I assume you are trying to find the biggest coin to your hero, while making sure that the coin is not really far away from you.

Yes the variables do reset after the if bestCoin: statement. This way, if you went and collected the best coin, you will find a new best coin again.

This piece of code could be applied to many Python projects. It’s base essence is finding the nearest thing to another thing. It could be used in game dev for pathfinding, AI League for finding the nearest enemy to you, and many other things.

Hope this helps! Feel free to @iggmaster99 me if you need anything else!

2 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.