AlexeySapsay / Python_solution_CodeCombat_2018

In that repository I will post my sollution for codecombat.com games for study Python
1 stars 0 forks source link

Wishing Well #174

Open AlexeySapsay opened 6 years ago

AlexeySapsay commented 6 years ago
# Follow me on youtube  channel Phy Tu
# Help with programming on Python, school math, physics
# https://www.youtube.com/channel/UCP5ycahCEZ24qmRRgNnko5Q

# You need exactly 104 gold. 

less = "Nimis"
more = "Non satis"
requiredGold = 104

# This function calculates the sum of all coin values.
def sumCoinValues(coins):
    coinIndex = 0
    totalV = 0
    # Iterate all coins.
    while coinIndex < len(coins):
        totalV += coins[coinIndex].value
        coinIndex += 1
    return totalV

def collectAllCoins():
    item = hero.findNearest(hero.findItems())
    while item:
        hero.moveXY(item.pos.x, item.pos.y)
        item = hero.findNearest(hero.findItems())

while True:
    items = hero.findItems()
    # Get the total value of coins.
    goldAmount = sumCoinValues(items)
    # If there are coins, then goldAmount isn't zero.
    if len(items) != 0:
        # If goldAmount is less than requiredGold
        if goldAmount > 104:
            # Then say "Non satis".
            hero.say(less)

        # If goldAmount is greater than requiredGold
        if goldAmount <104:
            # Then say "Nimis".
            hero.say(more)
        # If goldAmount is exactly equal to requiredGold
        if goldAmount ==requiredGold:
            # Then collect all coins:
            break
        #pass

while True:
    items = hero.findItems()
    item = hero.findNearest(items)
    if item:
        collectAllCoins()