import random

def play(data):
  # 解析json
  board = data['board']
  height, width = board['height'], board['width']
  pos_0 = data['you']
  body = pos_0['body']
  head = pos_0['head']
  action_list = get_available_action(head, body ,height, width)
  action = random.choice(action_list)
  return action


def get_available_action(head, body ,height, width):
  print(head)
  action_name = ["up", "down", "left", "right"]
  action_num = [[0,1],[0,-1],[-1,0],[1,0]]
  x, y = head['x'], head['y']
  available_action = []
  for index in range(len(action_num)):
    action = action_num[index] 
    new_x = x + action[0]
    new_y = y + action[1]

    if new_x < 0 or new_x > width-1 or new_y < 0 or new_y > height-1:
      continue
    
    
    is_conflict = False
    for b in body:
      b_x = b['x']
      b_y = b['y']
      if new_x == b_x and new_y == b_y:
        is_conflict = True
        break
    if is_conflict:
      continue
    available_action.append(action_name[index])
  return available_action

    
import random

def play(data):
  # 解析json
  board = data['board']
  height, width = board['height'], board['width']
  pos_0 = data['you']
  body = pos_0['body']
  head = pos_0['head']
  action_list = get_available_action(head, body ,height, width)
  action = random.choice(action_list)
  return action


def get_available_action(head, body ,height, width):
  print(head)
  action_name = ["up", "down", "left", "right"]
  action_num = [[0,1],[0,-1],[-1,0],[1,0]]
  x, y = head['x'], head['y']
  available_action = []
  for index in range(len(action_num)):
    action = action_num[index] 
    new_x = x + action[0]
    new_y = y + action[1]

    if new_x < 0 or new_x > width-1 or new_y < 0 or new_y > height-1:
      continue
    
    
    is_conflict = False
    for b in body:
      b_x = b['x']
      b_y = b['y']
      if new_x == b_x and new_y == b_y:
        is_conflict = True
        break
    if is_conflict:
      continue
    available_action.append(action_name[index])
  return available_action

    