% Represents a Hex Board. Contains a matrix of Hexes, a size 
%  (assumed to be a square board), and the current 
%  mouse-selectedHexLocation.
classdef Board < handle
  properties
     % a matrix of Hexes
     hexes
     % The size of a board; dimensions are size x size
     size
     % Currently mouse-selected hex location
     selectedHexLocation
  end
  methods
    % Board(Number) -> Board
    %
    % Creates a board of unplayed hexes; size controls total size of board
    %  when displayed.
    %
    % calls: Hex()
    function board = Board(size)      
    end
    
    % drawBoundaries(Board, Player, Player) -> graphics
    % 
    % Draws the boundary lines that show which sides must be connected
    % for each player to win
    function [] = drawBoundaries(board, player1, player2)
    end
        
    % Draws board on figure by drawing each hex. Sets 'ButtonDownFcn' on
    %  each hex handle to @setSelectedHex. 
    %
    % contains: setSelectedHex(source, eventdata, handles)
    % calls: hex.draw
    function [] = draw(board)
      function [] = setSelectedHex(source, eventdata, handles)
      end
    end
    
    % play(Board, Player, [Number Number]) -> boolean
    %
    % Plays the given move on the board for the given player.
    % Sets the player on the hex and checks if the board now
    %  has a winning path.
    %
    % calls: hex.setPlayer, board.getWinningPath
    function isWon = play(board, player, move)
    end
    
    % showPath(Board, [Numbers]) -> graphics
    %
    % For each number in the path, find the hex at that position
    %  and make it visually distinctive.
    function [] = showPath(board, path)
    end

    % getConnectedPaths(Board, Number) -> Matrix<number>
    %
    % Gets a matrix of all connected paths. Each path is labelled by
    %  a non-zero identifier. 
    %
    % This implementation has been provided and we will discuss it in
    %  class.    
    function paths = getConnectedPaths(board, playerNumber)
        hexArray = board.hexes;
        if (isa(board.hexes,'cell'))
            hexArray = [board.hexes{:}];
        end
        mask = reshape([hexArray.player],board.size,board.size) == playerNumber;
        %disp(mask);
        x = [0 1 1; 1 0 0; 1 0 1];
        n = repmat(x,board.size,board.size);
        %disp(n);
        n(1:3:end,1:3:end) = mask;
        bwn = bwlabel(n,4);
        %determine which direction the player has to connect
        paths = bwn(1:3:end,1:3:end); 
    end
    
    % getWinningPath(Board, [Number Number]) -> vector<Number>
    %
    % Gets the winning path from this board if one exists. The winning path
    %  is a vector of index positions that make up the path. If no winning
    %  path exists, returns an empty vector.
    %
    % calls: board.getConnectedPaths, intersect (MATLAB built-in),
    %        find (MATLAB built-in)
    function result = getWinningPath(board, move)
    end
    
    % isEmpty(Board, [Number Number]) -> boolean
    %
    % Returns true if the given location on the board is empty, false
    %  if it is already occcupied.
    %
    % calls: hex.isEmpty
    function result = isEmpty(board, location)
    end
  end
end