forked from sharkbait24/CS541_Warlight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
randombot.py
174 lines (156 loc) · 7.48 KB
/
randombot.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# ---------------------------------------------------------------------#
# Warlight AI Challenge - Starter Bot #
# ============ #
# #
# Last update: 20 Mar, 2014 #
# #
# @author Jackie <jackie@starapple.nl> #
# @version 1.0 #
# @license MIT License (http://opensource.org/licenses/MIT) #
# ---------------------------------------------------------------------#
# Modified by Joe Coleman
#
# Original code has been modified for our CS 541 project to
# work with our modified map and split from the bot class to
# provide a convenient way for us to create new AIs
from bot import Bot, PlaceArmyBuilder, AttackTransferBuilder
from const import PLACE_ARMIES, ATTACK_TRANSFER, NO_MOVES
from math import fmod, pi
from time import clock
# RandomBot as the name implies does everything by choosing randomly
class RandomBot(Bot):
def __init__(self, map_weights, heuristic):
super(RandomBot, self).__init__(map_weights, heuristic)
# Choose a random 6 regions from the ones supplied
# options[0] is time limit
def pick_starting_regions(self, options):
options = options[1:]
shuffled_regions = MyRandom.shuffle(options)
return ' '.join(shuffled_regions[:6])
# Places up to 2 armies on random regions
def place_armies(self, time_limit):
placements = PlaceArmyBuilder(self.name)
region_index = 0
troops_remaining = self.available_armies
owned_regions = self.map.get_owned_regions(self.name) # returns a copy of references to owned regions
shuffled_regions = MyRandom.shuffle(owned_regions)
while troops_remaining:
region = shuffled_regions[region_index]
if troops_remaining > 1:
placements.add(region.id, 2)
region.troop_count += 2
troops_remaining -= 2
else:
placements.add(region.id, 1)
region.troop_count += 1
troops_remaining -= 1
region_index += 1
return placements.to_string()
# Currently checks whether a region has more than six troops placed to attack,
# or transfers if more than 1 unit is available.
def attack_transfer(self, time_limit):
attack_transfers = AttackTransferBuilder(self.name)
owned_regions = self.map.get_owned_regions(self.name)
for region in owned_regions:
neighbors = [region for region in region.neighbors] # make a copy of references to neighbor regions
while len(neighbors) > 1:
target_region = neighbors[MyRandom.randrange(0, len(neighbors))]
if region.owner != target_region.owner and region.troop_count > 6:
attack_transfers.add(region.id, target_region.id, 5)
region.troop_count -= 5
elif region.owner == target_region.owner and region.troop_count > 1:
attack_transfers.add(region.id, target_region.id, region.troop_count - 1)
region.troop_count = 1
else:
neighbors.remove(target_region)
return attack_transfers.to_string()
class MyRandom(object):
@staticmethod
def randrange(r_min, r_max):
# A pseudo random number generator to replace random.randrange
#
# Works with an inclusive left bound and exclusive right bound.
# E.g. Random.randrange(0, 5) in [0, 1, 2, 3, 4] is always true
return r_min + int(fmod(pow(clock() + pi, 2), 1.0) * (r_max - r_min))
@staticmethod
def shuffle(items):
# Method to shuffle a list of items
i = len(items)
while i > 1:
i -= 1
j = MyRandom.randrange(0, i)
items[j], items[i] = items[i], items[j]
return items
# AttacBot decides to attack enemy positions over spreading army out to nutrual
# territory
class AttacBot(Bot):
def __init__(self, map_weights, heuristic):
super(AttacBot, self).__init__(map_weights, heuristic)
# Choose a random 6 regions from the ones supplied
# options[0] is time limit
''' THIS SHOULD BE MODIFIED TO PICK OUT OF THE BOTTLE NECKED REGIONS
WE WILL USE options PROVIDED BY SERVER AND EVALUATE WHICH
PriorityWeights ARE = 0 MEANING THE LOCATION IS A BOTTLENECK(DESIRABLE)
LOCATION FOR PLACING TROOPS '''
def pick_starting_regions(self, options):
options = options[1:]
shuffled_regions = MyRandom.shuffle(options)
return ' '.join(shuffled_regions[:6])
# Places up to 2 armies on random regions
''' REPLACE SHUFFLED_REGIONS WITH TUPLE FOR split_last_update WHICH SPLITS
THE LIST OF REGIONS INTO player_owned , neighbors , outliers '''
def place_armies(self, time_limit):
placements = PlaceArmyBuilder(self.name)
region_index = 0
troops_remaining = self.available_armies
owned_regions = self.map.get_owned_regions(self.name) # returns a copy of references to owned regions
shuffled_regions = MyRandom.shuffle(owned_regions)
while troops_remaining:
region = shuffled_regions[region_index]
if troops_remaining > 1:
placements.add(region.id, 2)
region.troop_count += 2
troops_remaining -= 2
else:
placements.add(region.id, 1)
region.troop_count += 1
troops_remaining -= 1
region_index += 1
return placements.to_string()
# Currently checks whether a region has more than six troops placed to attack,
# or transfers if more than 1 unit is available.
def attack_transfer(self, time_limit):
attack_transfers = AttackTransferBuilder(self.name)
owned_regions = self.map.get_owned_regions(self.name)
for region in owned_regions:
neighbors = [region for region in region.neighbors] # make a copy of references to neighbor regions
while len(neighbors) > 1:
target_region = neighbors[MyRandom.randrange(0, len(neighbors))]
if region.owner != target_region.owner and region.troop_count > 6:
attack_transfers.add(region.id, target_region.id, 5)
region.troop_count -= 5
elif region.owner == target_region.owner and region.troop_count > 1:
attack_transfers.add(region.id, target_region.id, region.troop_count - 1)
region.troop_count = 1
else:
neighbors.remove(target_region)
return attack_transfers.to_string()
'''
class MyRandom(object):
@staticmethod
def randrange(r_min, r_max):
# A pseudo random number generator to replace random.randrange
#
# Works with an inclusive left bound and exclusive right bound.
# E.g. Random.randrange(0, 5) in [0, 1, 2, 3, 4] is always true
return r_min + int(fmod(pow(clock() + pi, 2), 1.0) * (r_max - r_min))
@staticmethod
def shuffle(items):
# Method to shuffle a list of items
i = len(items)
while i > 1:
i -= 1
j = MyRandom.randrange(0, i)
items[j], items[i] = items[i], items[j]
return items
'''