import pygame import random import time cell_size = 10 WIN_WIDTH = 850 WIN_HEIGHT = 850 BACKGROUND_COLOR = "#D3D3D3" DISPLAY = (WIN_WIDTH, WIN_HEIGHT) p = 0.3 class Country(object): def __init__(self, x, y, dx, dy): self.x = x self.y = y self.dx = dx self.dy = dy self.color = (random.choice(range(256)), random.choice(range(256)), random.choice(range(256))) def get_neighbs(self, countries): self.neighbors = [] for i in range(len(countries)): c = countries[i] if cx == self.x and c.dx == self.dx and (cy + c.dy == self.y or cy == self.y + self.dy): self.neighbors.append(i) if cy == self.y and c.dy == self.dy and (cx + c.dx == self.x or cx == self.x + self.dx): self.neighbors.append(i) def __repr__(self): return str((self.x, self.y, self.dx, self.dy, self.neighbors)) __str__ = __repr__ def draw(self, screen): self.image = pygame.Rect(self.x, self.y, self.dx, self.dy) pygame.draw.rect(screen, self.color, self.image) def area(self): return self.dx * self.dy def world_init(): countries = [Country(50, 50, 250, 250), Country(300, 50, 250, 250), Country(550, 50, 250, 250), Country(50, 300, 250, 250), Country(300, 300, 250, 250), Country(550, 300, 250, 250), Country(50, 550, 250, 250), Country(300, 550, 250, 250), Country(550, 550, 250, 250)] for c in countries: c.get_neighbs(countries) return countries def merge(countries, i, j): """i < j""" if countries[i].x < countries[j].x: new_country = Country(countries[i].x, countries[i].y, countries[i].dx + countries[j].dx, countries[i].dy) if countries[i].y < countries[j].y: new_country = Country(countries[i].x, countries[i].y, countries[i].dx, countries[i].dy + countries[j].dy) if countries[i].x > countries[j].x: new_country = Country(countries[j].x, countries[j].y, countries[j].dx + countries[i].dx, countries[j].dy) if countries[i].y > countries[j].y: new_country = Country(countries[j].x, countries[j].y, countries[j].dx, countries[j].dy + countries[i].dy) del countries[i] del countries[j - 1] countries.append(new_country) for c in countries: c.get_neighbs(countries) def divide(countries, i, midx=0, midy=0): print countries[i].area() if midy == 0: new1 = Country(countries[i].x, countries[i].y, midx, countries[i].dy) new2 = Country(countries[i].x + midx, countries[i].y, countries[i].dx - midx, countries[i].dy) del countries[i] countries.append(new1) countries.append(new2) for c in countries: c.get_neighbs(countries) if midx == 0: new1 = Country(countries[i].x, countries[i].y, countries[i].dx, midy) new2 = Country(countries[i].x, countries[i].y + midy, countries[i].dx, countries[i].dy - midy) del countries[i] countries.append(new1) countries.append(new2) for c in countries: c.get_neighbs(countries) def world_draw(countries, screen): for c in countries: c.draw(screen) def random_action(countries): rand = (random.uniform(0, 1) < p) if rand: i = random.choice(range(len(countries))) if countries[i].neighbors: j = random.choice(countries[i].neighbors) merge(countries, i, j) else: i = random.choice(range(len(countries))) xaxis = random.choice([True, False]) if xaxis: try: mid = random.choice(range(cell_size, countries[i].dx, cell_size)) except IndexError: return 0 divide(countries, i, midx=mid) else: try: mid = random.choice(range(cell_size, countries[i].dy, cell_size)) except IndexError: return 0 divide(countries, i, midy=mid) def get_stat(countries): digits = [int(str(c.area())[0]) for c in countries] result = {0:0.0, 1:0.0, 2:0.0, 3:0.0, 4:0.0, 5:0.0, 6:0.0, 7:0.0, 8:0.0, 9:0.0} for d in digits: result[d] += 1 for d in range(1, 10): result[d] = round(result[d] / len(digits), 3) return result def main(): pygame.init() screen = pygame.display.set_mode(DISPLAY) pygame.display.set_caption("World History") bg = pygame.Surface((WIN_WIDTH, WIN_HEIGHT)) bg.fill(pygame.Color(BACKGROUND_COLOR)) countries = world_init() waiting = True while waiting: events = pygame.event.get() for event in events: if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: waiting = False for i in range(1000): for e in pygame.event.get(): if e.type == pygame.QUIT: raise SystemExit, "QUIT" world_draw(countries, screen) pygame.display.update() random_action(countries) time.sleep(0.07) print get_stat(countries) main()