Guest User

Firefighters.py

a guest
Oct 8th, 2015
171
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. import random
  4. import copy
  5. import sys
  6. import subprocess
  7. import time
  8.  
  9. board = []
  10. for i in range(32):
  11.     board.append(["."]*32)
  12.  
  13. ground = '.'
  14. bot = '@'
  15. ash = '#'
  16. wet = 'W'
  17. doublewet = 'WW'
  18. fire = ['1','2','3','4','5','6']
  19.  
  20. def chebyshev_distance(x1, y1, x2, y2):
  21.     return max(abs(x2-x1), abs(y2-y1))
  22.  
  23. def botify(board):
  24.     newboard = copy.deepcopy(board)
  25.     for bot in bots:
  26.         newboard[bot.y][bot.x] = '@'
  27.         # newboard[bot.y][bot.x] = bot.name[0]
  28.     return newboard
  29.  
  30. def vbotify(board):
  31.     newboard = copy.deepcopy(board)
  32.     for bot in bots:
  33.         # newboard[bot.y][bot.x] = '@'
  34.         newboard[bot.y][bot.x] = bot.name[0]
  35.     return newboard
  36.  
  37. def try_burn(cell):
  38.     if cell == ground:
  39.         return fire[0]
  40.     else:
  41.         return cell
  42.  
  43. def turn(board, bots):
  44.     newboard = []
  45.     # Bot updates
  46.     for bot in bots:
  47.         try:
  48.             result = bot.turn(bot.x, bot.y, board)
  49.             # print result
  50.             bot.x = bot.x + {'L':-1, 'U':0, 'D':0, 'S':0, 'R':1}[result[0]]
  51.             bot.y = bot.y + {'L':0, 'U':1, 'D':-1, 'S':0, 'R':0}[result[0]]
  52.             bot.action = result[1:3]
  53.         except:
  54.             # invalid move, bot does nothing
  55.             pass
  56.         bot.x = min(max(bot.x,0), 31)
  57.         bot.y = min(max(bot.y,0), 31)
  58.     for bot in bots:
  59.         try:
  60.             x = bot.x + {'L':-1, 'U':0, 'D':0, 'S':0, 'R':1}[bot.action[1]]
  61.             y = bot.y + {'L':0, 'U':1, 'D':-1, 'S':0, 'R':0}[bot.action[1]]
  62.             if bot.action[0] == 'B':
  63.                 if board[y][x]==ground:
  64.                     board[y][x] = fire[0]
  65.                 elif board[y][x]==wet:
  66.                     board[y][x] = ground
  67.             elif bot.action[0] == 'P':
  68.                 if board[y][x]==ground or board[y][x]==wet:
  69.                     board[y][x] = doublewet
  70.                 elif board[y][x] in fire:
  71.                     board[y][x] = ground
  72.         except:
  73.             # invalid action, bot does nothing
  74.             pass
  75.     # Environmental updates
  76.     for i in range(32):
  77.         newboard.append(['.']*32)
  78.     for x in range(32):
  79.         for y in range(32):
  80.             if board[x][y] == ground and newboard[x][y] == ground:
  81.                 newboard[x][y] = ground
  82.             elif board[x][y] == ash and newboard[x][y] == ground:
  83.                 newboard[x][y] = ash
  84.             elif board[x][y] == wet and newboard[x][y] == ground:
  85.                 newboard[x][y] = ground
  86.             elif board[x][y] == doublewet:
  87.                 newboard[x][y] = wet
  88.             elif board[x][y] in fire:
  89.                 if board[x][y]>'3':
  90.                     if x>0:
  91.                         newboard[x-1][y] = try_burn(board[x-1][y])
  92.                     if x<31:
  93.                         newboard[x+1][y] = try_burn(board[x+1][y])
  94.                     if y>0:
  95.                         newboard[x][y-1] = try_burn(board[x][y-1])
  96.                     if y<31:
  97.                         newboard[x][y+1] = try_burn(board[x][y+1])
  98.                 if board[x][y]>'5':
  99.                     newboard[x][y] = ash
  100.                 else:
  101.                     newboard[x][y] = fire[fire.index(board[x][y])+1]
  102.     print
  103.     for i in vbotify(newboard):
  104.         for j in i:
  105.             print j,
  106.         print
  107.     return newboard
  108.  
  109.  
  110. def do_match(bots):
  111.     lx = 0
  112.     ly = 0
  113.     for bot in bots:
  114.         bot.x, bot.y = random.randint(-4,3)+(lx*16)+8, random.randint(-4,3)+(ly*16)+8
  115.         bot.territory = {'x':bot.x, 'y':bot.y}
  116.         lx = (lx+ly)%2
  117.         ly = (ly+1)%2
  118.         print bot.x, bot.y
  119.  
  120.     for i in range(100):
  121.         global board
  122.         board = turn(board, bots)
  123.         end = False
  124.         for bot in bots:
  125.             bot.area = 0
  126.             for x in range(bot.territory['x']-4, bot.territory['x']+5):
  127.                 for y in range(bot.territory['y']-4, bot.territory['y']+5):
  128.                     if board[y][x] in (ground, wet):
  129.                         bot.area+=1
  130.             print (bot.name+" area: "+str(bot.area))
  131.             if bot.area==0:
  132.                 end = True
  133.         if end:
  134.             break
  135.         time.sleep(0.2)
  136.     print ("\n\n"+'-'*15)
  137.     print ("Final results:")
  138.     for bot in bots:
  139.         print (bot.name+" area: "+str(bot.area))
  140.  
  141. class Bot(object):
  142.     def __init__(self, name):
  143.         self.x = 0
  144.         self.y = 0
  145.         self.territory = {}
  146.         self.area = 0
  147.         self.name = name
  148.  
  149.     def turn(self, x, y, board):
  150.         instr = str(self.territory['x'])+'\n'+str(self.territory['y'])+'\n'
  151.         instr = instr+str(x)+'\n'+str(y)+'\n'+'\n'.join([''.join(i) for i in botify(board)])
  152.         bot = subprocess.Popen(['bots/'+self.name], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  153.         out = bot.communicate(input=instr)[0]
  154.         return out
  155.  
  156.  
  157. if __name__=="__main__":
  158.     bots = []
  159.     botnames = sys.argv[1:]
  160.     random.shuffle(botnames)
  161.     for botname in botnames:
  162.         # print botname
  163.         bots.append(Bot(botname))
  164.     do_match(bots)
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×