function  score = playTurn(p, n, S) 
% Inputs
% p - 1 or 2  according to what player is going to throw dats 
% n - maximum number of throws the player is allowed 
% S - the scoreboard matrix 
%
% calls function throwDart  n-times  to let player p 
% throw n darts. Places the darts on the board and 
% looks up the score from scoreboard matrix S 
% returns the total score. 

i=1;
[numRows, numCols] = size(S); 
score = 0; 

dartIsIn = 1; 

while (i <=n && dartIsIn) 
    [x,y] = throwDart();
    dartIsIn = (x>=0) && (y>=0) &&  (x <= numCols) && (y < numRows);

    if (dartIsIn) 
        if (p==1) 
            plot(x,y, 'k*', 'MarkerSize', 10)
        elseif(p==2) 
            plot(x,y, 'ko', 'MarkerSize', 10)
        end
        
        thisDartScore = S(ceil(y), ceil(x)) ;
        score = score + thisDartScore; 
        i=i+1;     
    end    
end

