# -*- Mode: Python -*- # vi:si:et:sw=4:sts=4:ts=4 import random COLORS = 6 WIDTH = 28 HEIGHT = 14 class Grid: def __init__(self, width, height, colors): self.grid = [] self._colors = colors total = width * height quantity = (total + colors - 1) / colors self.supply = Supply(colors, quantity) for i in range(0, height): self.grid.append([]) for j in range(0, width): allowed = self.get_allowed(i, j) color = self.supply.paint(allowed) self.grid[i].append(color) def get_allowed(self, x, y): """ Get a list of colors allowed to use for the position to avoid same colors neighbouring. """ used = [] if x > 0: used.append(self.grid[x - 1][y]) #if x > 1: # used.append(self.grid[x - 2][y]) if y > 0: used.append(self.grid[x][y - 1]) #if y > 1: # used.append(self.grid[x][y - 2]) #if x > 0 and y > 0: # used.append(self.grid[x - 1][y - 1]) ret = range(0, self._colors) for u in used: if u in ret: ret.remove(u) return ret class Supply: def __init__(self, colors, quantity): """ colors: number of colors used quantity: quantity of each color """ self._colors = colors self._quantity = quantity self._left = [] for i in range(0, colors): self._left.append(quantity) def paint(self, palette): """ palette: list of colors we would like to paint with. Picks a random color from the given palette, weighted by how much is left of that color. Returns the color picked and decrements the supply. """ choices = [] for i in palette: choices.extend([i] * self._left[i]) color = random.choice(choices) self._left[color] -= 1 return color def left(self): return self._left grid = Grid(WIDTH, HEIGHT, COLORS) #print grid.grid # #print grid # #print grid.supply.left() # #for i in range(0, HEIGHT): # for j in range(0, WIDTH): # print grid.grid[i][j], # print colors = [ '#00CC66', '#006666', '#00FF33', '#00FF99', '#000033', '#CCFFCC', ] colors = [ '#307D7E', '#3EA99F', # sea green '#41A317', # lime green '#BCE954', # dark olive green '#99C68E', # dark sea green 3 '#C3FDB8', # dark sea green 1 ] print "" for i in range(0, HEIGHT): print "" for j in range(0, WIDTH): color = grid.grid[i][j] print "" % (colors[color], color) print print "" print "
%d
"