I need help with the usual day level codecombat lua

i need help with the usual day level here,s the code

– Defeat munchkins, collect coins. Everything as usual.
– Use AND to check existence and type in one statement.

while true do
local enemy = hero:findNearestEnemy()
– With AND, the type is only checked if enemy exists.
if enemy and enemy.type == “munchkin” then
hero:attack(enemy)
end
– Find the nearest item.
item = hero:findNearestItem()

-- Collect item if it exists and its type is "coin".

end

1 Like

It is a little hard for me to tell what is going on, but this doesn’t look like javascript.

Please format your code. See this link about how to format your code. Put three backticks, then on the next line copy and paste all of your code, then put three more backticks.

You want to follow the pattern from the first part of this program

Here is the first part:

local enemy = hero:findNearestEnemy()
-- With AND, the type is only checked if enemy exists.
if enemy and enemy.type == "munchkin" then
    hero:attack(enemy)
end

The idea is to do the same thing in the next part, but instead of enemies, munchkins and attacking it will be items, coins and moving.

1 Like

sorry i mean lua :sweat_smile:

i don,t understand

:grinning:

Which part?

Here is an image to try to say the same thing:

UsualDay

3 Likes

the bottom part
:grinning:

1 Like

So pretty much it’s the exact same thing as the top part but with local item = hero:findNearestItem() instead of local enemy = hero:findNearestEnemy(), and coin instead of munchkin. So if the top part is

local enemy = hero:findNearestEnemy()
if enemy and enemy.type == "munchkin" then

then the bottom part should just replace a couple of things, as I mentioned above and @Mr-Borges did as well.

I think you can take it from here :slightly_smiling_face:

3 Likes

but i don,t understand what you need to write the same as

if enemy and enemy.type == “munchkin” then

Your hero needs to attack only the munchkins, so the hero checks the enemy’s type before attacking. Same deal with the items. Your hero only needs to collect the coins, right? So the hero needs to check if there is an item and if that item is a coin.

2 Likes

but i don,t now the strategy for looping with items

The first part is already and correctly done. Look at it and write the second part of the code using the same strategy, but instead of enemies, look at the items, and try to find coins instead of munchkins.

1 Like

but i don,t now what for code to write for the coins

It’s the exact same as the first part, but instead of enemy = hero.findNearestEnemy(), write item = hero.findNearestItem() because you are looking for the items and not enemies. Also replace if enemy and enemy.type == "munchkin": with if item and item.type == "coin": because now you are looking for items with type coin instead of enemies with type munchkin.

1 Like

but i now understand the item part but its still wrong but this is my code 4 now

– Defeat munchkins, collect coins. Everything as usual.
– Use AND to check existence and type in one statement.

while true do
local enemy = hero:findNearestEnemy()
– With AND, the type is only checked if enemy exists.
if enemy and enemy.type == “munchkin” then
hero:attack(enemy)
end
– Find the nearest item.
local item = hero:findNearestItem()

-- Collect item if it exists and its type is "coin".

if item and item.type == “coin”

then
end
end

if item and item.type == "coin" then

What do you do to the coin now that you’ve found it?
(Hint: it’s not attack)

then you need to pick it up but i don,t now how

Just move to the coin.

?:anguished::grimacing:

Your hero will pick up coins automatically when your hero moves over them.

i now but what code
:disappointed_relieved: