%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% made up game! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [] = gogame(nmax)

% close currently open windows
close all;

% test for grid size (passed parameter nmax)
if (nmax < 3)
    disp('You need a larger game grid than that!');
    return;
elseif mod(nmax,2) < 0
    disp('Please pick an even game grid size for the game to be fair!');
    return;
end

% create a new figure window
figure;
% create a set of invisible axes that will the window
fh = axes('Position',[0 0 1 1],'Visible','off');
% scale the axes for a nmax by nmax grid
axis([0 nmax 0 nmax]);
% hold on!
hold on;
% draw starting grey (no player squares) background
patch([0 0 nmax nmax],[0 nmax nmax 0],[0.5 0.5 0.5]);
% draw the game grid
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (1) CREATE FUNCTION TO DRAW THE GAME GRID LINES %%%%%%%%%%%%%%%%%%%%%%%%%
%     The code for this goes in a function, NOT here in the gogame m-file.
%     Your function is called on the next line.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
redraw_grid(nmax);

% set default player colors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (2) ADD CODE TO ASK FOR PLAYER COLORS TO BE ASSIGNED, HERE %%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% create game grid
tokens = zeros(nmax);
% start with player 1
player1 = 1;
% start game
game_on = true;
while game_on
    % get mouse click
    [x,y,button] = ginput(1);
    % determine clicked cell
    xi = round(x+0.5);
    yi = round(y+0.5);
    % determine whether a valid move
    if (tokens(xi,yi) == 0)
        % test for which player turn
        if player1
            % mark with player color
            patch([xi-1,xi-1,xi,xi],[yi-1,yi,yi,yi-1],player1_col);
            % refresh grid outline
            redraw_grid(nmax);
            % set player token
            tokens(xi,yi) = 1;
            % identify 2 or more adjacent cells of player 2
            neighs = find_neighs(tokens,xi,yi,2);
            n_neighs = length(neighs);
            if n_neighs > 1
                % turn colors and set tokens
                [row,col] = ind2sub([nmax nmax],neighs);
                for n=1:n_neighs
                    patch([row(n)-1,row(n)-1,row(n),row(n)],[col(n)-1,col(n),col(n),col(n)-1],player1_col);
                    tokens(row(n),col(n)) = 1;
                end
                % refresh grid outline
                redraw_grid(nmax);
            end
        else
            % mark with player color
            patch([xi-1,xi-1,xi,xi],[yi-1,yi,yi,yi-1],player2_col);
            % refresh grid outline
            redraw_grid(nmax);
            % set player token
            tokens(xi,yi) = 2;
            % identify 2 or more adjacent cells of player 1
            neighs = find_neighs(tokens,xi,yi,1);
            n_neighs = length(neighs);
            % turn colors
            if n_neighs > 1
                % turn colors and set tokens
                [row,col] = ind2sub([nmax nmax],neighs);
                for n=1:n_neighs
                    patch([row(n)-1,row(n)-1,row(n),row(n)],[col(n)-1,col(n),col(n),col(n)-1],player2_col);
                    tokens(row(n),col(n)) = 2;
                end
                % refresh grid outline
                redraw_grid(nmax);
            end
        end
        % test for no free cells -- game ends!
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % (3) WHO HAS WON??? ADD CODE HERE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % change player turn
        player1 = ~player1;
    else
        disp('Illegal move! Chose again.');
    end
end
