Advertisement
Guest User

forest code | /u/popcorncolonel

a guest
Jun 7th, 2014
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.11 KB | None | 0 0
  1. import sys
  2. import os
  3. import random
  4. import time
  5.  
  6. #if true, prints the month, #lumber and #deaths each month
  7. verbose = False
  8.  
  9. tree_init_percent = 0.5
  10. human_init_percent = 0.10
  11. bear_init_percent = 0.02
  12.  
  13. emptytile   = ' '
  14. saplingtile = 's'
  15. treetile    = 't'
  16. eldertile   = 'T'
  17. humantile   = '@'
  18. beartile    = 'b'
  19. bordertile  = '_'
  20.  
  21. #yearly counts
  22. lumber = 0
  23. deaths = 0
  24.  
  25. class Tile:
  26.     def __init__(self, parentBoard, char, location):
  27.         self.char = char #emptytile, saplingtile, treetile, eldertile, beartile, humantile
  28.         self.location = location #tuple of (x, y)
  29.         self.age = 0
  30.         self.parentBoard = parentBoard
  31.  
  32.     def move(self):
  33.         self.age += 1
  34.  
  35.         def sapling():
  36.             if self.age == 12:
  37.                 self.char = treetile
  38.  
  39.         def tree():
  40.             if self.age == 120:
  41.                 self.char = eldertile
  42.                 elder()
  43.                 return
  44.             # 10% chance to spawn sapling
  45.             if random.randint(0,9) == 0:
  46.                 pieces = self.parentBoard.get_adjacent_pieces(self)
  47.                 spaces = filter(lambda x:x.char == emptytile, pieces)
  48.                 if spaces != []:
  49.                     sapling_tile = random.choice(spaces)
  50.                     sapling_tile.char = saplingtile
  51.                     sapling_tile.age = 0
  52.  
  53.         def elder():
  54.             if random.randint(0,4) == 0:
  55.                 pieces = self.parentBoard.get_adjacent_pieces(self)
  56.                 spaces = filter(lambda x:x.char == emptytile, pieces)
  57.                 if spaces != []:
  58.                     sapling_tile = random.choice(spaces)
  59.                     sapling_tile.char = saplingtile
  60.                     sapling_tile.age = 0
  61.  
  62.         #selects a random adjacent blank spot to go to if one exists
  63.         def wander(tile):
  64.             spaces = filter(lambda x:x.char == emptytile, self.parentBoard.get_adjacent_pieces(self))
  65.             if spaces != []:
  66.                 loc = tile.location
  67.                 dest = random.choice(spaces)
  68.                 destloc = dest.location
  69.                 self.parentBoard.tilemap[destloc[0]][destloc[1]].char = tile.char
  70.                 self.parentBoard.tilemap[destloc[0]][destloc[1]].age = tile.age
  71.                 self.parentBoard.tilemap[loc[0]][loc[1]].char = emptytile
  72.  
  73.         def chop(tree):
  74.             global lumber
  75.             if tree.char == treetile:
  76.                 lumber += 1
  77.             elif tree.char == eldertile:
  78.                 lumber += 2
  79.             tree.char = emptytile
  80.  
  81.         def human():
  82.             found_tree = False
  83.             for i in range(3):
  84.                 pieces = self.parentBoard.get_adjacent_pieces(self)
  85.                 trees = filter(lambda x:x.char == treetile or x.char == eldertile, pieces)
  86.                 if trees != []:
  87.                     found_tree = True
  88.                     tree = random.choice(trees)
  89.                     break
  90.                 wander(self)
  91.             if found_tree:
  92.                 chop(tree)
  93.  
  94.         def eat(human):
  95.             global deaths
  96.             deaths += 1
  97.             human.char = emptytile
  98.  
  99.         def bear():
  100.             found_human = False
  101.             pieces = self.parentBoard.get_adjacent_pieces(self)
  102.             humans = filter(lambda x:x.char == humantile, pieces)
  103.             if humans != []:
  104.                 human = random.choice(humans)
  105.                 eat(human)
  106.             for i in range(5):
  107.                 wander(self)
  108.                 pieces = self.parentBoard.get_adjacent_pieces(self)
  109.                 humans = filter(lambda x:x.char == humantile, pieces)
  110.                 if humans != []:
  111.                     break
  112.  
  113.         options = {
  114.             saplingtile : sapling,
  115.             treetile : tree,
  116.             eldertile : elder,
  117.             beartile : bear,
  118.             humantile : human,
  119.             bordertile : lambda:None,
  120.             emptytile : lambda:None,
  121.         }
  122.  
  123.         options[self.char]()
  124.  
  125. class Board:
  126.     def __init__(self, size):
  127.         self.size = size #for a 3x3 (9 non-bordertile squares), size = 3.
  128.         self.tilemap = [[Tile(self, emptytile, (x, y)) for y in range(size+2)] for x in range(size+2)]
  129.         for i in range(size+2):
  130.             self.tilemap[0][i].char = bordertile
  131.             self.tilemap[i][0].char = bordertile
  132.             self.tilemap[size+1][i].char = bordertile
  133.             self.tilemap[i][size+1].char = bordertile
  134.  
  135.         #populate the forest
  136.         num_trees = int(tree_init_percent * (size * size))
  137.         spaces = filter(lambda x:x.char == emptytile, self.get_tile_list())
  138.         l = random.sample(spaces, num_trees)
  139.         for tree in l:
  140.             tree.char = treetile
  141.             tree.age = 0
  142.  
  143.         num_ppl = int(human_init_percent * (size * size))
  144.         spaces = filter(lambda x:x.char == emptytile, self.get_tile_list())
  145.         l = random.sample(spaces, num_ppl)
  146.         for human in l:
  147.             human.char = humantile
  148.             human.age = 0
  149.  
  150.         num_bears = int(bear_init_percent * (size * size))
  151.         spaces = filter(lambda x:x.char == emptytile, self.get_tile_list())
  152.         l = random.sample(spaces, num_bears)
  153.         for bear in l:
  154.             bear.char = beartile
  155.             bear.age = 0
  156.  
  157.     #returns a list of all the tiles in non-2d-array form
  158.     def get_tile_list(self):
  159.         fullist = []
  160.         for l in self.tilemap:
  161.             fullist.extend(l)
  162.         return fullist
  163.  
  164.     def move(self):
  165.         global lumber, deaths, verbose
  166.         tile_list = self.get_tile_list();
  167.         humans = filter(lambda x:x.char == humantile, tile_list)
  168.         bears  = filter(lambda x:x.char == beartile, tile_list)
  169.         spaces = filter(lambda x:x.char == emptytile, tile_list)
  170.         age = tile_list[0].age
  171.         if age % 12 == 0:
  172.             if lumber != 0 and lumber / len(humans) > 0: #add humans
  173.                 for i in range(lumber / len(humans)):
  174.                     if spaces != []:
  175.                         if verbose:
  176.                             print "adding",lumber / len(humans), "humans!"
  177.                         newspace = random.choice(spaces)
  178.                         newspace.char = humantile
  179.                         spaces = filter(lambda x:x.char == emptytile, spaces)
  180.                         humans = filter(lambda x:x.char == humantile, tile_list)
  181.             elif len(humans) < lumber: #destroy human
  182.                 if humans != []:
  183.                     if verbose:
  184.                         print "destroying human :(", len(humans), "humans and", lumber, "wood"
  185.                     deadhuman = random.choice(humans)
  186.                     deadhuman.char = emptytile
  187.                     spaces = filter(lambda x:x.char == emptytile, tile_list)
  188.                     humans = filter(lambda x:x.char == humantile, humans)
  189.             lumber = 0
  190.             if deaths == 0:
  191.                 if spaces != []:
  192.                     if verbose:
  193.                         print "No deaths! Adding a new bear!"
  194.                     newspace = random.choice(spaces)
  195.                     newspace.char = beartile
  196.                     spaces = filter(lambda x:x.char == emptytile, spaces)
  197.                     bears = filter(lambda x:x.char == beartile, tile_list)
  198.             else: #kill bear
  199.                 if bears != []:
  200.                     if verbose:
  201.                         print "Someone died! Killing bear!"
  202.                     deadbear = random.choice(bears)
  203.                     deadbear.char = emptytile
  204.                     spaces = filter(lambda x:x.char == emptytile, tile_list)
  205.                     bears = filter(lambda x:x.char == beartile, bears)
  206.             deaths = 0
  207. #add/subtract bears
  208.         for tile in tile_list:
  209.             tile.move()
  210.  
  211.     def print_board(self):
  212.         global lumber,deaths,verbose
  213.         forest = ""
  214.         for charlist in self.tilemap:
  215.             for tile in charlist:
  216.                 forest += tile.char
  217.             #for tile in charlist:
  218.             #    print tile.location,
  219.             forest += "\n"
  220.         os.write(1, forest)
  221.         if verbose:
  222.             print "Month", self.get_tile_list()[0].age
  223.             print lumber, "lumber"
  224.             print deaths, "deaths"
  225.  
  226.     #returns a list of Tiles that are not bordertiles
  227.     def get_adjacent_pieces(self, tile):
  228.         loc = tile.location
  229.         x = loc[0]
  230.         y = loc[1]
  231.         l = []
  232.         l.append(self.tilemap[x+1][y-1])
  233.         l.append(self.tilemap[x+1][y])
  234.         l.append(self.tilemap[x+1][y+1])
  235.         l.append(self.tilemap[x][y+1])
  236.         l.append(self.tilemap[x-1][y+1])
  237.         l.append(self.tilemap[x-1][y])
  238.         l.append(self.tilemap[x-1][y-1])
  239.         l.append(self.tilemap[x][y-1])
  240.         l = filter(lambda x:x.char != bordertile, l)
  241.         return l
  242.  
  243. size = 10
  244. delay = 1
  245. if len(sys.argv) == 2:
  246.     size = int(sys.argv[1])
  247. elif len(sys.argv) == 3:
  248.     size = int(sys.argv[1])
  249.     delay = float(sys.argv[2])
  250.  
  251. newBoard = Board(size)
  252. newBoard.print_board()
  253.  
  254. raw_input("press enter to start")
  255. while True:
  256.     try:
  257.         time.sleep(delay)
  258.     except (NameError, SyntaxError):
  259.         pass
  260.     newBoard.move()
  261.     newBoard.print_board()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement