Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import random
- import logging
- logging.basicConfig(level=logging.DEBUG, filename='bot.log', filemode='w', format='%(message)s')
- config = input()
- config = json.loads(config)
- commands = ['left', 'down', 'right', 'up']
- def distance_from_head_to_trail(head, trail):
- hd = head["position"]
- coord = 999999
- for trl in trail["lines"]:
- coord = min(coord, abs(trl[0] - hd[0]) + abs(trl[1] - hd[1]))
- return coord
- def inborder(c):
- if c[0] < 0 or c[1] < 0 or c[0] > config["params"]["width"] * config["params"]["x_cells_count"] \
- or c[1] > config["params"]["width"] * config["params"]["y_cells_count"]:
- return 1
- return 0
- strt = [0, 0]
- while True:
- state = input()
- state = json.loads(state)
- players = state["params"]["players"]
- for player in players:
- if player == 'i':
- my_info = players[player]
- if strt == [0, 0]:
- strt = my_info['position']
- ter = my_info['territory']
- for line in my_info['lines']:
- ter.append(line)
- sz = 1
- cnt = {}
- for et in ter:
- ind = max(abs(et[0] - strt[0]), abs(et[1] - strt[1]))
- if ind not in cnt.keys():
- cnt[ind] = 1
- else:
- cnt[ind] += 1
- n = 1
- i = 0
- nn = len(cnt)
- mnn = 1 + len(cnt) * 2
- for sss in sorted(cnt.keys()):
- need = max(1, n * 4 - 4)
- if need > cnt[sss]:
- mnn = min(mnn, n)
- nn = min(nn, i)
- n += 2
- i += 1
- needto = []
- while not needto:
- for mult in range(-nn, nn + 1):
- elem = [strt[0] + mult * 30, strt[1] - nn * 30]
- if inborder(elem):
- continue
- if elem not in ter:
- needto = elem
- for mult in range(-nn, nn + 1):
- elem = [strt[0] - nn * 30, strt[1] + mult * 30]
- if inborder(elem):
- continue
- if elem not in ter:
- needto = elem
- for mult in range(-nn, nn + 1):
- elem = [strt[0] + nn * 30, strt[1] + mult * 30]
- if inborder(elem):
- continue
- if elem not in ter:
- needto = elem
- for mult in range(-nn, nn + 1):
- elem = [strt[0] + mult * 30, strt[1] + nn * 30]
- if inborder(elem):
- continue
- if elem not in ter:
- needto = elem
- nn += 1
- dst = 999999
- for player in players:
- if player != 'i':
- dst = min(dst, distance_from_head_to_trail(players[player], my_info))
- if not needto or dst / 30 <= 3:
- needto = random.choice(my_info["territory"])
- while needto[0] == my_info["position"][0] or needto[1] == my_info["position"][1]:
- needto = random.choice(my_info["territory"])
- pos = my_info["position"]
- direct = my_info["direction"]
- if pos[0] > needto[0] and direct != commands[2]:
- cmd = commands[0]
- elif pos[0] < needto[0] and direct != commands[0]:
- cmd = commands[2]
- elif pos[1] > needto[1] and direct != commands[3]:
- cmd = commands[1]
- elif pos[1] < needto[1] and direct != commands[1]:
- cmd = commands[3]
- print(json.dumps({"command": cmd, 'debug': cmd}))
- # python localrunner.py -p1 "python main.py" -p2 simple_bot -p3 simple_bot -p4 simple_bot -p5 simple_bot -p6 simple_bot
- # python localrunner.py -p1 "python main.py"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement