"""Starter bot for StrategyBot Treadline. Upload this file as it is, then make it better.

Your file needs one function, act(state), called once per move of the game (120 in all).
Only the player whose turn it is moves; on the other player's moves you get your_turn =
False and anything you send is ignored, so just return "WAIT".
On your move return "MOVE x1 y1 x2 y2": an edge from your node (x1, y1) to (x2, y2).
Anything you print() shows up in your bot's log, not in the game.

state (points are [x, y], x = column, y = row, [0, 0] top-left; you are 0 Blue or 1 Red):
  turn, turns        moves played so far, moves in the game (120)
  round, rounds      the rules' "turn" (61): Blue 1 move, then 2 each, last one 1 move
  your_turn          True when you move now; moves_left = moves left in this round
  width, height      grid points across and down; reach = 3 (max |dx| and |dy|)
  self, opponent     id, color, score, area (enclosed now), nodes [[x, y], ...]
  edges              every edge: id, a, b, owner, turn (round placed), protected
  regions            every enclosed region: owner, area, outline, edges (ids on its outline)
  legal_moves        [x1, y1, x2, y2, breaks, closes] for each legal move:
                     breaks = the enemy edge id it would break (-1 = none),
                     closes = 1 if it ends on your node or crosses your edge (may enclose)
  legal_actions      the same moves as "MOVE x1 y1 x2 y2" strings (["WAIT"] if not your turn)

Points: at the end of every round both players add their enclosed area to their score.

This bot breaks an enemy edge that holds up one of their regions if it can, otherwise
closes something, otherwise draws any legal edge.
"""
import random


def act(state):
    if not state["your_turn"]:
        return "WAIT"
    moves = state["legal_moves"]
    me = state["self"]["id"]

    # enemy edges on the outline of an enemy region: breaking one costs them that income
    walls = {e for r in state["regions"] if r["owner"] != me for e in r["edges"]}
    for x1, y1, x2, y2, breaks, closes in moves:
        if breaks in walls:
            return f"MOVE {x1} {y1} {x2} {y2}"

    # a move that comes back to your own lines might enclose something: try one
    closing = [m for m in moves if m[5]]
    pick = random.choice(closing or moves)
    return "MOVE %d %d %d %d" % tuple(pick[:4])
