| 88 | |
| 89 | |
| 90 | class Board: |
| 91 | def __init__(self, zombies): |
| 92 | self.zombies = zombies |
| 93 | random.shuffle(self.zombies) |
| 94 | |
| 95 | numberOfZombies = len(zombies) |
| 96 | numberOfSpaces = WIDTH * HEIGHT |
| 97 | if numberOfZombies >= numberOfSpaces: |
| 98 | raise Exception('Too many zombies for this size board.') |
| 99 | |
| 100 | # Place the zombie objects on the board: |
| 101 | for zombie in zombies: |
| 102 | while True: |
| 103 | x = random.randint(0, WIDTH - 1) |
| 104 | y = random.randint(0, HEIGHT - 1) |
| 105 | zombieAtXY = self.getZombieAt(x, y) |
| 106 | |
| 107 | if zombieAtXY == None: |
| 108 | zombie._x = x |
| 109 | zombie._y = y |
| 110 | break |
| 111 | |
| 112 | |
| 113 | def getZombieAt(self, x, y): |
| 114 | for zombie in self.zombies: |
| 115 | if zombie._x == x and zombie._y == y: |
| 116 | return zombie |
| 117 | return None |
| 118 | |
| 119 | |
| 120 | def display(self): |
| 121 | for zombie in self.zombies: |
| 122 | bext.goto(zombie._x, zombie._y) |
| 123 | bext.fg(zombie.color) |
| 124 | # Display the zombie's text character: |
| 125 | if zombie.direction == NORTH: |
| 126 | print(FACE_UP, end='') |
| 127 | elif zombie.direction == SOUTH: |
| 128 | print(FACE_DOWN, end='') |
| 129 | elif zombie.direction == EAST: |
| 130 | print(FACE_RIGHT, end='') |
| 131 | elif zombie.direction == WEST: |
| 132 | print(FACE_LEFT, end='') |
| 133 | |
| 134 | # Display a count of each kind of zombie: |
| 135 | # TODO - potential bug - erase full first |
| 136 | bext.goto(0, HEIGHT) |
| 137 | count = {} |
| 138 | for zombie in self.zombies: |
| 139 | count.setdefault(zombie.__class__, 0) |
| 140 | count[zombie.__class__] += 1 |
| 141 | for zombieType in sorted(count.keys(), key=lambda x: x.__name__): |
| 142 | bext.fg(zombieType.color) |
| 143 | print(zombieType.__name__ + ': ' + str(count[zombieType]) + ' ', end='') |
| 144 | print('', flush=True) |
| 145 | |
| 146 | def runSimulation(self): |
| 147 | try: |