Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import random
- import copy
- import sys
- import subprocess
- import time
- board = []
- for i in range(32):
- board.append(["."]*32)
- ground = '.'
- bot = '@'
- ash = '#'
- wet = 'W'
- doublewet = 'WW'
- fire = ['1','2','3','4','5','6']
- def chebyshev_distance(x1, y1, x2, y2):
- return max(abs(x2-x1), abs(y2-y1))
- def botify(board):
- newboard = copy.deepcopy(board)
- for bot in bots:
- newboard[bot.y][bot.x] = '@'
- # newboard[bot.y][bot.x] = bot.name[0]
- return newboard
- def vbotify(board):
- newboard = copy.deepcopy(board)
- for bot in bots:
- # newboard[bot.y][bot.x] = '@'
- newboard[bot.y][bot.x] = bot.name[0]
- return newboard
- def try_burn(cell):
- if cell == ground:
- return fire[0]
- else:
- return cell
- def turn(board, bots):
- newboard = []
- # Bot updates
- for bot in bots:
- try:
- result = bot.turn(bot.x, bot.y, board)
- # print result
- bot.x = bot.x + {'L':-1, 'U':0, 'D':0, 'S':0, 'R':1}[result[0]]
- bot.y = bot.y + {'L':0, 'U':1, 'D':-1, 'S':0, 'R':0}[result[0]]
- bot.action = result[1:3]
- except:
- # invalid move, bot does nothing
- pass
- bot.x = min(max(bot.x,0), 31)
- bot.y = min(max(bot.y,0), 31)
- for bot in bots:
- try:
- x = bot.x + {'L':-1, 'U':0, 'D':0, 'S':0, 'R':1}[bot.action[1]]
- y = bot.y + {'L':0, 'U':1, 'D':-1, 'S':0, 'R':0}[bot.action[1]]
- if bot.action[0] == 'B':
- if board[y][x]==ground:
- board[y][x] = fire[0]
- elif board[y][x]==wet:
- board[y][x] = ground
- elif bot.action[0] == 'P':
- if board[y][x]==ground or board[y][x]==wet:
- board[y][x] = doublewet
- elif board[y][x] in fire:
- board[y][x] = ground
- except:
- # invalid action, bot does nothing
- pass
- # Environmental updates
- for i in range(32):
- newboard.append(['.']*32)
- for x in range(32):
- for y in range(32):
- if board[x][y] == ground and newboard[x][y] == ground:
- newboard[x][y] = ground
- elif board[x][y] == ash and newboard[x][y] == ground:
- newboard[x][y] = ash
- elif board[x][y] == wet and newboard[x][y] == ground:
- newboard[x][y] = ground
- elif board[x][y] == doublewet:
- newboard[x][y] = wet
- elif board[x][y] in fire:
- if board[x][y]>'3':
- if x>0:
- newboard[x-1][y] = try_burn(board[x-1][y])
- if x<31:
- newboard[x+1][y] = try_burn(board[x+1][y])
- if y>0:
- newboard[x][y-1] = try_burn(board[x][y-1])
- if y<31:
- newboard[x][y+1] = try_burn(board[x][y+1])
- if board[x][y]>'5':
- newboard[x][y] = ash
- else:
- newboard[x][y] = fire[fire.index(board[x][y])+1]
- print
- for i in vbotify(newboard):
- for j in i:
- print j,
- print
- return newboard
- def do_match(bots):
- lx = 0
- ly = 0
- for bot in bots:
- bot.x, bot.y = random.randint(-4,3)+(lx*16)+8, random.randint(-4,3)+(ly*16)+8
- bot.territory = {'x':bot.x, 'y':bot.y}
- lx = (lx+ly)%2
- ly = (ly+1)%2
- print bot.x, bot.y
- for i in range(100):
- global board
- board = turn(board, bots)
- end = False
- for bot in bots:
- bot.area = 0
- for x in range(bot.territory['x']-4, bot.territory['x']+5):
- for y in range(bot.territory['y']-4, bot.territory['y']+5):
- if board[y][x] in (ground, wet):
- bot.area+=1
- print (bot.name+" area: "+str(bot.area))
- if bot.area==0:
- end = True
- if end:
- break
- time.sleep(0.2)
- print ("\n\n"+'-'*15)
- print ("Final results:")
- for bot in bots:
- print (bot.name+" area: "+str(bot.area))
- class Bot(object):
- def __init__(self, name):
- self.x = 0
- self.y = 0
- self.territory = {}
- self.area = 0
- self.name = name
- def turn(self, x, y, board):
- instr = str(self.territory['x'])+'\n'+str(self.territory['y'])+'\n'
- instr = instr+str(x)+'\n'+str(y)+'\n'+'\n'.join([''.join(i) for i in botify(board)])
- bot = subprocess.Popen(['bots/'+self.name], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
- out = bot.communicate(input=instr)[0]
- return out
- if __name__=="__main__":
- bots = []
- botnames = sys.argv[1:]
- random.shuffle(botnames)
- for botname in botnames:
- # print botname
- bots.append(Bot(botname))
- do_match(bots)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement