The Farmer Was Replaced

The Farmer Was Replaced

If you need some help with trees
Here's a function to determine whether any given tile on the grid should be plantable, regardless of size. Bottom left should always be allowed to plant trees, checkered out from there. I'm sure it can be done more efficiently, but this is what my brain could handle.

def checker_grid(x,y): worldArea = get_world_size() * get_world_size() grid_plot = [] for i in range(worldArea): if((get_world_size() % 2) > 0): if(( i % 2 ) > 0): grid_plot.append(0) else: grid_plot.append(1) else: if( i > get_world_size()): if((grid_plot) == 1):
grid_plot.append(0)
else:
grid_plot.append(1)
else:
if(( i % 2 ) > 0):
grid_plot.append(0)
else:
grid_plot.append(1)

if(grid_plot[(y*get_world_size()+x)] == 1):
return True
else:
return False[/code]
< >
Showing 1-15 of 19 comments
Oof some of my code was snipped due to square brackets. Weird. Disregard!
A few notes... you can actually store True/False directly in your array, so rather than appending 0 and 1 you can just append False and True, respectively... then you can drop your comparisons to 1 for your truth values, and your last block becomes simply:
return grid_plot[y*get_world_size() + x]

A bigger change, your whole loop can be collapsed down to a simple expression. Consider that if the world size is odd you already _almost_ have this expression. When the world size is even, the result of multiplying the y pos with the world size means that you're never changing the parity of your result based on your y position. Have a play around and see if you can get a similar result without multiplying by world size, which means you can use the same algorithm regardless of the parity of the world size.
Originally posted by SharkBite:
A few notes... you can actually store True/False directly in your array, so rather than appending 0 and 1 you can just append False and True, respectively... then you can drop your comparisons to 1 for your truth values, and your last block becomes simply:
return grid_plot[y*get_world_size() + x]

A bigger change, your whole loop can be collapsed down to a simple expression. Consider that if the world size is odd you already _almost_ have this expression. When the world size is even, the result of multiplying the y pos with the world size means that you're never changing the parity of your result based on your y position. Have a play around and see if you can get a similar result without multiplying by world size, which means you can use the same algorithm regardless of the parity of the world size.

Ah, the 1/0 translation there was due to an error earlier in setup, where I realized that you can't set the value of an index directly. I thought the problem was passing boolean values through the array initially, and I guess I never changed it back.

And you're totally right, I don't even need the top half of the code, I think. Rewriting time! <3
EDIT: Shortened this a whole lot, dropped the entire list, and it now runs FAR faster. For your viewing pleasure:

def checker_grid(x,y): worldArea = get_world_size()*get_world_size() if(is_even(abs(x-y))): return True else: return False

100% reliable regardless of world size, and VERY fast. If you use the is_even function from the Trees hint, this is golden.

Tested over many iterations with completely random movement.
Can you drop your whole tree code?
What if
def checker_grid ( x , y ): return is_even ( x + y )
For what it's worth most of this is unnecessary. All you need to plant tree's is this.

if (get_pos_x() + get_pos_y()) % 2 == 0:
plant(Entities.Tree)
else:
plant(Entities.Bush)
Originally posted by SpaZz:
For what it's worth most of this is unnecessary. All you need to plant tree's is this.

if (get_pos_x() + get_pos_y()) % 2 == 0:
plant(Entities.Tree)
else:
plant(Entities.Bush)
Oh yeah I also use one iff function, mine still was quickly thrown together where I just check X and Y position to be both even or both not even.
clear() size = get_world_size() while(True): for x in range(size): for y in range(size): if get_ground_type() != Grounds.Turf: till() if get_water() < 0.5: use_item(Items.Water_Tank) if can_harvest(): harvest() if (((size - get_pos_x()) % 2) == 0): if (((size - get_pos_y()) % 2) == 0): plant(Entities.Tree) else: plant(Entities.Bush) else: if (((size - get_pos_y()) % 2) == 0): plant(Entities.Bush) else: plant(Entities.Tree) move(North) move(East)
My initial attempt. It's not optimal, but does the trick.
well now i feel silly. (x +y) % 2 == 0 is so much cleaner than mine. Not sure why I decided I needed to do boolean comparisons when maths does it for me.

mine is now:

def watering(): while get_water() < .75: use_item(Items.Water_Tank) def treeing(): watering() if (get_pos_x() + get_pos_y()) % 2 == 0: if get_ground_type() != Grounds.Soil: till() plant(Entities.Tree) return True else: return False
This way I can do
for i in range(field): if treeing(): treeing() else: carroting()
for example
Nice. I ended up with something similar:

if get_pos_x() % 2 == get_pos_y() % 2
I did:


feld = 6
def ufe():
for j in range(feld):
if get_ground_type() == Grounds.Turf:
till()

if get_water() < 0.25:
use_item(Items.Water_Tank)
if can_harvest():
harvest()
if j % 2 == 0:
plant(Entities.Tree)
move(North)

def ufez():
for j in range(feld):
if get_ground_type() == Grounds.Turf:
till()

if get_water() < 0.25:
use_item(Items.Water_Tank)
if can_harvest():
harvest()
if j % 2 != 0:
plant(Entities.Tree)
move(North)

while True:
for k in range(feld//2):
ufe()
move(East)
ufez()
move(East)






"ufe" means up in swissgerman. so i define the field size in "feld" wich i still do manualy. then i iterate through the fields and plant once on each even number then once on every odd number.
Originally posted by SpaZz:
For what it's worth most of this is unnecessary. All you need to plant tree's is this.

if (get_pos_x() + get_pos_y()) % 2 == 0:
plant(Entities.Tree)
else:
plant(Entities.Bush)
Thanks mate, this help me a lot
Originally posted by SpaZz:
For what it's worth most of this is unnecessary. All you need to plant tree's is this.

if (get_pos_x() + get_pos_y()) % 2 == 0:
plant(Entities.Tree)
else:
plant(Entities.Bush)

i got on 2 functions

the 1st:

def post():
for i in range(get_world_size()):
if (get_pos_x() + get_pos_y()) % 2 == 0:
plant(Entities.Tree)
else:
plant(Entities.Bush)
def get():
if(can_harvest()):
harvest()
post()
---------------------------------------------------
the 2nd:

import farm_tree
clear()
do_a_flip()
while(True):
for i in range(get_world_size()):
if(get_pos_x()<get_world_size()):
farm_tree.post()
farm_tree.get()
move(East)
move(North)
< >
Showing 1-15 of 19 comments
Per page: 1530 50