Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Mixed Unit Tactics - Python

It says that soldier is not defined. Help.

# Practice using modulo to loop over an array

# Choose the mix and order of units you want to summon by populating this array:
summonTypes = ["soldier", "archer", "griffin-rider"]
totalGold = 0
def summonTroops():
    # Use % to wrap around the summonTypes array based on len(hero.built)
    #type = summonTypes[???]
    len(hero.built) % len(summonTypes)
    pass

def collectCoins():
    coin = hero.findNearestItem()
    if coin:
        hero.move(coin.pos)
        totalGold = totalGold + coin.value
    pass

def commandTroops():
    hero.command(soldier, 
        "attack", enemy)
    hero.command(archer, "attack", enemy)
    hero.command(griffin-rider, "attack", enemy)
    if friend.type == "palisade": 
        hero.move(coin.pos)
    pass

while True:
    collectCoins()
    summonTroops()
    commandTroops()
1 Like

we can solve it together if i can get the link from you and if you want the solution we could go into a DM? :space_invader:

Here is the link.
https://codecombat.com/play/level/mixed-unit-tactics?

thxs

@Code_Kid13

it says summonTypes isn’t defined. I could probably define it before the def summonTroops() though

yes try that and i wish you luck because i am stuck on hunter valley

it doesn’t work. maybe it is that in the while true, the summonTroops and commandTroops is before collectCoins. You need coins to summon.

A huge problem is that the code Zax provided was written prior to about 2017…they changed the game engine and no longer use ‘self’; ‘loop’ has been replaced too.

Otherwise, yes, you must define summonTypes and do so above the function. But, since you are not making use of the variable ‘type’, why bother?

Since you are testing to see if you have enough gold, it does not matter what order the call statements are in.

I completed Hunter Valley first try. You just model the moveUpRight function the same way as the moveDownRight function, except you put in the right information.

Oh. That’s why
[20 characters]

1 Like

Hi guys,
I’ve noticed a lot of argument here so I want to clarify some things here. @dedreous it’s recommended that you flag an off-topic post instead of replying here because it sends a notification in the moderator inbox, and it’s easier for us to help. @ZAX155 please refrain from bringing off-topic discussions outside of a thread, as it’s confusing and it stops the flow of the thread.
Kind Regards,
@Chaboi_3000

1 Like

Can you help me with my problem?(code posted at top)

sorry i cannot help you out now

I mean’t Chaboi_3000

wow

[en_US.composer.my_button_text]

OK, this code has quite a few problems. Mostly because it’s missing essential elements.
Let’s start from the top:

First of all, the line where you find the type of the unit you want to summon.
Think about this line:

len(hero.built) % len(summonTypes)

Both of these values are numbers. Can you summon a number? Which array holds the types?
Read this comment carefully:

Next, once you’ve got a type variable you need to summon that type. You should be able to do that.
Your collectCoins function looks good, except of the totalGold adding up, but that doesn’t really matter, you don’t use it anywhere so it’s fine.
Next this bit:

To start with you don’t need write anything about the palisade, and you don’t need to move anywhere here; your collectCoins function does that.

Secondly, the main problem with this function:
The error message should help you; read it carefully:

When you ‘define’ something in programming it means you make a variable for it.
enemy = hero.findNearestEnemy() this ‘defines’ the variable enemy by storing a ‘thing’ (technical term :wink:) inside it.
Where have you made a variable called ‘soldier’? The code isn’t going to be able to command nothing!
I would recommend using one, or multiple for loops to command your troops, which means you’ll have to make some arrays.
I hope this helps, please post your new code once you’ve read this.
Danny

I defined soldier as summonTypes[0] and archer as summonTypes[1] in the def summonTroops(). Here is the new code:

# Practice using modulo to loop over an array

# Choose the mix and order of units you want to summon by populating this array:
summonTypes = ["soldier", "archer"]
totalGold = 0
def summonTroops():
    # Use % to wrap around the summonTypes array based on len(hero.built)
    #type = summonTypes[???]
    type = summonTypes["soldier", "archer"]
    len(hero.built) % len(summonTypes)
    soldier = summonTypes[0]
    archer = summonTypes[1]
    pass

def collectCoins():
    coin = hero.findNearestItem()
    if coin:
        hero.move(coin.pos)
        totalGold = totalGold + coin.value
    pass

def commandTroops():
    hero.command(soldier, 
        "attack", enemy)
    hero.command(archer, "attack", enemy)
    pass

while True:
    collectCoins()
    summonTroops()
    commandTroops()

This code says that Line 23 should have type unit, but got array.

# Practice using modulo to loop over an array

# Choose the mix and order of units you want to summon by populating this array:
summonTypes = ["soldier", "archer"]
totalGold = 0
def summonTroops():
    # Use % to wrap around the summonTypes array based on len(hero.built)
    #type = summonTypes[???]
    type = summonTypes["soldier", "archer"]
    len(hero.built) % len(summonTypes)
    pass

def collectCoins():
    coin = hero.findNearestItem()
    if coin:
        hero.move(coin.pos)
        totalGold = totalGold + coin.value
    pass
def commandTroops():
    soldier = hero.findFriends()
    archer = hero.findFriends()
    enemy = hero.findNearestEnemy()
    hero.command(soldier, 
        "attack", enemy)
    hero.command(archer, "attack", enemy)
    pass

while True:
    collectCoins()
    summonTroops()
    commandTroops()

Some how, you inserted a line break at line 23:

 hero.command(soldier, 
        "attack", enemy)

all of that needs to be on a single line

the way my cursor is aligning is messed up