MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Solution 205205

Submitted on 17 Feb 2013 by Richard Zapor

Correct

623Size
`This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.`

### Test Suite

Test
Code Input and Output
1
Pass

```%%
feval(@assignin,'caller','score',1000);
pwidth=50; % Total size +/- 50 for 101 Paddle
vup=10; % Sub-sampling ball movements for Interactive
spfx=1.08; % Speed increase factor
spfy=1.04; % to Avoid fixed Paddle solution
negVmax=-200;
posVmax=210;
maxLives=4;
maxHits=600; % Return Mission Complete
qballs=3; % quantity of balls 1 to 5
% Initial Start
balls=[500 500 32 20;500 550 30 18;500 450 28 22;450 550 33 20;450 450 29 21]; % x y vx vy  Treated as a Point
balls=balls(1:qballs,:);
lives=0; % Lives
hits=0;
entry=0;
active=ones(1,size(balls,1));
while lives<maxLives && hits<maxHits+100*lives % Allow 0 Score
if abs(curdir)>1,curdir=0;end % Max 1 / -1  of scalar allowed
curmov=mov_step*curdir;
if entry==0 % Initialize movement history vector
curdirvec=curdir;
entry=1;
else
curdirvec=[curdirvec curdir]; % Saving moves for file create
end
% Ball Move
for j=1:vup
for nballs=1:size(balls,1)
if active(nballs)==0,continue;end
ball=balls(nballs,:);
% ball=[500 500 1 1]; % x y vx vy  Treated as a Point
if ball(1)+ball(3)/vup<=0 % Check if Point is Over
% Find x=0 crossing and check if paddle is within
% set speed scalar
xc=ball(2)-ball(1)*ball(4)/ball(3);
if xc>=1000
xc=1000-(xc-1000);
else
xc=abs(xc);
end
active(nballs)=0;
balls(nballs,:)=-50; % Place off screen/ Id as Passed
if sum(active)==0,lives=lives+1;end % All 3 Balls Lost
fprintf('Oops Life %i  Ball %i\n',lives,nballs);
if lives>=maxLives,break;end
if sum(active)==0
%balls=[500 500 32 20;500 550 30 18;500 450 28 22]; % x y vx vy
balls=[500-100*lives 500 32+12*lives 20-3*lives; ...
500-100*lives 550 30+11*lives 18-3*lives; ...
500-100*lives 450 28+10*lives 22-3*lives; ...
450-100*lives 550 33+11*lives 17-3*lives; ...
450-100*lives 450 29+10*lives 23-3*lives]; % x y vx vy
balls=balls(1:qballs,:);
active=ones(1,size(balls,1));
break;
end
continue; % Ball Not returned, next ball
end
% Ball returned
hits=hits+sum(active)^2; % Multi-Ball Bonus
ball(1:2)=ball(1:2)+ball(3:4)/vup;
ball(1)=-ball(1);
ball(3)=-spfx*ball(3);
if ball(2)<0
ball(2)=-ball(2);
ball(4)=-spfy*ball(4);
elseif ball(2)>1000
ball(2)=2000-ball(2);
ball(4)=-spfy*ball(4);
else
ball(4)=spfy*ball(4);
end
ball(3)=max(negVmax,min(posVmax,ball(3)));
ball(4)=max(negVmax,min(posVmax,ball(4)));
balls(nballs,:)=ball;
else % Wall bounces
ball(1:2)=ball(1:2)+ball(3:4)/vup;
if ball(1)>=2000 % To the right
ball(1)=2000-(ball(1)-2000);
ball(3)=-ball(3);
if ball(2)>=1000 % TR
ball(2)=1000-(ball(2)-1000);
ball(4)=-ball(4);
elseif ball(2)<=0 % BR
ball(2)=-ball(2); % abs
ball(4)=-ball(4);
end
else % Middle
if ball(2)>=1000 % TM
ball(2)=1000-(ball(2)-1000);
ball(4)=-ball(4);
elseif ball(2)<=0 % BM
ball(2)=-ball(2); % abs
ball(4)=-ball(4);
end
end
balls(nballs,:)=ball;
end % Ball Pass / New Position
end % nballs
end % j vup
end % while Alive and Hits < Total Success
%fprintf('%i ',curdirvec);fprintf('\n'); % Moves
fprintf('Hits %i\n',hits)
fprintf('Lives %i\n',lives)
score= max(0,maxHits-hits+100*lives); %
fprintf('Score %i\n',score)
% Passing Score is 75 hit points to Score 925 or Less
assert(score<=925,sprintf('Score %i\n',score))
feval( @assignin,'caller','score',floor(min( 1000,score )) );
```
```Oops Life 0  Ball 3
Oops Life 0  Ball 1
Oops Life 1  Ball 2
Oops Life 1  Ball 3
Oops Life 1  Ball 1
Oops Life 2  Ball 2
Oops Life 2  Ball 3
Oops Life 2  Ball 1
Oops Life 3  Ball 2
Oops Life 3  Ball 3
Oops Life 3  Ball 1
Oops Life 4  Ball 2
Hits 377
Lives 4
Score 623
```