def volcanoAction(self, row, col): with self.lock: for i in range(len(self.volcanoes[(row,col)])): if self.volcanoes[(row,col)][i] == 1: #kill cell on row,col, show smoke on adj(row,col), show lava on row,col if self.cells[row][col].team not in neutral_teams: self.cells[row][col] = neutral() self.land[row][col].decimate(.35) for r,c in self.adj(row,col): self.warning_cells[(r,c)] = '#E59400' self.lava_cells.add((row,col)) self.volcanoes[(row,col)][i] += 1 elif self.volcanoes[(row,col)][i] == 2: #kill cells on adj(row,col) for r,c in self.adj(row,col): self.lava_cells.add((r,c)) if self.cells[r][c].team not in neutral_teams: self.cells[r][c] = neutral() self.land[r][c].decimate(.35) self.volcanoes[(row,col)][i] += 1 for i in range(len(self.volcanoes[(row,col)])): if self.volcanoes[(row,col)] == 3: self.volcanoes[(row,col)].remove(3) break if not self.volcanoes[(row,col)]: del self.volcanoes[(row,col)]
def move(self, displacement, team): with self.lock: ret = copy.copy(self.selected[team]) if self.selected[team]: r,c = iter(self.selected[team]).next() if self.cells[r][c].team == tornado_str and not self.testing: return "Cannot move tornado." if self.cells[r][c].team != team and not self.testing: cost = len(self.selected[team]) * 2 else: cost = len(self.selected[team]) * 1 if cost > self.points[team] and not self.testing: # TODO response return "Move requires " + str(cost) + " points." for row, col in self.selected[team]: brow = row + displacement[0] bcol = col + displacement[1] if not (self.cells[row][col].team != neutral_str and self.inGrid(brow, bcol) and (self.cells[brow][bcol] == neutral() or (brow,bcol) in self.selected[team])): break else: #move is valid self.points[team] -= cost ordered = sorted(self.selected[team], key = orderings[displacement]) for row, col in ordered: brow = row + displacement[0] bcol = col + displacement[1] self.cells[brow][bcol] = self.cells[row][col] self.cells[row][col] = neutral() self.selected[team].remove((row,col)) self.selected[team].add((brow,bcol)) ret.add((brow,bcol)) return ret
def __init__(self, height = 8, width = 8, team1 = "Red", team2 = "Blue"): self.cells = [[neutral() for col in range(width)] for row in range(height)] self.rand = random.Random() self.width = width self.height = height self.teams = [team1,team2] self.volcanoes = dict() self.warning_cells = dict() self.lava_cells = set() self.lock = threading.RLock() self.reset()
def reset(self, include_tornado = True, testing = False): with self.lock: self.testing = testing self.turn = 0 self.land = [[baseLand() for col in range(self.width)] for row in range(self.height)] self.selected = dict() for team in self.teams: self.selected[team] = set() self.points = collections.Counter() self.points[self.teams[0]] = 0 self.points[self.teams[1]] = 0 for row in range(self.height): for col in range((self.width + 1) / 2): result = self.rand.random() if result < .75 and col < self.width * 2 / 5: # < 1000 10000 # .25 .265 .2927 # .35 .385 # .50 .469 # .55 .487 .4802 # .60 .518 .4972 # .65 .522 .5022 # .66 .4956 # .67 .509 .5052 # .68 .4987 # .70 .522 .5001 # .75 .488 .5182 # .76 .5012 # .77 .4969 # .80 .5126 # .85 .4758 self.cells[row][col] = Cell(self.teams[0], 3, initDna()) self.cells[row][self.width - col - 1] = Cell(self.teams[1], 3, initDna()) else: self.cells[row][col] = neutral() self.cells[row][self.width - col - 1] = neutral() if include_tornado: self.cells[self.width / 2][self.height / 2] = tornado() self.warning_cells.clear() self.lava_cells.clear() self.volcanoes.clear()
def kill(self, row, col, team): # returns true if any cells were killed ret = set() with self.lock: if (row, col) in self.selected[team]: if self.cells[row][col].team == team or self.testing: for r,c in self.selected[team]: for r1, c1 in self.friendlyAdj(r,c): ret.add((r1,c1)) self.cells[r][c] = neutral() ret.add((r,c)) else: if not self.selected[team]: # nothing selected if team == self.cells[row][col].team or self.testing: for r,c in self.friendlyAdj(row,col): ret.add((r,c)) self.cells[row][col] = neutral() ret.add((row,col)) ret = ret.union(self.clearSelection(team)) return ret
def internal(self): step = [[neutral() for col in range(self.width)] for row in range(self.height)] with self.lock: for row in range(self.height): for col in range(self.width): counts = collections.Counter() strengths = collections.Counter() adjacents = self.adj(row, col) team = self.cells[row][col].team for (r,c) in adjacents: counts[self.cells[r][c].team] += 1 strengths[self.cells[r][c].team] += self.cells[r][c].strength if team not in neutral_teams: friendly = counts[team] if self.cells[row][col].isWarrior2(): friendly += self.cells[row][col].strength sum_enemy = 0 for str_team, strength in strengths.items(): if str_team != team: sum_enemy += strength is_dead = True if self.land[row][col].canSupport(friendly): if sum_enemy < strengths[team]: step[row][col] = self.cells[row][col] is_dead = False else: for r,c in self.friendlyAdj(row,col): if self.cells[r][c].isMedic(): for i in range(self.cells[r][c].medicLevel()): if self.rand.random() < .25: step[row][col] = self.cells[row][col] is_dead = False break else: # Medic did not save continue # Medic saved break else: # killed in combat for str_team, strength in strengths.items(): if str_team != team and str_team not in neutral_teams: self.points[str_team] += 1 if is_dead: for k_team in self.selected.iterkeys(): if (row,col) in self.selected[k_team]: self.selected[k_team].remove((row,col)) for r,c in adjacents: if self.cells[r][c].isHunter(): for i in range(self.cells[r][c].hunterLevel()): self.land[row][col].regen() self.land[row][col].regen() self.land[row][col].regen() self.land[row][col].regen() elif team == neutral_str: threes = [] for c_team, count in counts.items(): if count == 3 and c_team not in neutral_teams: threes.append(c_team) if len(threes) == 1: strength = strengths[threes[0]] + self.rand.randint(1,12) parents = [self.cells[loc[0]][loc[1]] for loc in adjacents if self.cells[loc[0]][loc[1]].team == threes[0]] step[row][col] = offspring(parents[0],parents[1],parents[2]) self.points[threes[0]] += 1 for steam in self.selected: if (row,col) in self.selected[steam]: self.selected[steam].remove((row,col)) elif len(threes) == 2: if strengths[threes[0]] > strengths[threes[1]]: winner = threes[0] elif strengths[threes[0]] < strengths[threes[1]]: winner = threes[1] else: winner = threes[self.rand.randint(0,1)] parents = [self.cells[loc[0]][loc[1]] for loc in adjacents if self.cells[loc[0]][loc[1]].team == winner] step[row][col] = offspring(parents[0],parents[1],parents[2]) for steam in self.selected: if (row,col) in self.selected[steam]: self.selected[steam].remove((row,col)) self.points[winner] += 1 for (new_r, new_c), (old_r, old_c) in zip(self.tornadoes, self.old_tornadoes): step[new_r][new_c] = tornado() for team in self.teams: if (old_r,old_c) in self.selected[team]: self.selected[team].remove((old_r,old_c)) self.selected[team].add((new_r,new_c)) self.cells = step