Created
September 21, 2010 21:44
-
-
Save hamstah/590640 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"name": "StupidBot", | |
"author": null, | |
"language": "python", | |
"location": ["stupidbot.py"] | |
}, | |
{ | |
"name": "StillStupidBot", | |
"author": null, | |
"language": "python", | |
"location": ["stillstupidbot.py"] | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"name": "BullyBot", | |
"author": null, | |
"language": "java", | |
"location": ["example_bots", "BullyBot.jar"] | |
}, | |
{ | |
"name": "DualBot", | |
"author": null, | |
"language": "java", | |
"location": ["example_bots", "DualBot.jar"] | |
}, | |
{ | |
"name": "ProspectorBot", | |
"author": null, | |
"language": "java", | |
"location": ["example_bots", "ProspectorBot.jar"] | |
}, | |
{ | |
"name": "RageBot", | |
"author": null, | |
"language": "java", | |
"location": ["example_bots", "RageBot.jar"] | |
}, | |
{ | |
"name": "RandomBot", | |
"author": null, | |
"language": "java", | |
"location": ["example_bots", "RandomBot.jar"] | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/usr/bin/env python -v | |
import subprocess | |
import errno | |
import glob | |
import sys | |
import os | |
maps_path='./maps/' | |
tools_path='./tools/' | |
try: | |
import json | |
except ImportError: | |
import simplejson as json | |
def get_bots(cwd, bots_file='bots.json'): | |
while True: | |
try: | |
fp = open(os.path.join(cwd, bots_file)) | |
except IOError, e: | |
if e.errno == errno.EAGAIN: | |
continue | |
print >> sys.stderr, e | |
return None | |
break | |
try: | |
bots = json.load(fp) | |
except ValueError, e: | |
print >> sys.stderr, 'Error Loading %s' % bots_file | |
print >> sys.stderr, e | |
return None | |
return bots | |
def get_maps(cwd, maps_dir=maps_path, file_glob='map*.txt'): | |
maps_glob = '%s%s%s' % (os.path.join(cwd, maps_dir), os.sep, file_glob) | |
return glob.glob(maps_glob) | |
def get_command(cwd, bot_info): | |
if bot_info['language'].lower() == 'python': | |
return 'python %s' % os.path.join(cwd, *bot_info['location']) | |
elif bot_info['language'].lower() == 'java': | |
return 'java -jar %s' % os.path.join(cwd, *bot_info['location']) | |
elif bot_info['language'].lower() == 'php': | |
# Thanks to Naktibalda for PHP | |
return 'php %s' % os.path.join(cwd, *bot_info['location']) | |
def run_tests(cwd, timeout, turns, logfile, quiet=True): | |
bots = get_bots(cwd) | |
if bots is None: | |
return None | |
opponents = bots + get_bots(cwd, bots_file='examples.json') | |
maps = get_maps(cwd) | |
maps_count = len(maps) | |
for bot in bots: | |
bot['command'] = get_command(cwd, bot) | |
for opponent in opponents: | |
opponent['command'] = get_command(cwd, opponent) | |
cmd = [ | |
'java', '-jar', os.path.join(cwd, tools_path, 'PlayGame.jar'), | |
'maps/map1.txt', | |
timeout, turns, logfile, | |
'BOT 1', | |
'BOT 2', | |
] | |
results = {} | |
for i, bot in enumerate(bots): | |
cmd[7] = bot['command'] | |
for opponent in opponents: | |
if not results.has_key((bot['name'], opponent['name'])) and \ | |
not results.has_key((opponent['name'], bot['name'])) and \ | |
bot['name'] != opponent['name']: | |
result = {} | |
if not quiet: | |
print bot['name'], 'vs', opponent['name'] | |
for i, map in enumerate(maps): | |
if not quiet: | |
print ' ', '(%s/%s)' % (i+1, maps_count), map[len(cwd) + 1:] | |
cmd[3] = map | |
cmd[8] = opponent['command'] | |
p = subprocess.Popen([str(x) for x in cmd], | |
stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
stdout, stderr = p.communicate() | |
if stderr.find('Player 1 Wins!') != -1: | |
_winner = bot['name'] | |
elif stderr.find('Player 2 Wins!') != -1: | |
_winner = opponent['name'] | |
else: | |
_winner = None | |
_turns = -1 | |
for line in stderr.split('\n')[::-1]: | |
line = line.split(' ') | |
if line[0] == 'Turn': | |
_turns = int(line[1]) | |
break | |
result[map[len(cwd) + 1:]] = { | |
'replay': stdout, | |
'winner': _winner, | |
'turns': _turns, | |
} | |
# showing results after each map | |
print ' ', _winner,' @ ',_turns | |
results[(bot['name'], opponent['name'])] = result | |
#results[(opponent['name'], bot['name'])] = result | |
return results | |
def mean(nums): | |
if len(nums): | |
return float( sum(nums) / len(nums)) | |
return 0.0 | |
if __name__ == "__main__": | |
cwd = os.path.dirname(os.path.abspath(__file__)) | |
results = run_tests(cwd, 1000, 200, '/dev/null', quiet=False) | |
print 'Test Results' | |
print '---------------------------------------------' | |
for key, value in results.iteritems(): | |
# added draw count | |
won, total, draw, turns = 0, 0, 0,[] | |
for map, results in value.iteritems(): | |
if results['winner'] == key[0]: | |
won = won +1 | |
elif results['winner'] == None: | |
draw = draw+1 | |
total = total + 1 | |
turns.append(results['turns']) | |
print key[0], 'vs', key[1], '\n\tW:\t%3d (%5.2d%%)\n\tD:\t%3d (%5.1d%%)\n\tT:\t%3d\n\t' % (won,(100.0*won/total), draw,(100.0*draw/total),total), 'Avg Turns:', mean(turns) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment