<jupyter_start><jupyter_text>Board Representation and Visualisation using ArraysThis notebook defines a board representation based on [this guide](https://www.redblobgames.com/grids/hexagons/) and provides code for visualising the layout.The board representation is defined using Numpy arrays in a way that should be able to passed as input to a CNN.  Install DependenciesThe following dependencies are required to run this notebook (in addition to PyTorch).<jupyter_code>%%bash

# pip install numpy
# pip install matplotlib
# pip install hexagdly<jupyter_output><empty_output><jupyter_text>Imports<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon

import torch
import hexagdly<jupyter_output><empty_output><jupyter_text>Define and Visualise the Board RepresentationThe board is represented as a $4 \times (2N - 1) \times (2N - 1)$ array, where each channel stores the locations of pieces of a specific colour and each index $(c, q, r)$ represents a cell with [axial coordinates](https://www.redblobgames.com/grids/hexagons/coordinates-axial) $(q, r)$.<jupyter_code>def generate_board(n):
    """Generate a 2D array that stores the position and state
    of each cell in axial coordinates.
    """
    board = np.zeros((4, 2*n - 1, 2*n - 1))

    return board

def axial_to_pixel(q, r):
    """Convert axial coordinates to pixel (i.e. cartesian
    coordinates).
    """
    x = np.sqrt(3) * q  +  np.sqrt(3) / 2 * r
    y = 3 / 2 * r
    return x, y

def draw_board(board):
    """Visualise the board using matplotlib.
    """
    n = (board.shape[-1] + 1) / 2

    fig, ax = plt.subplots(1)
    ax.set_aspect('equal')

    for q in range(0, board.shape[-1]):
        for r in range(0, board.shape[-1]):
            if (q + r >= n - 1) and (4*n - 4 - q - r >= n - 1):
                x, y = axial_to_pixel(q, r)
                hexagon = RegularPolygon((x, y), numVertices=6, radius=1.75*np.sqrt(1/3), alpha=0.2, edgecolor='k', facecolor='C0')
                ax.add_patch(hexagon)

    #         # Uncomment to draw illegal cells (in a different colour)    
    #         else:
    #             x, y = axial_to_pixel(q, r)
    #             hexagon = RegularPolygon((x, y), numVertices=6, radius=1.75*np.sqrt(1/3), alpha=0.2, edgecolor='k', facecolor='C1')
    #             ax.add_patch(hexagon)

    plt.autoscale(enable = True)
    plt.show()
    
board = generate_board(n=3)
draw_board(board)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11686746987951807, 'fraction_numerical': 0.01887550200803213, 'mean_word_length': 3.1795302013422817, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '16276', 'license': 'MIT', 'n_tokens_mistral': 853, 'n_tokens_neox': 812, 'n_words': 253, 'path': 'notebooks/board_representation.ipynb', 'repo_name': 'adamtupper/alphablooms', 'size': '2490'}