Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. import json
  2. import random
  3. import logging
  4.  
  5.  
  6. logging.basicConfig(level=logging.DEBUG, filename='bot.log', filemode='w', format='%(message)s')
  7. config = input()
  8. config = json.loads(config)
  9. commands = ['left', 'down', 'right', 'up']
  10.  
  11.  
  12. def distance_from_head_to_trail(head, trail):
  13. hd = head["position"]
  14. coord = 999999
  15. for trl in trail["lines"]:
  16. coord = min(coord, abs(trl[0] - hd[0]) + abs(trl[1] - hd[1]))
  17. return coord
  18.  
  19.  
  20. def inborder(c):
  21. if c[0] < 0 or c[1] < 0 or c[0] > config["params"]["width"] * config["params"]["x_cells_count"] \
  22. or c[1] > config["params"]["width"] * config["params"]["y_cells_count"]:
  23. return 1
  24. return 0
  25.  
  26.  
  27. strt = [0, 0]
  28.  
  29.  
  30. while True:
  31. state = input()
  32. state = json.loads(state)
  33. players = state["params"]["players"]
  34. for player in players:
  35. if player == 'i':
  36. my_info = players[player]
  37. if strt == [0, 0]:
  38. strt = my_info['position']
  39. ter = my_info['territory']
  40. for line in my_info['lines']:
  41. ter.append(line)
  42. sz = 1
  43. cnt = {}
  44. for et in ter:
  45. ind = max(abs(et[0] - strt[0]), abs(et[1] - strt[1]))
  46. if ind not in cnt.keys():
  47. cnt[ind] = 1
  48. else:
  49. cnt[ind] += 1
  50.  
  51. n = 1
  52. i = 0
  53. nn = len(cnt)
  54. mnn = 1 + len(cnt) * 2
  55. for sss in sorted(cnt.keys()):
  56. need = max(1, n * 4 - 4)
  57. if need > cnt[sss]:
  58. mnn = min(mnn, n)
  59. nn = min(nn, i)
  60. n += 2
  61. i += 1
  62. needto = []
  63. while not needto:
  64. for mult in range(-nn, nn + 1):
  65. elem = [strt[0] + mult * 30, strt[1] - nn * 30]
  66. if inborder(elem):
  67. continue
  68. if elem not in ter:
  69. needto = elem
  70. for mult in range(-nn, nn + 1):
  71. elem = [strt[0] - nn * 30, strt[1] + mult * 30]
  72. if inborder(elem):
  73. continue
  74. if elem not in ter:
  75. needto = elem
  76. for mult in range(-nn, nn + 1):
  77. elem = [strt[0] + nn * 30, strt[1] + mult * 30]
  78. if inborder(elem):
  79. continue
  80. if elem not in ter:
  81. needto = elem
  82. for mult in range(-nn, nn + 1):
  83. elem = [strt[0] + mult * 30, strt[1] + nn * 30]
  84. if inborder(elem):
  85. continue
  86. if elem not in ter:
  87. needto = elem
  88. nn += 1
  89.  
  90. dst = 999999
  91. for player in players:
  92. if player != 'i':
  93. dst = min(dst, distance_from_head_to_trail(players[player], my_info))
  94.  
  95. if not needto or dst / 30 <= 3:
  96. needto = random.choice(my_info["territory"])
  97. while needto[0] == my_info["position"][0] or needto[1] == my_info["position"][1]:
  98. needto = random.choice(my_info["territory"])
  99.  
  100. pos = my_info["position"]
  101. direct = my_info["direction"]
  102. if pos[0] > needto[0] and direct != commands[2]:
  103. cmd = commands[0]
  104. elif pos[0] < needto[0] and direct != commands[0]:
  105. cmd = commands[2]
  106. elif pos[1] > needto[1] and direct != commands[3]:
  107. cmd = commands[1]
  108. elif pos[1] < needto[1] and direct != commands[1]:
  109. cmd = commands[3]
  110. print(json.dumps({"command": cmd, 'debug': cmd}))
  111.  
  112. # python localrunner.py -p1 "python main.py" -p2 simple_bot -p3 simple_bot -p4 simple_bot -p5 simple_bot -p6 simple_bot
  113. # python localrunner.py -p1 "python main.py"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement