-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzone.py
More file actions
72 lines (58 loc) · 1.95 KB
/
zone.py
File metadata and controls
72 lines (58 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from maptile import MapTile
class Zone(object):
paint_blocks = 0.00
player_number = 0
raised = False
def __init__(self):
self.tile_contents = []
def add(self, mp):
"""
Adds a maptile to the zone
If the maptile isnt raised it adds a number to paint_blocks for
zone control calculations
"""
self.tile_contents.append(mp)
if(self.tile_contents[-1].raised == False):
self.paint_blocks += 1.00
def get_owner(self, n):
"""
Returns the player_number of whoever has control
over the zone, requires at least 51%
Takes an int n for the number of players in the game, works with
numbers greater than but less efficient.
"""
for i in range(n):
if(get_percent(n) >= .51):
return n
return 0
def calculate_owner(self, n):
"""
Updates player_number
"""
self.player_number = self.get_owner(n)
def get_dominant_color(self):
colors = [None]
frequencies = [0]
for tile in self.tile_contents:
color = tile.paint_color
if color is not None:
if not color in colors:
colors.append(color)
frequencies.append(1)
else:
frequencies[colors.index(color)] += 1
freqs = sorted(colors, key=lambda c: (frequencies[colors.index(c)]), reverse=True)
if(len(freqs)>=2 and frequencies[colors.index(freqs[0])] == frequencies[colors.index(freqs[1])]):
return None
return freqs[0]
def get_percent(self, n):
"""
Returns percent control of player n
"""
controlled = 0.00
for i in range(len(self.tile_contents)):
if(self.tile_contents[i].player_number == n):
controlled += 1.00
return float(controlled / self.paint_blocks)
def __repr__(self):
return "[ZONE %s]"%(len(self.tile_contents))