Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Correct

440Size
Leading solution size is 155.
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
 bwidth=10; % Radius of ball
 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;
 mov_step=50; % Paddle Quantized Movement  (1/2 Paddle)
 maxLives=4;
 maxHits=100;
% Initial Start
 paddle=500; % position y % min max paddle [50 950]
 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
 [curdir]=PONG_001_solver(paddle,ball); % FUNCTION CALL
 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
% Paddle Move
 paddle=max(pwidth,min(1000-pwidth,paddle+curmov)); % [50 : 950] limits
% 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
    % [paddle-pwidth-bwidth,paddle+pwidth+bwidth] pwidth=50;
    % set speed scalar
      xc=ball(2)-ball(1)*ball(4)/ball(3);
      if xc>=1000
       xc=1000-(xc-1000);
      else
       xc=abs(xc);
      end
      paddlemax= paddle+pwidth+bwidth;
      paddlemin= paddle-pwidth-bwidth;
      if xc>paddlemax || xc<paddlemin % Swing and a Miss
       lives=lives+1;
       fprintf('Oops %i\n',lives);
       if lives>=maxLives,break;end
       % draw ball
       %paddle=500; % position y % min max paddle [50 950]
       % 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 12
Lives 4
Score 440