"""
Random agent — reference implementation.

This agent plays randomly: it places the given piece on a random empty cell
and gives a random remaining piece to the opponent.

Use this as a baseline benchmark.  Every submitted agent should aim to beat it.
"""

import random


def play_turn(board, piece_to_place, pieces_left):
    """
    board         : list[int | None], length 16 (row-major 4x4 grid).
    piece_to_place: int 0-15, the piece you MUST place this turn.
    pieces_left   : list[int], remaining pieces not on the board.
                    Choose one to hand to your opponent, or return None
                    if this list is empty (final move).

    Returns: (cell, next_piece)
        cell       : int 0-15, index of an empty board cell.
        next_piece : int from pieces_left, or None if pieces_left is empty.
    """
    # Find all empty cells
    empty = [i for i, v in enumerate(board) if v is None]

    # Pick a random empty cell to place the piece
    cell = random.choice(empty)

    # Pick a random piece to give to the opponent
    next_piece = random.choice(pieces_left) if pieces_left else None

    return cell, next_piece
