Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import numpy as np
- import random as rand
- radMax = 2 * np.pi
- LonMax = 360 * 60
- LatMax = 90 * 60
- class Map():
- def __init__(self, initLandChance):
- self.landChance = initLandChance
- self.world = [[0 for x in range(LonMax)] for y in range(LatMax)]
- def generate(self):
- for Lon in range(LonMax):
- for Lat in range(LatMax):
- roll = rand.randint(0, 99)
- if(roll < self.landChance):
- self.world[Lon][Lat] = Cell(self, Lon, Lat, 1)
- else:
- self.world[Lon][Lat] = Cell(self, Lon, Lat, 0)
- class Cell():
- def __init__(self, world, theta, rad, startState):
- self.world = world
- self.theta = theta
- self.rad = rad
- self.currState = startState
- self.nextState = self.currState
- def check(self):
- neighbors = 0
- if(self.world.world[self.theta+1][self.rad+1].currState == 1):
- neighbors += 1
- if(self.world.world[self.theta+1][self.rad+0].currState == 1):
- neighbors += 1
- if(self.world.world[self.theta+1][self.rad-1].currState == 1):
- neighbors += 1
- if(self.world.world[self.theta+0][self.rad-1].currState == 1):
- neighbors += 1
- if(self.world.world[self.theta+0][self.rad+1].currState == 1):
- neighbors += 1
- if(self.world.world[self.theta-1][self.rad+1].currState == 1):
- neighbors += 1
- if(self.world.world[self.theta-1][self.rad+0].currState == 1):
- neighbors += 1
- if(self.world.world[self.theta-1][self.rad-1].currState == 1):
- neighbors += 1
- if(neighbors >= 4):
- self.nextState = 1
- else:
- self.nextState = 0
- def tick(self):
- self.currState = self.nextState
- #Converts a longitude to a number of radians
- def LonToRads(Lon):
- rads = (Lon/LonMax) * radMax
- return rads
- def stitchWorlds(map1, map2):
- for lon in range(LonMax):
- if(map1[lon][LatMax] != map2[flipLon(lon)][LatMax]):
- if(rand.randint(1, 2) == 1):
- map2[flipLon(lon)][LatMax] = map1[lon][LatMax]
- else:
- map2[flipLon(lon)][LatMax] = map2[flipLon(lon)][LatMax]
- def flipLon(lon):
- lon += (LonMax//2)
- lon = lon % LonMax
- return lon
- land = int(input("How likely would you like Land to be (insert value between 1 and 98)? "))
- generations = int(input("How many generations? "))
- north = Map(land)
- north.generate()
- south = Map(land)
- south.generate()
- for i in range(generations):
- for lon in range(LonMax):
- for lat in range(LatMax):
- north.world[lon][lat].check()
- south.world[lon][lat].check()
- for lon in range(LonMax):
- for lat in range(LatMax):
- north.world[lon][lat].tick()
- south.world[lon][lat].tick()
- stitchWorlds(north, south)
- fig, (ax1, ax2) = plt.subplots(1, 2, subplot_kw=dict(projection='polar'))
- for lon in range(LonMax):
- for lat in range(LatMax):
- if(north.world[lon][lat].currState == 1):
- ax1.plot(LonToRads(lon), lat, 'go')
- elif(north.world[lon][lat].currState == 0):
- ax1.plot(LonToRads(lon), lat, 'bo')
- if(south.world[lon][lat].currState == 1):
- ax1.plot(LonToRads(lon), lat, 'go')
- elif(south.world[lon][lat].currState == 0):
- ax1.plot(LonToRads(lon), lat, 'bo')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement