I need help with the usual day level codecombat lua


#1

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


#2

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.


#3

sorry i mean lua :sweat_smile:


#4

i don,t understand

:grinning:


#5

Which part?

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

UsualDay


#6

the bottom part
:grinning:


#7

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:


#8

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

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


#9

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.


#10

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


#11

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.


#12

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


#13

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.


#14

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


#15
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)


#16

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


#17

Just move to the coin.


#18

?:anguished::grimacing:


#19

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


#20

i now but what code
:disappointed_relieved: