function connectDots(positions)
% An interactive game to play connect the dots.
% positions is an n-by-2 matrix. positions(:, 1) are the x coordinates of
% the dots in order of connection, and positions(:, 2) are the y
% coordinates of the dots

% set up graphics
close all
board = imread('connect_dots.jpg');

% start game
continuing = 1;                     % indicates when loop should continue
numDots = 0;                        % number of dots user has selected
totalDots = size(positions, 1);     % total dots in game
figLims = plotBoard(board);         % limits of figure window

% store x and y positions of clicks in these vectors
x = [];
y = [];

% each loop does a click
while continuing	
	% notify user in title of current number of dots
	% ADD CODE HERE
	
	% select new dot and find coordinates
	% ADD CODE HERE
	
	% check if dot is within game board
	if % ADD CODE HERE
		
		% get number of first selected dot
		% ADD CODE HERE
		
		
		if % ADD CODE HERE
            % display message for dot number which user clicked
			% ADD CODE HERE
			pause(0.5)
        else
            % display message for missed click
			% ADD CODE HERE
			pause(1)
		end
		
		% check if correct dot is clicked
		if % ADD CODE HERE
			% draw line to selected dot and update number of dots
			% ADD CODE HERE
		else
			% incorrect dot, tell user which dot number to click
			% ADD CODE HERE
            pause(1)
        end
		
        % check for win game condition
		if % ADD CODE HERE
			continuing = 0;
		end
	else
		% pressed outside the boundaries --> game over
		continuing = 0;
	end
end

if % ADD CODE HERE
	% display message for player quitting game
	title('You quit the game!');
else
	% redraw board with solutions, display message for winning game
	plotBoard(board);
	plot(positions(:, 1), positions(:, 2), 'k', 'LineWidth', 2);
	title('You won the game! Congrats, you are awesome!');
end

hold off


function res = isWithinLims(x, y, figLims)
% checks if the coordinate is within the figure limits
% x, y, coordinate of mouse button click
% figLims: limits of an immages
% res equals 1 if click is in limits, 2 if click is out of limits
% figLims(1) is left bound
% figLims(2) is right bound
% figLims(3) is lower bound
% figLims(4) is upper bound

% ADD CODE HERE


function i = getSelectedNodeIndex(x, y, positions)
% given coordinates (x,y) determine the index  i for which the point with
% coordinates (positions(i,1), positions(i,2))  is closest to (x,y) AND
% within distance at most 10 units.
% returns 0 if no such point exists

% ADD CODE HERE


function figLims = plotBoard(board)
% given an image matrix 'board', set up graphics and return dimensions
clf

[n, m] = size(board);
imshow(board);
axis off

figLims = [1 m 1 n];
hold on