The Farmer Was Replaced

The Farmer Was Replaced

Kitkatbars 19. mai 2024 kl. 17.43
My pumpkin script
I'm fairly new to coding, so this took me a minute to figure out, but I thought I'd share for feedback/help for others.

Mid-tier function after unlocking pumpkins; scales with world size.

There are probably better ways, but this function fits nicely in my main plotting loop where I have a lot of switches based on item quantity.

spoilers below:

get_world_size() grown_plots = 0 total_plots = get_world_size()*get_world_size() def are_all_plots_pumpkins(): for i in range(get_world_size()): for i in range(get_world_size()): if get_entity_type() == Entities.Pumpkin and can_harvest() == True: move(North) grown_plots = grown_plots + 1 else: harvest() while (num_items(Items.Pumpkin_Seed) <= total_plots): trade(Items.Pumpkin_Seed) plant(Entities.Pumpkin) move(North) move(East) if grown_plots == total_plots: harvest() while True: are_all_plots_pumpkins()
Sist redigert av Kitkatbars; 19. mai 2024 kl. 19.50
< >
Viser 14 av 4 kommentarer
b07 20. mai 2024 kl. 11.21 
pretty good. I'd add a check before planting to see if the tile is tilled and till it, if for whatever reason you use it outside your main loop on a cleared map.

when you get dictionaries you can optimize to only visit tiles we are waiting to be grown
Hazir 21. mai 2024 kl. 10.41 
that's a pretty neat approach

i only optimized pumpkins after i unlocked fertilizer
with a watered tile + fertilizer they grow instantly so you can immediately re-plant if it dies and move on serially without any need to backtrack
Luka 22. mai 2024 kl. 13.42 
There's a heap of bugs in that code:
* There's no point in comparing the result of can_harvest() with true (can_harvest() == True) just have the call to can_harvest there, it already returns a boolean.
* If the drone reaches a tile that contains a not grown pumpkin you harvest it, that doesn't make sense.
* You then buy seeds until you have as many as there are tiles on the farm, why not just do this before the loop where you traverse the farm, then you should never end up with a random amount of seeds after a run of the function.
* After moving across the entire field you check that the grown_plots variable is equal to the total_plots variable, this will never be true if you hit the "True" case of your if statement only some of the times during the traversal of the farm, the grown_plots variable will just continue to grow, you might want to reset it at the start of the function.
b07 23. mai 2024 kl. 8.54 
Opprinnelig skrevet av Luka:
There's a heap of bugs in that code:

I wouldn't call them bugs (except one), I've ran the code and it works as expected without any bugs.

Opprinnelig skrevet av Luka:
* There's no point in comparing the result of can_harvest() with true (can_harvest() == True) just have the call to can_harvest there, it already returns a boolean.
Sure, it's redundant.

Opprinnelig skrevet av Luka:
* If the drone reaches a tile that contains a not grown pumpkin you harvest it, that doesn't make sense.
Agree, it's not really necessary, but maybe he hasn't cleared his field and maybe he is harvesting if there was something else underneath from the previous plantation.

Opprinnelig skrevet av Luka:
* You then buy seeds until you have as many as there are tiles on the farm, why not just do this before the loop where you traverse the farm, then you should never end up with a random amount of seeds after a run of the function.
I found it strange as well, but even if he buys the amount of tiles in seeds before the run he has to buy more if any pumpkin doesn't grow to full. I would just buy a seed before each plant().

Opprinnelig skrevet av Luka:
* After moving across the entire field you check that the grown_plots variable is equal to the total_plots variable, this will never be true if you hit the "True" case of your if statement only some of the times during the traversal of the farm, the grown_plots variable will just continue to grow, you might want to reset it at the start of the function.
This is the only bug I think, that I missed in my previous review. Indeed the grown_plots needs to be reset after the condition is met. HOWEVER in-game this is not the case, it seems to be resetting on it's own? I can't figure it out but I tested it and and it works, the grown_plots is somehow reset on every run.

You've made good points, but your attitude wasn't really all that good towards a newbie coder.

EDIT: I guess the line grown_plots = grown_plots + 1 doesn't use the global variable and instead makes a new one for each cycle
Sist redigert av b07; 23. mai 2024 kl. 9.23
< >
Viser 14 av 4 kommentarer
Per side: 1530 50

Dato lagt ut: 19. mai 2024 kl. 17.43
Innlegg: 4