""" The Farm Ver 1.3 (thefarm.py) featuring... from the OST Skunkworks: a Freakish Tractor generator that moves forward to a boundary reading current character, writing current marker. Fuel might run out. The send method may be employed to tank up, redirect, change the marker. Farm is an n x m array, presumably smallish, for interactive console experiments. -- Python track, O'Reilly School of Technology http://www.facebook.com/oreillyschool """ import ost from ost.farmworld import Farm # an ascii canvas def tractor(thefarm , pos = [0,0], facing="N", marker="O", fuel=10): "pos is mutable = current position" while True: y,x = pos if fuel > 0: if facing == "N": if y > 0: y -= 1 else: return elif facing == "S": if y < thefarm.h - 1: y += 1 else: return elif facing == "W": if x > 0: x -= 1 else: return elif facing == "E": if x < thefarm.w - 1: x += 1 else: return fuel -= 1 pos = (y,x) else: return changes = yield (thefarm.field[y][x], pos, fuel) thefarm.field[y][x] = marker if changes: facing = changes.get('facing',facing) marker = changes.get('marker',marker) fuel = changes.get('fuel',fuel) pos = changes.get('pos',pos) def _test(n): # Agile Programming: TDD is your friend thefarm = Farm(20,20, bg = ".") print("Empty field, all is peaceful", thefarm, sep="\n\n") # frame of film t1 = tractor(thefarm, pos=[10,10], marker="O", facing="N") t2 = tractor(thefarm, pos=[10,11], marker="X", facing="S") thefarm.add(t1) thefarm.add(t2) print("Showing the tractors in a list: ", thefarm.tractors, "===", sep="\n") print("A busy day begins...", thefarm, sep="\n\n") # frame of film for arrow_of_time in range(n): thefarm.ticktock() # much physics involved in terms of what's a realistic throughput print("After {} ticks of the clock, the tractors have moved...".format(n), thefarm, sep="\n\n") # frame of film if __name__ == "__main__": _test(10)