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

Solution 202487

Submitted on 10 Feb 2013 by Richard Zapor

Correct

185Size
`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',500);
pwidth=50; % Total size +/- 50 for 101 Paddle
vup=10; % Sub-sampling ball movements for Interactive
spfx=1.10; % Speed increase factor
spfy=1.05; % to Avoid fixed Paddle solution
negVmax=-200;
posVmax=210;
maxLives=4;
maxHits=100;
% Initial Start
ball=[500 500 40 60]; % x y vx vy  Treated as a Point
lives=0; % Lives
hits=0;
entry=0;
while lives<maxLives && hits<maxHits
if abs(curdir)>1,curdir=0;end % Max 1 / -1 allowed
curmov=mov_step*curdir;
if entry==0
curdirvec=curdir;
entry=1;
else
curdirvec=[curdirvec curdir]; % Saving moves for file create
end
% Ball Move : Hopefully I got the Mirror solutions right
for j=1:vup
% 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
lives=lives+1;
fprintf('Oops %i\n',lives);
if lives>=maxLives,break;end
% draw ball
% Reset Ball Keep deterministic but different
ball=[500-100*lives 500 40+11*lives 35-3*lives];
break;
end
% Ball returned
hits=hits+1;
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)));
else % Wall bounces
ball(1:2)=ball(1:2)+ball(3:4)/vup;
if ball(1)>=1000 % To the right
ball(1)=1000-(ball(1)-1000);
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
end % Ball Pass / New Position
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-5*hits+100*lives); %
fprintf('Score %i\n',score)
% Passing Score is 10 hits to Score 450 or Less
assert(score<=450,sprintf('Score %i\n',score))
feval( @assignin,'caller','score',floor(min( 500,score )) );
```
```Oops 1
Oops 2
Oops 3
Oops 4
Hits 63
Lives 4
Score 185
```