% script dart Game
% implements an interactive dart game between two players
% - calls function makeScoreBoard2 to construct a score matrix S
% - calls function makeBoardDesign2 to construct a design matrix D
% used to draw the board by means of function drawBoard.
% Players take turns in throwing darts until one of them accumulates
% 300 points.
% a player is allowed to throw up to 3 darts per turn
% each players turn is executed through function playTurn

scoreGoal = 300;
dartsPerTurn = 3;
S = makeScoreBoard2();
D = makeBoardDesign2();

% initializes scores for both players
scoreP1 = 0;
scoreP2 = 0;

p =1; % player 1 throws first
while (scoreP1 < scoreGoal && scoreP2 < scoreGoal)
% draw a clearead board
drawBoard(D);
title(sprintf('Player %d play!\n P1 has %d poins P2 has %d points ',...
p, scoreP1, scoreP2), 'FontSize', 16);
if (p==1)
scoreP2= scoreP1 + playTurn(p, dartsPerTurn, S);
p= 2; % hand to player
elseif (p==2)
scoreP1= scoreP1 + playTurn(p, dartsPerTurn, S);
p= 1;
else
error('something is wrong as p is not 1 or 2')
end

pause(2);

end

if(scoreP1 >= scoreGoal)
winner =1;
elseif (scoreP2 >= scoreGoal)
winner =2;
end

title(sprintf('Player %d has wan!', winner), 'FontSize', 18);

hold off;