This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.
New variable " balls" vs prior ball. ball(2) would find the x position of the second ball. balls(5) would find y of second ball.
Test | Status | Code Input and Output |
---|---|---|
1 | Fail |
%%
feval(@assignin,'caller','score',1000);
pwidth=50; % Total size +/- 50 for 101 Paddle
bwidth=10; % Radius of ball
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;
mov_step=25; % Paddle Quantized Movement (1/4 Paddle)
maxLives=4;
maxHits=600; % Return Mission Complete
qballs=3; % quantity of balls 1 to 5
% Initial Start
paddle=500; % position y % min max paddle [50 950]
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
[curdir]=PONG_003_solver(paddle,balls); % FUNCTION CALL
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
% Paddle Move
paddle=max(pwidth,min(1000-pwidth,paddle+curmov)); % [50 : 950] limits
% 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
% [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
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 )) );
Error: Undefined function 'ball' for input arguments of type 'double'.
|
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!