|
| 1 | +import random |
| 2 | +import math |
| 3 | +import copy |
| 4 | + |
| 5 | +STILL = 0 |
| 6 | +NORTH = 1 |
| 7 | +EAST = 2 |
| 8 | +SOUTH = 3 |
| 9 | +WEST = 4 |
| 10 | + |
| 11 | +DIRECTIONS = [a for a in range(0, 5)] |
| 12 | +CARDINALS = [a for a in range(1, 5)] |
| 13 | + |
| 14 | +ATTACK = 0 |
| 15 | +STOP_ATTACK = 1 |
| 16 | + |
| 17 | +class Location: |
| 18 | + def __init__(self, x=0, y=0): |
| 19 | + self.x = x |
| 20 | + self.y = y |
| 21 | +class Site: |
| 22 | + def __init__(self, owner=0, strength=0, production=0): |
| 23 | + self.owner = owner |
| 24 | + self.strength = strength |
| 25 | + self.production = production |
| 26 | +class Move: |
| 27 | + def __init__(self, loc=0, direction=0): |
| 28 | + self.loc = loc |
| 29 | + self.direction = direction |
| 30 | + |
| 31 | +class GameMap: |
| 32 | + def __init__(self, width = 0, height = 0, numberOfPlayers = 0): |
| 33 | + self.width = width |
| 34 | + self.height = height |
| 35 | + self.contents = [] |
| 36 | + |
| 37 | + for y in range(0, self.height): |
| 38 | + row = [] |
| 39 | + for x in range(0, self.width): |
| 40 | + row.append(Site(0, 0, 0)) |
| 41 | + self.contents.append(row) |
| 42 | + |
| 43 | + def inBounds(self, l): |
| 44 | + return l.x >= 0 and l.x < self.width and l.y >= 0 and l.y < self.height |
| 45 | + |
| 46 | + def getDistance(self, l1, l2): |
| 47 | + dx = math.abs(l1.x - l2.x) |
| 48 | + dy = math.abs(l1.y - l2.y) |
| 49 | + if dx > self.width / 2: |
| 50 | + dx = self.width - dx |
| 51 | + if dy > self.height / 2: |
| 52 | + dy = self.height - dy |
| 53 | + return dx + dy |
| 54 | + |
| 55 | + def getAngle(self, l1, l2): |
| 56 | + dx = l2.x - l1.x |
| 57 | + dy = l2.y - l1.y |
| 58 | + |
| 59 | + if dx > self.width - dx: |
| 60 | + dx -= self.width |
| 61 | + elif -dx > self.width + dx: |
| 62 | + dx += self.width |
| 63 | + |
| 64 | + if dy > self.height - dy: |
| 65 | + dy -= self.height |
| 66 | + elif -dy > self.height + dy: |
| 67 | + dy += self.height |
| 68 | + return math.atan2(dy, dx) |
| 69 | + |
| 70 | + def getLocation(self, loc, direction): |
| 71 | + l = copy.deepcopy(loc) |
| 72 | + if direction != STILL: |
| 73 | + if direction == NORTH: |
| 74 | + if l.y == 0: |
| 75 | + l.y = self.height - 1 |
| 76 | + else: |
| 77 | + l.y -= 1 |
| 78 | + elif direction == EAST: |
| 79 | + if l.x == self.width - 1: |
| 80 | + l.x = 0 |
| 81 | + else: |
| 82 | + l.x += 1 |
| 83 | + elif direction == SOUTH: |
| 84 | + if l.y == self.height - 1: |
| 85 | + l.y = 0 |
| 86 | + else: |
| 87 | + l.y += 1 |
| 88 | + elif direction == WEST: |
| 89 | + if l.x == 0: |
| 90 | + l.x = self.width - 1 |
| 91 | + else: |
| 92 | + l.x -= 1 |
| 93 | + return l |
| 94 | + def getSite(self, l, direction = STILL): |
| 95 | + l = self.getLocation(l, direction) |
| 96 | + return self.contents[l.y][l.x] |
0 commit comments