Mrowqa

PIZZA 2013 contest, python user move script for labirynth

Aug 26th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. # script for PIZZA/labirynth contest by Mrowqa @ 2013 -- http://mrowqa.pl
  2.  
  3. import socket
  4. import time
  5.  
  6. HOST = 'pizza.natodia.net'
  7. PORT = 10000
  8. USER = 'mrowqa'
  9. PASS = 'sweet secret ;>'
  10.  
  11. def cmd(command):
  12.     f.write(command + "\n")
  13.     f.flush()
  14.  
  15. def wait():
  16.     cmd("WAIT")
  17.     read()
  18.     read()
  19.  
  20. def read():
  21.     return f.readline().strip()
  22.  
  23. def visualize(map):
  24.     map = map.split("\n")
  25.     map[-1] = "M 0 0" # ME
  26.     minX, maxX, minY, maxY = 0,0,0,0
  27.     for m in map: # calculate bounds
  28.         m = m.split(" ")
  29.         x, y = int(m[1]), int(m[2])
  30.         minX, maxX = min(minX, x), max(maxX, x)
  31.         minY, maxY = min(minY, y), max(maxY, y)
  32.     #
  33.     w, h = (maxX-minX+1), (maxY-minY+1)
  34.     xmap=[list([' ']*w) for cnt in range(h)] # map[y][x]
  35.     s2m = {'F':'.', 'S':'!', 'E':'?', 'W':'#', 'M':'+'}
  36.     for m in map:
  37.         m = m.split(" ")
  38.         x, y = int(m[1])+abs(minX), int(m[2])+abs(minY)
  39.         try:
  40.             xmap[y][x] = s2m[m[0]]
  41.         except KeyError:
  42.             xmap[y][x] = '@'
  43.     #
  44.     for i in range(len(xmap)):
  45.         xmap[i] = ''.join(xmap[i])
  46.     #
  47.     print '\n'.join(xmap)
  48.  
  49. def scan():
  50.     cmd("SCAN")
  51.     err = read()
  52.     if(err!="OK"):
  53.         return err
  54.     cnt = int(read())
  55.     map = ""
  56.     for i in range(cnt):
  57.         map += read() + "\n"
  58.     visualize(map)
  59.  
  60. def move(x,y):
  61.     cmd("MOVE " + str(x) + " " + str(y))
  62.     return read()
  63.  
  64. def d(): #down
  65.     move(0,1)
  66.     wait()
  67.     scan()
  68.  
  69. def u(): #up
  70.     move(0,-1)
  71.     wait()
  72.     scan()
  73.  
  74. def l(): #left
  75.     move(-1,0)
  76.     wait()
  77.     scan()
  78.  
  79. def r(): #right
  80.     move(1,0)
  81.     wait()
  82.     scan()
  83.  
  84. def t(foo, cnt):
  85.     for i in range(cnt):
  86.         foo()
  87.  
  88.  
  89. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  90. s.connect((HOST,PORT))
  91. f = s.makefile()
  92.  
  93. read() # read 'LOGIN'
  94. cmd(USER)
  95. read() # read 'PASS'
  96. cmd(PASS)
  97. read() # read 'OK'
  98.  
  99. # command(f, "TURNS_LEFT")
  100. # f.readline() # read 'OK'
  101. # print f.readline().strip()
Advertisement
Add Comment
Please, Sign In to add comment