Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import os
- import random
- import time
- #if true, prints the month, #lumber and #deaths each month
- verbose = False
- tree_init_percent = 0.5
- human_init_percent = 0.10
- bear_init_percent = 0.02
- emptytile = ' '
- saplingtile = 's'
- treetile = 't'
- eldertile = 'T'
- humantile = '@'
- beartile = 'b'
- bordertile = '_'
- #yearly counts
- lumber = 0
- deaths = 0
- class Tile:
- def __init__(self, parentBoard, char, location):
- self.char = char #emptytile, saplingtile, treetile, eldertile, beartile, humantile
- self.location = location #tuple of (x, y)
- self.age = 0
- self.parentBoard = parentBoard
- def move(self):
- self.age += 1
- def sapling():
- if self.age == 12:
- self.char = treetile
- def tree():
- if self.age == 120:
- self.char = eldertile
- elder()
- return
- # 10% chance to spawn sapling
- if random.randint(0,9) == 0:
- pieces = self.parentBoard.get_adjacent_pieces(self)
- spaces = filter(lambda x:x.char == emptytile, pieces)
- if spaces != []:
- sapling_tile = random.choice(spaces)
- sapling_tile.char = saplingtile
- sapling_tile.age = 0
- def elder():
- if random.randint(0,4) == 0:
- pieces = self.parentBoard.get_adjacent_pieces(self)
- spaces = filter(lambda x:x.char == emptytile, pieces)
- if spaces != []:
- sapling_tile = random.choice(spaces)
- sapling_tile.char = saplingtile
- sapling_tile.age = 0
- #selects a random adjacent blank spot to go to if one exists
- def wander(tile):
- spaces = filter(lambda x:x.char == emptytile, self.parentBoard.get_adjacent_pieces(self))
- if spaces != []:
- loc = tile.location
- dest = random.choice(spaces)
- destloc = dest.location
- self.parentBoard.tilemap[destloc[0]][destloc[1]].char = tile.char
- self.parentBoard.tilemap[destloc[0]][destloc[1]].age = tile.age
- self.parentBoard.tilemap[loc[0]][loc[1]].char = emptytile
- def chop(tree):
- global lumber
- if tree.char == treetile:
- lumber += 1
- elif tree.char == eldertile:
- lumber += 2
- tree.char = emptytile
- def human():
- found_tree = False
- for i in range(3):
- pieces = self.parentBoard.get_adjacent_pieces(self)
- trees = filter(lambda x:x.char == treetile or x.char == eldertile, pieces)
- if trees != []:
- found_tree = True
- tree = random.choice(trees)
- break
- wander(self)
- if found_tree:
- chop(tree)
- def eat(human):
- global deaths
- deaths += 1
- human.char = emptytile
- def bear():
- found_human = False
- pieces = self.parentBoard.get_adjacent_pieces(self)
- humans = filter(lambda x:x.char == humantile, pieces)
- if humans != []:
- human = random.choice(humans)
- eat(human)
- for i in range(5):
- wander(self)
- pieces = self.parentBoard.get_adjacent_pieces(self)
- humans = filter(lambda x:x.char == humantile, pieces)
- if humans != []:
- break
- options = {
- saplingtile : sapling,
- treetile : tree,
- eldertile : elder,
- beartile : bear,
- humantile : human,
- bordertile : lambda:None,
- emptytile : lambda:None,
- }
- options[self.char]()
- class Board:
- def __init__(self, size):
- self.size = size #for a 3x3 (9 non-bordertile squares), size = 3.
- self.tilemap = [[Tile(self, emptytile, (x, y)) for y in range(size+2)] for x in range(size+2)]
- for i in range(size+2):
- self.tilemap[0][i].char = bordertile
- self.tilemap[i][0].char = bordertile
- self.tilemap[size+1][i].char = bordertile
- self.tilemap[i][size+1].char = bordertile
- #populate the forest
- num_trees = int(tree_init_percent * (size * size))
- spaces = filter(lambda x:x.char == emptytile, self.get_tile_list())
- l = random.sample(spaces, num_trees)
- for tree in l:
- tree.char = treetile
- tree.age = 0
- num_ppl = int(human_init_percent * (size * size))
- spaces = filter(lambda x:x.char == emptytile, self.get_tile_list())
- l = random.sample(spaces, num_ppl)
- for human in l:
- human.char = humantile
- human.age = 0
- num_bears = int(bear_init_percent * (size * size))
- spaces = filter(lambda x:x.char == emptytile, self.get_tile_list())
- l = random.sample(spaces, num_bears)
- for bear in l:
- bear.char = beartile
- bear.age = 0
- #returns a list of all the tiles in non-2d-array form
- def get_tile_list(self):
- fullist = []
- for l in self.tilemap:
- fullist.extend(l)
- return fullist
- def move(self):
- global lumber, deaths, verbose
- tile_list = self.get_tile_list();
- humans = filter(lambda x:x.char == humantile, tile_list)
- bears = filter(lambda x:x.char == beartile, tile_list)
- spaces = filter(lambda x:x.char == emptytile, tile_list)
- age = tile_list[0].age
- if age % 12 == 0:
- if lumber != 0 and lumber / len(humans) > 0: #add humans
- for i in range(lumber / len(humans)):
- if spaces != []:
- if verbose:
- print "adding",lumber / len(humans), "humans!"
- newspace = random.choice(spaces)
- newspace.char = humantile
- spaces = filter(lambda x:x.char == emptytile, spaces)
- humans = filter(lambda x:x.char == humantile, tile_list)
- elif len(humans) < lumber: #destroy human
- if humans != []:
- if verbose:
- print "destroying human :(", len(humans), "humans and", lumber, "wood"
- deadhuman = random.choice(humans)
- deadhuman.char = emptytile
- spaces = filter(lambda x:x.char == emptytile, tile_list)
- humans = filter(lambda x:x.char == humantile, humans)
- lumber = 0
- if deaths == 0:
- if spaces != []:
- if verbose:
- print "No deaths! Adding a new bear!"
- newspace = random.choice(spaces)
- newspace.char = beartile
- spaces = filter(lambda x:x.char == emptytile, spaces)
- bears = filter(lambda x:x.char == beartile, tile_list)
- else: #kill bear
- if bears != []:
- if verbose:
- print "Someone died! Killing bear!"
- deadbear = random.choice(bears)
- deadbear.char = emptytile
- spaces = filter(lambda x:x.char == emptytile, tile_list)
- bears = filter(lambda x:x.char == beartile, bears)
- deaths = 0
- #add/subtract bears
- for tile in tile_list:
- tile.move()
- def print_board(self):
- global lumber,deaths,verbose
- forest = ""
- for charlist in self.tilemap:
- for tile in charlist:
- forest += tile.char
- #for tile in charlist:
- # print tile.location,
- forest += "\n"
- os.write(1, forest)
- if verbose:
- print "Month", self.get_tile_list()[0].age
- print lumber, "lumber"
- print deaths, "deaths"
- #returns a list of Tiles that are not bordertiles
- def get_adjacent_pieces(self, tile):
- loc = tile.location
- x = loc[0]
- y = loc[1]
- l = []
- l.append(self.tilemap[x+1][y-1])
- l.append(self.tilemap[x+1][y])
- l.append(self.tilemap[x+1][y+1])
- l.append(self.tilemap[x][y+1])
- l.append(self.tilemap[x-1][y+1])
- l.append(self.tilemap[x-1][y])
- l.append(self.tilemap[x-1][y-1])
- l.append(self.tilemap[x][y-1])
- l = filter(lambda x:x.char != bordertile, l)
- return l
- size = 10
- delay = 1
- if len(sys.argv) == 2:
- size = int(sys.argv[1])
- elif len(sys.argv) == 3:
- size = int(sys.argv[1])
- delay = float(sys.argv[2])
- newBoard = Board(size)
- newBoard.print_board()
- raw_input("press enter to start")
- while True:
- try:
- time.sleep(delay)
- except (NameError, SyntaxError):
- pass
- newBoard.move()
- newBoard.print_board()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement