# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_board.ipynb.

# %% auto 0
__all__ = ['Board']

# %% ../nbs/02_board.ipynb 2
import numpy as np

# %% ../nbs/02_board.ipynb 4
class Board():

    '''A class to represent the board. The board is a hexacon with 19 tiles that are represented with cubic coordinates'''

    def __init__(self,
                 ):
        '''Initialize the board.'''
        # Create the board
        self.board = np.zeros((19, 6), dtype=int)
        # Create the board coordinates using the first three columns
        single_index = 0
        for q in range (-2, 3):
            for s in reversed(range(-2, 3)):
                r = -q - s
                # pass if maximum absolute of any variable value is greater than 2
                if max(abs(q), abs(s), abs(r)) > 2:
                    continue
                self.board[single_index, 0:3] = [q, r, s]
                single_index += 1
        self.potential_moves=np.ones(19, dtype=bool)


    def __str__(self):
        return "Tiles object"
    
    __repr__ = __str__
    
    def put_tile(self, tile, coordinates):
        '''Put a tile on the board.'''
        # Get the index of the coordinates
        index = self.get_index(coordinates)
        if self.board[index, 3] != 0:
            return("Slot already occupied")
        else:
            # Put the tile on the board
            self.board[index, 3:] = tile
            self.potential_moves[index] = False
            return("Tile placed")

    
    def get_index(self, coordinates):
        '''Get the index of the coordinates.'''
        # Get the index of the coordinates
        index = np.where((self.board[:, 0] == coordinates[0]) & (self.board[:, 1] == coordinates[1]) & (self.board[:, 2] == coordinates[2]))
        return index
    
    def get_column_score(self, column_index, potential=False):
        value_columns=0
        for i in range(-2, 3):
            column = self.board[self.board[:, column_index]==i, column_index+3]
            if potential:
                non_zero_column = column[column != 0]
                if all (x == non_zero_column[0] for x in non_zero_column):
                    value_column = np.sum(column)
                else:
                    value_column = 0
            else:
                if all (x == column[0] for x in column):
                    value_column = np.sum(column)
                else:
                    value_column = 0
            value_columns += value_column
        return value_columns
    
    def get_score(self):
        '''Get the score of the board.'''
        score = 0
        for i in range(3):
            score += self.get_column_score(i, potential=False)
        return score
    
    def get_score_potential(self):
        '''Get the score of the board.'''
        score = 0
        for i in range(3):
            score += self.get_column_score(i, potential=True)
        return score
    
    def get_potential_moves(self):
        '''Get the potential moves.'''
        indices = np.where(self.potential_moves)[0]
        potential_moves_coordinates = self.board[indices, :3].copy()
        return self.potential_moves.copy(), potential_moves_coordinates

    
    def is_full(self):
        '''Check if the board is full.'''
        if sum(self.potential_moves) == 0:
            return True
        else:
            return False

    def reset(self):
        '''Reset the chosen array to all False.'''
        self.board[:, 3:]=0
