# \[Solved\] Mad Maxer Gets Greedy

**URL:** <https://discourse.codecombat.com/t/solved-mad-maxer-gets-greedy/15893>\
**Category:** Uncategorized\
**Created:** [September 10, 2018, 3:09pm UTC](https://discourse.codecombat.com/t/solved-mad-maxer-gets-greedy/15893 "2018-09-10T15:09:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mratranslate](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@mratranslate](https://discourse.codecombat.com/u/mratranslate)\
**Post date:** [September 10, 2018, 3:09pm UTC](https://discourse.codecombat.com/t/solved-mad-maxer-gets-greedy/15893/1 "2018-09-10T15:09:56Z")

</div>

````python
while True:
    bestCoin = None
    maxRating = 0
    coinIndex = 0
    coins = hero.findItems()
    # Try calculating "value / distance" to decide which coins to get.
    for coin in coins:
        if coinIndex < len(coins):
            value = coin.value
            distance = hero.distanceTo(coin)
            if value / distance > maxRating:
                bestCoin = coin
                maxRating = value / distance
                coinIndex += 1 
                if bestCoin:
                    hero.move(bestCoin.pos)```

This is my code - can anyone help point me towards what I am doing wrong? Thanks
````

---

<div class="post-metadata">

**Author:** ![Archion](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/archion/32/26234_2.png) [@Archion](https://discourse.codecombat.com/u/Archion)\
**Post date:** [September 10, 2018, 3:58pm UTC](https://discourse.codecombat.com/t/solved-mad-maxer-gets-greedy/15893/2 "2018-09-10T15:58:42Z")

</div>

You have to update `coinIndex` after you move to the best coin.Also you should use moveXY because it is the one that’s required to use in this level. move() is from the mountain levels. And this level uses moveXY @mratranslate

---

<div class="post-metadata">

**Author:** ![mratranslate](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@mratranslate](https://discourse.codecombat.com/u/mratranslate)\
**Post date:** [September 11, 2018, 12:55pm UTC](https://discourse.codecombat.com/t/solved-mad-maxer-gets-greedy/15893/9 "2018-09-11T12:55:31Z")

</div>

```python
while True:
    bestCoin = None
    maxRating = 0
    coinIndex = 0
    coins = hero.findItems()
    # Try calculating "value / distance" to decide which coins to get.
    for coin in coins:
        if coinIndex < len(coins):
            value = coin.value
            distance = hero.distanceTo(coin)
            if value / distance > maxRating:
                bestCoin = coin
                maxRating = value / distance
                if bestCoin:
                    hero.moveXY(bestCoin.pos.x, bestCoin.pos.y)
                    coinIndex += 1 

```

I changed my code to add to index as you said but it doesn’t work still. Anya collects far too slow to beat the CPU and also at times it says “I can’t get there”. Also I used moveXY as you said but the actual hints say to use ‘move’. Either way it doesn’t work ☹

---

<div class="post-metadata">

**Author:** ![mratranslate](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@mratranslate](https://discourse.codecombat.com/u/mratranslate)\
**Post date:** [September 11, 2018, 1:15pm UTC](https://discourse.codecombat.com/t/solved-mad-maxer-gets-greedy/15893/10 "2018-09-11T13:15:43Z")

</div>

I got some help and it seems the final if statement should match-up to for coin in coins. Also I used ‘move’. Then it worked.
