document.write('<script src="https://cdn.jsdelivr.net/highlight.js/9.0.0/highlight.min.js" type="text/javascript"></script>');
document.write('<link media="all" type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/highlight.js/9.0.0/styles/default.min.css">');
document.write('<link media="all" type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/highlight.js/9.0.0/styles/tomorrow.min.css">');
document.write('<style>#pastee-k60xJgHtDgTR411m-content { margin-top: 0px !important; background-color: #f7f7f9; border: 1px solid #e1e1e8; white-space: pre-wrap; } #pastee-k60xJgHtDgTR411m-content > code { margin-top: 0px !important; }</style>');
document.write('Paste provided by <a href="https://paste.ee">Paste.ee</a> - <a href="https://paste.ee/p/Xf65d">View Original</a> - <a href="https://paste.ee/r/Xf65d/0">View Raw</a> - <a href="https://paste.ee/d/Xf65d/0">Download</a><br />');
document.write('<pre id="pastee-k60xJgHtDgTR411m-content"><code>#! /usr/bin/env python\n\nimport os\nimport subprocess\nimport copy\nimport random\nimport time\n\nworld_size = 32\nworld = [[\&#039;x\&#039; for i in range(world_size)] for i in range(world_size)]\n\nbots = []\nnew_world = copy.deepcopy(world)\nbots_to_delete = []\n\nclass Bot(object):\n	def __init__(self, name, cmdline, x, y):\n		self.name = name\n		self.cmdline = cmdline\n		self.x, self.y = x, y\n	def move(self):\n		local_world = copy.deepcopy(world)\n		for bot in bots:\n			if bot!=self:\n				local_world[bot.y][bot.x] = str(bots.index(bot) + 1)\n		for bot in bots:\n			if bot==self:\n				local_world[bot.y][bot.x] = \&#039;@\&#039;\n		out = \&#039;\n\&#039;.join([\&#039;\&#039;.join(i) for i in local_world])\n		proc = subprocess.Popen(list(self.cmdline) + [out],\n					stdout=subprocess.PIPE)\n		result = proc.communicate()[0]\n		try:\n			dx, dy = [int(i) for i in result.split()]\n			if abs(dx) + abs(dy) &gt; 4:\n				dx, dy = 0, 0\n			if self.x + dx &lt; 0:\n				dx = -self.x\n			if self.y + dy &lt; 0:\n				dy = -self.y\n			if self.x + dx &gt;= world_size:\n				dx = world_size - self.x - 1\n			if self.y + dy &gt;= world_size:\n				dy = world_size - self.y - 1\n		except:\n			dx, dy = 0, 0\n		self.x += dx\n		self.y += dy\n\n		if world[self.y][self.x] == \&#039;x\&#039;:\n			new_world[self.y][self.x] = \&#039; \&#039;\n		else:\n			bots_to_delete.append(self)\n\ndef turn():\n	global world\n	global new_world\n	global bots_to_delete\n	new_world = copy.deepcopy(world)\n	for bot in bots:\n		bot.move()\n	world = new_world\n	for ex_bot in bots_to_delete:\n		del bots[bots.index(ex_bot)]\n	bots_to_delete = []\n\n\ndef match(bot_list, max_bots, debug=False):\n	global world\n	global bots\n	global bots_to_delete\n	world = [[\&#039;x\&#039; for i in range(world_size)] for i in range(world_size)]\n	bots = []\n	bots_to_delete = []\n	try:\n		competing_bots = random.sample(bot_list, max_bots)\n	except ValueError:\n		competing_bots = (bot_list*int(max_bots/len(bot_list)+ 1))[:max_bots]\n	for bot in competing_bots:\n		bots.append(Bot(bot[0], bot[1], random.randint(0, world_size-1), random.randint(0, world_size-1)))\n	current_competitors = [i.name for i in bots]\n	for i in range(32*32):\n		turn()\n		if debug:\n			local_world = copy.deepcopy(world)\n			for bot in bots:\n				local_world[bot.y][bot.x] = \&#039;@\&#039;\n			time.sleep(0.25)\n			print \&#039;\n\&#039;*20\n			print \&#039;\n\&#039;.join([\&#039; \&#039;.join(i) for i in local_world]) \n		if not bots or len(current_competitors)==1:\n			winners = current_competitors\n			return winners\n		else:\n			current_competitors = [i.name for i in bots]\n\n\nif __name__==&quot;__main__&quot;:\n	possibilities = [\n		(\&#039;Random Bot\&#039;, (\&#039;python\&#039;, \&#039;random_bot.py\&#039;)),\n		(\&#039;UpBot\&#039;, (\&#039;python\&#039;, \&#039;upbot.py\&#039;)),\n		(\&#039;Slow Bot\&#039;, (\&#039;python\&#039;, \&#039;slowbot.py\&#039;)),\n		(\&#039;LookBot\&#039;, (\&#039;./lookbot\&#039;,)),\n		(\&#039;JumpBot\&#039;, (\&#039;./jumpbot\&#039;,)),\n		(\&#039;Moat Builder\&#039;, (\&#039;python\&#039;, \&#039;moatbuilder.py\&#039;)),\n	]\n	inp = raw_input(&quot;Run tournament? y/N: &quot;)\n	if inp.lower().startswith(\&#039;y\&#039;):\n		stats = dict([(i[0], 0) for i in possibilities])\n		while True:\n			try:\n				rounds = int(input(&quot;Tournament rounds: &quot;))\n				break\n			except:\n				print &quot;Invalid value. Please enter a number.&quot;\n		for i in xrange(rounds):\n			result = match(possibilities, 4)\n			print &quot;Round %i: %s&quot; % (i+1, &quot;, &quot;.join(result))\n			for bot in result:\n				stats[bot] += 1\n		keyfunc = lambda x: x[1]\n		results = reversed(sorted(stats.items(), key=keyfunc))\n		for i in results:\n			print i[0].ljust(25), i[1]\n	else:\n		result = match(possibilities, 4, True)\n		print result</code></pre>');

function initEmbeddedPaste_k60xJgHtDgTR411m() {
	hljs.highlightBlock(document.getElementById('pastee-k60xJgHtDgTR411m-content'));
}

addEventListener('DOMContentLoaded', initEmbeddedPaste_k60xJgHtDgTR411m, false);
addEventListener('load', initEmbeddedPaste_k60xJgHtDgTR411m, false);