Skip to content

Commit 04c6415

Browse files
committed
Added function test map and bot.
1 parent 64d311d commit 04c6415

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

ants/ants.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,20 @@ def __init__(self, options=None):
5050
self.viewradius = int(options["viewradius2"])
5151
self.attackradius = int(options["attackradius2"])
5252
self.spawnradius = int(options["spawnradius2"])
53+
print("Starting game with view=%s, attack=%s and spawn=%s" % (self.viewradius, self.attackradius, self.spawnradius))
5354
self.do_attack = self.do_attack_closest
5455
if 'attack' in options:
5556
if options['attack'] == 'occupied':
5657
self.do_attack = self.do_attack_occupied
5758
elif options['attack'] == 'closest':
5859
self.do_attack = self.do_attack_closest
59-
self.render = self.render_changes
6060
self.do_food = self.do_food_sections
61+
if 'food' in options:
62+
if options['food'] == 'none':
63+
self.do_food = self.do_food_none
64+
elif options['food'] == 'sections':
65+
self.do_food = self.do_food_sections
66+
self.render = self.render_changes
6167

6268
self.width = None # the map
6369
self.height = None
@@ -154,7 +160,7 @@ def distance(self, x1, y1, x2, y2):
154160
d_y = min(abs(y1 - y2), self.height - abs(y1 - y2))
155161
return d_x + d_y
156162

157-
def get_vision(self, player, radius=96):
163+
def get_vision(self, player):
158164
vision = [[False for col in range(self.width)] for row in range(self.height)]
159165
squaresToCheck = deque()
160166
for row, col in self.player_ants(player):
@@ -168,7 +174,7 @@ def get_vision(self, player, radius=96):
168174
d_row = min(d_row, self.height - d_row)
169175
d_col = abs(a_col - n_col)
170176
d_col = min(d_col, self.width - d_col)
171-
if not vision[n_row][n_col] and (d_row**2 + d_col**2) <= radius:
177+
if not vision[n_row][n_col] and (d_row**2 + d_col**2) <= self.viewradius:
172178
vision[n_row][n_col] = True
173179
if not self.revealed[player][n_row][n_col]:
174180
self.turn_reveal[player].append((n_row, n_col))
@@ -180,8 +186,8 @@ def get_vision(self, player, radius=96):
180186
squaresToCheck.append(((a_row,a_col),(n_row,n_col)))
181187
return vision
182188

183-
def get_perspective(self, player, radius=96):
184-
v = self.get_vision(player, radius)
189+
def get_perspective(self, player):
190+
v = self.get_vision(player, self.viewradius)
185191
#start_row = self.center[player][1] - self.height // 2
186192
#stop_row = start_row + self.height
187193
#start_col = self.center[player][0] - self.width // 2
@@ -350,7 +356,7 @@ def do_spawn(self):
350356
new_ants = {}
351357
for f_row, f_col in self.food_list[:]:
352358
owner = None
353-
for (n_row, n_col), n_owner in self.nearby_ants(f_row, f_col, None, 1, 9):
359+
for (n_row, n_col), n_owner in self.nearby_ants(f_row, f_col, None, 1, self.spawnradius):
354360
if owner == None:
355361
owner = n_owner
356362
elif owner != n_owner:
@@ -371,7 +377,7 @@ def do_attack_occupied(self):
371377
score = [Fraction(0, 1) for i in range(self.num_players)]
372378
for (a_row, a_col), a_owner in self.ant_list.items():
373379
killers = []
374-
enemies = self.nearby_ants(a_row, a_col, a_owner, 1, 2)
380+
enemies = self.nearby_ants(a_row, a_col, a_owner, 1, self.attackradius)
375381
occupied = len(enemies)
376382
for (e_row, e_col), e_owner in enemies:
377383
e_occupied = len(self.nearby_ants(e_row, e_col, e_owner, 1, 2))
@@ -396,7 +402,7 @@ def find_enemy(row, col, owner, min_d, max_d):
396402
if not (n_row, n_col) in ant_group:
397403
ant_group[(n_row, n_col)] = n_owner
398404
find_enemy(n_row, n_col, n_owner, min_d, max_d)
399-
for distance in range(1, 10):
405+
for distance in range(1, self.attackradius):
400406
for (a_row, a_col), a_owner in self.ant_list.items():
401407
if self.map[a_row][a_col] != LAND:
402408
ant_group = {(a_row, a_col): a_owner}
@@ -496,6 +502,9 @@ def find_closest_land(self, coord):
496502

497503
return None
498504

505+
def do_food_none(self, amount=0):
506+
pass
507+
499508
def do_food_random(self, amount=1):
500509
"""
501510
Place food randomly on the map

ants/bots/python/MyBot.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
from ants import *
3+
4+
class TestBot:
5+
def do_turn(self, ants):
6+
destinations = []
7+
for a_row, a_col in ants.my_ants():
8+
# try all directions in order
9+
directions = ('n','e','s','w')
10+
for direction in directions:
11+
(n_row, n_col) = ants.destination(a_row, a_col, direction)
12+
if (not (n_row, n_col) in destinations and
13+
ants.passable(n_row, n_col)):
14+
ants.issue_order((a_row, a_col, direction))
15+
destinations.append((n_row, n_col))
16+
break
17+
else:
18+
destinations.append((a_row, a_col))
19+
20+
if __name__ == '__main__':
21+
try:
22+
import psyco
23+
psyco.full()
24+
except ImportError:
25+
pass
26+
try:
27+
Ants.run(TestBot())
28+
except KeyboardInterrupt:
29+
print('ctrl-c, leaving ...')

ants/playgame.py

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def main(argv):
6262
parser.add_option("--attack", dest="attack",
6363
default="closest",
6464
help="Attack method to use for engine. (closest, occupied)")
65+
parser.add_option("--food", dest="food",
66+
default="sections",
67+
help="Food spawning method. (none, random, sections)")
6568
parser.add_option("--viewradius2", dest="viewradius2",
6669
default=96, type="int",
6770
help="Vision radius of ants squared")
@@ -81,6 +84,7 @@ def main(argv):
8184
game_options = {
8285
"map": opts.map,
8386
"attack": opts.attack,
87+
"food": opts.food,
8488
"viewradius2": opts.viewradius2,
8589
"attackradius2": opts.attackradius2,
8690
"spawnradius2": opts.spawnradius2,

ants/test_bot.cmd

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@echo off
2+
playgame.py --food none -o viewer -t 30 -m submission_test/test.map %1 "python submission_test/TestBot.py"
3+
4+

ants/test_bot.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
./playgame.py --food none -o viewer -t 30 -m submission_test/test.map %1 "python submission_test/TestBot.py"
2+

0 commit comments

Comments
 (0)