Skip to content

Instantly share code, notes, and snippets.

@Leth01
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leth01/72697109cbe51578a9a9 to your computer and use it in GitHub Desktop.
Save Leth01/72697109cbe51578a9a9 to your computer and use it in GitHub Desktop.
import sys,os,time,copy,math
from random import randrange
class Map:
def __init__(self,size):
#Generates map.
#Generates a forest based on the attributes#
#Lumberjack - 10% #
#Trees -
#Bears -
self.map = [["." for x in xrange(size)] for x in xrange(size)]
self.lumberjacks = []
self.trees = []
self.bears = []
self.tickCount = 0
self.yearlyLumber = 0
self.monthlyLog = []
self.yearlyLog = []
self.monthlySaplings = 0
#Spawns lumberjacks - 10%#
for i in range(0,int(round(((size*size)*0.1)))):
x,y = findLocation(self.map)
lumberjack = Lumberjack(x,y)
self.lumberjacks.append(lumberjack)
self.calculateMap(size)
#Spawns trees - 50% #
for i in range(0,int(round(((size*size)*0.5)))):
x,y = findLocation(self.map)
tree = Tree("Tree",x,y)
self.trees.append(tree)
self.calculateMap(size)
#Spawns bears - 2% #
for i in range(0,int(round(((size*size)*0.02)))):
x,y = findLocation(self.map)
bear = Bear(x,y)
self.bears.append(bear)
self.calculateMap(size)
def displayMap(self):
#Displays the map as 2D ASCII#
mapDisplay = ""
for x in self.map:
for y in x:
mapDisplay = mapDisplay + str(y)
mapDisplay = mapDisplay + "\n"
print(mapDisplay)
def calculateMap(self,size):
self.map = [["." for x in xrange(size)] for x in xrange(size)]
for tree in self.trees:
x,y = tree.getLocation()
self.map[y][x] = tree
for lumberjack in self.lumberjacks:
x,y = lumberjack.getLocation()
self.map[y][x] = lumberjack
for bear in self.bears:
x,y = bear.getLocation()
self.map[y][x] = bear
def tick(self):
self.tickCount += 1
self.monthlyLog = []
#Tree tick
for tree in self.trees:
#Drop sapling
drop = tree.dropChance(self.map)
if(drop != None):
tree = Tree("Sapling",drop[0],drop[1])
self.trees.append(tree)
self.calculateMap(len(self.map[0]))
self.monthlySaplings += 1
#Age up
type = tree.monthUp()
self.monthlyLog.append("["+str(self.monthlySaplings) + "] new saplings.")
self.monthlySaplings = 0
lumber = 0
#Lumberjack tick
for lumberjack in self.lumberjacks:
noOfLumber = lumberjack.move(self.map)
if(noOfLumber > 0):
lumber = lumber + noOfLumber
self.yearlyLumber += noOfLumber
location = lumberjack.getLocation()
for i, tree in enumerate(self.trees):
if(tree.getLocation() == location):
del self.trees[i]
self.calculateMap(len(self.map[0]))
#Bear tick
for bear in self.bears:
if(bear.move(self.map) == 1):
location = bear.getLocation()
for i, lumberjack in enumerate(self.lumberjacks):
if(lumberjack.getLocation() == location):
del self.lumberjacks[i]
self.calculateMap(len(self.map[0]))
# Each year #
if(self.tickCount % 12 == 0):
self.yearlyLog = []
self.yearlyLog.append("["+str(self.yearlyLumber)+"] pieces of lumber harvested by Lumberjacks.")
if(self.yearlyLumber > len(self.lumberjacks)):
amountToHire = int((self.yearlyLumber - len(self.lumberjacks)) / 10)
for i in range(0,amountToHire):
x,y = findLocation(self.map)
lumberjack = Lumberjack(x,y)
self.lumberjacks.append(lumberjack)
#self.yearlyLog.append("Added jack at "+str(x)+","+str(y))
self.calculateMap(len(self.map[0]))
self.yearlyLog.append("["+str(amountToHire)+"] new lumberjack(s)")
elif(self.yearlyLumber < len(self.lumberjacks)):
if(len(self.lumberjacks) > 1):
amountToFire = int(math.ceil((len(self.lumberjacks) - self.yearlyLumber) / 10))
for i in range(0,amountToFire):
del self.lumberjacks[random(len(self.lumberjacks))]
self.yearlyLog.append("A lumberjack has been fired")
self.yearlyLog.append("["+str(len(self.lumberjacks))+"] Lumberjacks.")
#Bear year#
mawCount = 0
for bear in self.bears:
mawCount += bear.getMaws()
bear.newYear()
self.yearlyLog.append("["+str(mawCount)+"] maws.")
if(mawCount > 0):
del self.bears[random(len(self.bears))]
self.yearlyLog.append("[1] bear has been captured and taken to the Zoo.")
if(len(self.lumberjacks) == 0):
x,y = findLocation(self.map)
lumberjack = Lumberjack(x,y)
self.lumberjacks.append(lumberjack)
elif(mawCount == 0):
x,y = findLocation(self.map)
bear = Bear(x,y)
self.bears.append(bear)
self.yearlyLog.append("[1] bear has roamed into the area.")
## Log information. ##
self.monthlyLog.append("["+str(lumber)+"] pieces of lumber harvested by Lumberjacks.")
def log(self):
mawCount = 0
print("Year "+str(int(self.tickCount / 12))+ ", Month "+ str((self.tickCount % 12)+1))
print("========Monthly Log========")
for logItem in self.monthlyLog:
print(logItem)
print("\n========Yearly log========")
for logItem in self.yearlyLog:
print(logItem)
if(self.tickCount % 12 == 0):
self.yearlyLumber = 0
class Tree:
def __init__(self,treeType,x,y):
self.type = treeType
self.x = x
self.y = y
if(treeType == "Sapling"):
self.age = 0
elif(treeType == "Tree"):
self.age = 12
elif(treeType == "Elder"):
self.age = 120
def __str__(self):
if(self.type == "Sapling"):
return "S"
elif(self.type == "Tree"):
return "T"
else:
return "E"
def getDisplay(self):
if(self.type == "Sapling"):
return "S"
elif(self.type == "Tree"):
return "T"
elif(self.type == "Elder"):
return "E"
def monthUp(self):
self.age = self.age + 1
if(self.age >= 120):
self.type = "Elder"
elif(self.age >= 12):
self.type = "Tree"
def drop(self,map,maxSize):
validDrop = []
for x in range(-1,2):
for y in range(-1,2):
checkX = self.x + x
checkY = self.y + y
if(checkX != -1 and checkX != maxSize):
if(checkY != -1 and checkY != maxSize):
if(map[checkY][checkX] == "."):
validDrop.append((checkX,checkY))
if(len(validDrop) == 0):
return None
else:
index = random(len(validDrop))
return(validDrop[index])
def getLocation(self):
return self.x,self.y
def dropChance(self,map):
maxSize = len(map[0])
randomNo = random(100)
if(self.type == "Tree"):
if(randomNo < 10):
return self.drop(map,maxSize)
if(self.type == "Elder"):
if(randomNo < 20):
return self.drop(map,maxSize)
class Lumberjack:
def __init__(self,x,y):
self.x = x
self.y = y
self.woodCount = 0
def __str__(self):
return "L"
def move(self,map):
maxSize = len(map[0])
validMove = []
for i in range(0,3):
for x in range(-1,2):
for y in range(-1,2):
checkX = self.x + x
checkY = self.y + y
if(checkX != -1 and checkX != maxSize):
if(checkY != -1 and checkY != maxSize):
if(str(map[checkY][checkX]) != "S"):
validMove.append((checkX,checkY))
index = random(len(validMove))
self.x = validMove[index][1]
self.y = validMove[index][0]
if(str(map[self.y][self.x]) == "T"):
self.woodCount = self.woodCount + 1
return 1
elif(str(map[self.y][self.x]) == "E"):
self.woodCount = self.woodCount + 2
return 2
def getLocation(self):
return self.x,self.y
class Bear:
def __init__(self,x,y):
self.x = x
self.y = y
self.mawCount = 0
def __str__(self):
return "B"
def newYear(self):
self.mawCount = 0
def getMaws(self):
return self.mawCount
def move(self,map):
maxSize = len(map[0])
validMove = []
for i in range(0,5):
for x in range(-1,2):
for y in range(-1,2):
checkX = self.x + x
checkY = self.y + y
if(checkX != -1 and checkX != maxSize):
if(checkY != -1 and checkY != maxSize):
validMove.append((checkX,checkY))
index = random(len(validMove))
self.x = validMove[index][1]
self.y = validMove[index][0]
if(str(map[self.y][self.x]) == "L"):
if(random(100) < 50):
self.mawCount += 1
return 1
else:
return 0
def getLocation(self):
return self.x,self.y
def findLocation(map):
x = random(len(map[0]))
y = random(len(map[0]))
while(str(map[y][x]) != "."):
x = random(len(map[0]))
y = random(len(map[0]))
return x,y
def random(size):
return randrange(0,size)
def tick():
print("Tick")
try:
if __name__ == "__main__":
map = Map(int(sys.argv[1]))
while(True):
map.tick()
os.system("cls")
map.displayMap()
map.calculateMap(int(sys.argv[1]))
map.log()
time.sleep(1)
except KeyboardInterrupt:
sys.exit("\nFinished simulation")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment