Cody

Problem 1257. PONG 001: Player vs Wall, 4 Lives, Interactive download

Solution 1499140

Submitted on 21 Apr 2018 by Michael
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status 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 )) );

PX:20.00 PY:720.00 VX:-40.00 VY:60.00 t1:0.50 t2:45.45 y1:750.00 y2:386.36 paddle: 700.00 newpos:700.00 pdir:0.00 PX:42.00 PY:446.50 VX:-44.00 VY:-63.00 t1:0.95 t2:41.32 y1:386.36 y2:347.11 paddle: 336.36 newpos:336.36 pdir:-0.00 PX:13.60 PY:328.65 VX:-48.40 VY:66.15 t1:0.28 t2:37.57 y1:347.24 y2:956.46 paddle: 397.24 newpos:397.24 pdir:0.00 PX:45.17 PY:897.59 VX:-53.24 VY:69.46 t1:0.85 t2:34.15 y1:956.52 y2:552.86 paddle: 906.52 newpos:906.52 pdir:-0.00 PX:0.22 PY:552.98 VX:-58.56 VY:-72.93 t1:0.00 t2:31.05 y1:552.70 y2:175.29 paddle: 502.70 newpos:502.70 pdir:0.00 PX:3.78 PY:179.46 VX:-64.42 VY:-76.58 t1:0.06 t2:28.22 y1:174.97 y2:94.38 paddle: 124.97 newpos:124.97 pdir:0.00 PX:20.27 PY:71.52 VX:-70.86 VY:80.41 t1:0.29 t2:25.66 y1:94.52 y2:260.72 paddle: 144.52 newpos:144.52 pdir:0.00 PX:73.68 PY:180.96 VX:-77.95 VY:84.43 t1:0.95 t2:23.33 y1:260.77 y2:328.50 paddle: 310.77 newpos:310.77 pdir:0.00 PX:23.63 PY:304.28 VX:-85.74 VY:88.65 t1:0.28 t2:21.20 y1:328.71 y2:302.45 paddle: 278.71 newpos:278.71 pdir:-0.00 PX:45.53 PY:257.62 VX:-94.32 VY:93.08 t1:0.48 t2:19.28 y1:302.55 y2:186.58 paddle: 252.55 newpos:252.55 pdir:-0.00 PX:79.00 PY:112.24 VX:-103.75 VY:97.73 t1:0.76 t2:17.52 y1:186.65 y2:14.96 paddle: 136.65 newpos:136.65 pdir:-0.00 PX:33.05 PY:44.51 VX:-114.12 VY:-102.62 t1:0.29 t2:15.93 y1:14.78 y2:298.14 paddle: 64.78 newpos:64.78 pdir:-0.00 PX:27.88 PY:322.02 VX:-125.54 VY:-107.75 t1:0.22 t2:14.48 y1:298.09 y2:659.47 paddle: 348.09 newpos:348.09 pdir:0.00 PX:98.38 PY:739.70 VX:-138.09 VY:-113.14 t1:0.71 t2:13.17 y1:659.09 y2:905.04 paddle: 709.09 newpos:709.09 pdir:-0.00 PX:134.73 PY:800.13 VX:-151.90 VY:118.80 t1:0.89 t2:11.97 y1:905.49 y2:398.53 paddle: 855.49 newpos:855.49 pdir:-0.00 PX:143.32 PY:291.61 VX:-167.09 VY:124.74 t1:0.86 t2:10.88 y1:398.60 y2:176.23 paddle: 348.60 newpos:348.60 pdir:-0.00 PX:136.57 PY:273.31 VX:-183.80 VY:-130.97 t1:0.74 t2:9.89 y1:175.99 y2:815.60 paddle: 225.99 newpos:225.99 pdir:0.00 PX:129.48 PY:903.34 VX:-202.18 VY:-137.52 t1:0.64 t2:8.99 y1:815.26 y2:483.29 paddle: 675.99 newpos:765.26 pdir:1.00 Oops 1 PX:19.00 PY:508.00 VX:-51.00 VY:-32.00 t1:0.37 t2:35.65 y1:496.08 y2:701.78 paddle: 546.08 newpos:546.08 pdir:-0.00 PX:1.44 PY:700.96 VX:-56.10 VY:33.60 t1:0.03 t2:32.41 y1:701.82 y2:154.76 paddle: 651.82 newpos:651.82 pdir:0.00 PX:27.28 PY:170.25 VX:-61.71 VY:-35.28 t1:0.44 t2:29.46 y1:154.65 y2:936.79 paddle: 204.65 newpos:204.65 pdir:0.00 PX:61.82 PY:903.15 VX:-67.88 VY:37.04 t1:0.91 t2:26.78 y1:936.88 y2:21.29 paddle: 886.88 newpos:886.88 pdir:-0.00 PX:52.54 PY:48.51 VX:-74.67 VY:-38.90 t1:0.70 t2:24.35 y1:21.14 y2:973.33 paddle: 71.14 newpos:71.14 pdir:-0.00 PX:5.11 PY:970.96 VX:-82.14 VY:40.84 t1:0.06 t2:22.14 y1:973.50 y2:77.23 paddle: 923.50 newpos:923.50 pdir:0.00 PX:18.24 PY:85.82 VX:-90.35 VY:-42.88 t1:0.20 t2:20.12 y1:77.16 y2:828.96 paddle: 127.16 newpos:127.16 pdir:-0.00 PX:33.26 PY:814.08 VX:-99.38 VY:45.03 t1:0.33 t2:18.29 y1:829.15 y2:305.91 paddle: 779.15 newpos:779.15 pdir:-0.00 PX:69.42 PY:335.80 VX:-109.32 VY:-47.28 t1:0.64 t2:16.63 y1:305.78 y2:519.84 paddle: 355.78 newpos:355.78 pdir:-0.00 PX:32.73 PY:506.47 VX:-120.26 VY:49.64 t1:0.27 t2:15.12 y1:519.98 y2:691.93 paddle: 569.98 newpos:569.98 pdir:-0.00 PX:52.13 PY:712.41 VX:-132.28 VY:-52.12 t1:0.39 t2:13.74 y1:691.87 y2:60.40 paddle: 641.87 newpos:641.87 pdir:0.00 PX:20.30 PY:52.78 VX:-145.51 VY:54.73 t1:0.14 t2:12.50 y1:60.41 y2:778.49 paddle: 110.41 newpos:110.41 pdir:0.00 PX:102.49 PY:741.84 VX:-160.06 VY:57.47 t1:0.64 t2:11.36 y1:778.64 y2:535.93 paddle: 660.41 newpos:728.64 pdir:1.00 Oops 2 PX:26.00 PY:717.00 VX:-62.00 VY:-29.00 t1:0.42 t2:29.33 y1:704.84 y2:188.12 paddle: 654.84 newpos:654.84 pdir:0.00 PX:51.30 PY:165.32 VX:-68.20 VY:30.45 t1:0.75 t2:26.66 y1:188.23 y2:959.40 paddle: 238.23 newpos:238.23 pdir:-0.00 PX:31.22 PY:972.64 VX:-75.02 VY:-31.97 t1:0.42 t2:24.24 y1:959.33 y2:145.70 paddle: 909.33 newpos:909.33 pdir:-0.00 PX:54.44 PY:167.73 VX:-82.52 VY:-33.57 t1:0.66 t2:22.03 y1:145.58 y2:631.06 paddle: 195.58 newpos:195.58 pdir:0.00 PX:63.18 PY:606.59 VX:-90.77 VY:35.25 t1:0.70 t2:20.03 y1:631.13 y2:627.53 paddle: 581.13 newpos:581.13 pdir:-0.00 PX:72.50 PY:654.40 VX:-99.85 VY:-37.01 t1:0.73 t2:18.21 y1:627.52 y2:80.12 paddle: 577.52 newpos:577.52 pdir:0.00 PX:103.43 PY:43.65 VX:-109.84 VY:38.86 t1:0.94 t2:16.55 y1:80.25 y2:755.73 paddle: 130.25 newpos:130.25 pdir:0.00 PX:60.47 PY:735.41 VX:-120.82 VY:40.81 t1:0.50 t2:15.05 y1:755.83 y2:599.39 paddle: 705.83 newpos:705.83 pdir:-0.00 PX:74.18 PY:623.12 VX:-132.90 VY:-42.85 t1:0.56 t2:13.68 y1:599.21 y2:16.26 paddle: 549.21 newpos:549.21 pdir:0.00 PX:35.45 PY:5.43 VX:-146.19 VY:44.99 t1:0.24 t2:12.44 y1:16.34 y2:603.83 paddle: 66.34 newpos:66.34 pdir:0.00 PX:110.10 PY:571.61 VX:-160.81 VY:47.24 t1:0.68 t2:11.31 y1:603.95 y2:835.26 paddle: 616.34 newpos:653.95 pdir:0.75 PX:175.53 PY:884.45 VX:-176.89 VY:-49.60 t1:0.99 t2:10.28 y1:835.23 y2:299.93 paddle: 785.23 newpos:785.23 pdir:0.00 PX:52.81 PY:314.05 VX:-194.58 VY:-52.08 t1:0.27 t2:9.34 y1:299.91 y2:211.05 paddle: 285.23 newpos:249.91 pdir:-0.71 PX:167.43 PY:177.33 VX:-210.00 VY:54.68 t1:0.80 t2:8.66 y1:220.93 y2:718.05 paddle: 270.93 newpos:270.93 pdir:-0.00 PX:67.43 PY:749.32 VX:-210.00 VY:57.42 t1:0.32 t2:8.66 y1:767.76 y2:710.26 paddle: 717.76 newpos:717.76 pdir:-0.00 PX:177.43 PY:709.23 VX:-210.00 VY:-60.29 t1:0.84 t2:8.66 y1:658.29 y2:110.21 paddle: 608.29 newpos:608.29 pdir:-0.00 PX:77.43 PY:78.91 VX:-210.00 VY:-63.30 t1:0.37 t2:8.66 y1:55.57 y2:519.92 paddle: 158.29 newpos:105.57 pdir:-1.00 PX:187.43 PY:518.04 VX:-210.00 VY:66.47 t1:0.89 t2:8.66 y1:577.37 y2:818.37 paddle: 508.29 newpos:627.37 pdir:1.00 PX:87.43 PY:787.03 VX:-210.00 VY:-69.79 t1:0.42 t2:8.66 y1:757.97 y2:123.50 paddle: 707.97 newpos:707.97 pdir:0.00 PX:197.43 PY:129.24 VX:-210.00 VY:-73.28 t1:0.94 t2:8.66 y1:60.34 y2:605.85 paddle: 307.97 newpos:110.34 pdir:-1.00 Oops 3 PX:48.00 PY:876.00 VX:-73.00 VY:-26.00 t1:0.66 t2:24.91 y1:858.90 y2:178.95 paddle: 808.90 newpos:808.90 pdir:-0.00 PX:45.61 PY:194.41 VX:-80.30 VY:-27.30 t1:0.57 t2:22.64 y1:178.90 y2:470.14 paddle: 228.90 newpos:228.90 pdir:0.00 PX:18.84 PY:464.07 VX:-88.33 VY:28.67 t1:0.21 t2:20.58 y1:470.18 y2:910.28 paddle: 520.18 newpos:520.18 pdir:-0.00 PX:78.23 PY:934.40 VX:-97.16 VY:-30.10 t1:0.81 t2:18.71 y1:910.17 y2:318.79 paddle: 860.17 newpos:860.17 pdir:0.00 PX:56.27 PY:335.29 VX:-106.88 VY:-31.60 t1:0.53 t2:17.01 y1:318.66 y2:245.84 paddle: 268.66 newpos:268.66 pdir:0.00 PX:64.04 PY:227.88 VX:-117.57 VY:33.18 t1:0.54 t2:15.47 y1:245.95 y2:784.79 paddle: 295.95 newpos:295.95 pdir:-0.00 PX:1.91 PY:784.36 VX:-129.32 VY:34.84 t1:0.01 t2:14.06 y1:784.87 y2:700.78 paddle: 734.87 newpos:734.87 pdir:0.00 PX:11.61 PY:703.63 VX:-142.26 VY:-36.58 t1:0.08 t2:12.78 y1:700.64 y2:209.68 paddle: 650.64 newpos:650.64 pdir:-0.00 PX:135.25 PY:242.85 VX:-156.48 VY:-38.41 t1:0.86 t2:11.62 y1:209.65 y2:259.01 paddle: 259.65 newpos:259.65 pdir:0.00 PX:83.77 PY:239.44 VX:-172.13 VY:40.33 t1:0.49 t2:10.56 y1:259.07 y2:706.42 paddle: 309.07 newpos:309.07 pdir:-0.00 PX:9.60 PY:704.29 VX:-189.34 VY:42.35 t1:0.05 t2:9.60 y1:706.44 y2:866.54 paddle: 756.44 newpos:756.44 pdir:0.00 PX:137.00 PY:895.70 VX:-208.28 VY:-44.47 t1:0.66 t2:8.73 y1:866.45 y2:458.84 paddle: 816.45 newpos:816.45 pdir:0.00 PX:38.20 PY:430.33 VX:-210.00 VY:-46.69 t1:0.18 t2:8.66 y1:421.84 y2:2.64 paddle: 371.84 newpos:371.84 pdir:0.00 PX:148.20 PY:10.44 VX:-210.00 VY:49.03 t1:0.71 t2:8.66 y1:45.04 y2:490.74 paddle: 95.04 newpos:95.04 pdir:0.00 PX:48.20 PY:523.26 VX:-210.00 VY:51.48 t1:0.23 t2:8.66 y1:535.08 y2:996.94 paddle: 545.04 newpos:585.08 pdir:0.80 PX:158.20 PY:991.04 VX:-210.00 VY:-54.05 t1:0.75 t2:8.66 y1:950.32 y2:458.94 paddle: 900.32 newpos:900.32 pdir:0.00 PX:58.20 PY:425.65 VX:-210.00 VY:-56.75 t1:0.28 t2:8.66 y1:409.92 y2:106.03 paddle: 450.32 newpos:359.92 pdir:-1.00 PX:168.20 PY:109.83 VX:-210.00 VY:59.59 t1:0.80 t2:8.66 y1:157.56 y2:699.31 paddle: 207.56 newpos:207.56 pdir:0.00 PX:68.20 PY:732.87 VX:-210.00 VY:62.57 t1:0.32 t2:8.66 y1:753.19 y2:677.97 paddle: 657.56 newpos:703.19 pdir:0.91 PX:178.20 PY:677.08 VX:-210.00 VY:-65.70 t1:0.85 t2:8.66 y1:621.33 y2:24.05 paddle: 571.33 newpos:571.33 pdir:0.00 PX:78.20 PY:9.82 VX:-210.00 VY:68.99 t1:0.37 t2:8.66 y1:35.51 y2:662.66 paddle: 121.33 newpos:85.51 pdir:-0.72 PX:188.20 PY:660.36 VX:-210.00 VY:72.44 t1:0.90 t2:8.66 y1:725.27 y2:616.22 paddle: 485.51 newpos:675.27 pdir:1.00 Oops 4 Hits 69 Lives 4 Score 155