Ezelia Posted January 20, 2014 Share Posted January 20, 2014 Just wanted to see at what level I can optimize a minimalistic pong implementation with simple artificial intelligence and score display. here is my 460 bytes version : http://labs.ezelia.com/upong/upong.html and the commented version here : http://labs.ezelia.com/upong/upong-commented.html (see source code) Mike 1 Quote Link to comment Share on other sites More sharing options...
Karma Octopus Posted January 20, 2014 Share Posted January 20, 2014 That's really neat! Inspiring stuff. Quote Link to comment Share on other sites More sharing options...
OzRamos Posted January 27, 2014 Share Posted January 27, 2014 Wow that is really minimal, I learned a lot from looking at the source comments. I don't think I've ever really seen bitwise operations like that before Quote Link to comment Share on other sites More sharing options...
relaxx Posted January 28, 2014 Share Posted January 28, 2014 Nice! Why AI player (at the right side) cannot handle starting ball 5 of 6-7 times?I got score at 5 without doing anything Maybe you should allow AI to move up-down more quickly? FYI - tested on firefox, desktop. Quote Link to comment Share on other sites More sharing options...
relaxx Posted January 28, 2014 Share Posted January 28, 2014 Also you can save additional 3 bytes defining all 20's as variable at start: <body onkeydown=e=event onload="t=c.getContext('2d');t.T=t.fillText;t.R=t.fillRect;e=a=b=0;w=300;x=h=150,y=10,v=1,z=2;A=B=j=20;setInterval('(W=e.which)?A+=(W-39)*2:0;v>0&&x>(Math.random()+.5)*200&&(y<B+10?B-=2:B+=2);x<8&&y>A&&y<A+j||x>290&&y>B&&y<B+j?v=-v:0;y>h||y<0?z=-z:0;x<0?b++:(x>w)?a++:0;x>w||x<0?x=h:0;A<0?A=0:(A>130?A=130:0);x+=v;y+=z;t.clearRect(0,0,w,h);t.R(x,y,4,4);t.R(2,A,6,j);t.R(294,B,6,j);t.T(a,j,10);t.T(b,280,10);',16);"><canvas id=c> 452 Bytes total Initially want to reduce lower than 450 but went out of ideas))Who can do better? Quote Link to comment Share on other sites More sharing options...
Ezelia Posted January 28, 2014 Author Share Posted January 28, 2014 the AI logic is to follow ball when its X coord is between 100 and a random value between 100 and 300, where 300 is the canvas width.if the random value is too low, the lower random number generated the less accurate is AI move. so it completely depend on random number generation there is an unneeded semicolon on the end of code 451 bytes ! <body onkeydown=e=event onload="t=c.getContext('2d');t.T=t.fillText;t.R=t.fillRect;e=a=b=0;w=300;x=h=150,y=10,v=1,z=2;A=B=j=20;setInterval('(W=e.which)?A+=(W-39)*2:0;v>0&&x>(Math.random()+.5)*200&&(y<B+10?B-=2:B+=2);x<8&&y>A&&y<A+j||x>290&&y>B&&y<B+j?v=-v:0;y>h||y<0?z=-z:0;x<0?b++:(x>w)?a++:0;x>w||x<0?x=h:0;A<0?A=0:(A>130?A=130:0);x+=v;y+=z;t.clearRect(0,0,w,h);t.R(x,y,4,4);t.R(2,A,6,j);t.R(294,B,6,j);t.T(a,j,10);t.T(b,280,10);',16)"><canvas id=c> Quote Link to comment Share on other sites More sharing options...
Ezelia Posted January 28, 2014 Author Share Posted January 28, 2014 Wow that is really minimal, I learned a lot from looking at the source comments. I don't think I've ever really seen bitwise operations like that before I removed the bitwise in the minimalized version actually, there is no need to compare X coord to an integer, float values are OK too for comparision Quote Link to comment Share on other sites More sharing options...
relaxx Posted January 28, 2014 Share Posted January 28, 2014 aw yeah, previous semicolon is also not required,and 1 byte from 10's replacing<body onkeydown=e=event onload="t=c.getContext('2d');t.T=t.fillText;t.R=t.fillRect;e=a=b=0;w=300;x=h=150,y=l=10,v=1,z=2;A=B=j=20;setInterval('(W=e.which)?A+=(W-39)*2:0;v>0&&x>(Math.random()+.5)*200&&(y<B+l?B-=2:B+=2);x<8&&y>A&&y<A+j||x>290&&y>B&&y<B+j?v=-v:0;y>h||y<0?z=-z:0;x<0?b++:(x>w)?a++:0;x>w||x<0?x=h:0;A<0?A=0:(A>130?A=130:0);x+=v;y+=z;t.clearRect(0,0,w,h);t.R(x,y,4,4);t.R(2,A,6,j);t.R(294,B,6,j);t.T(a,j,l);t.T(b,280,l)',16)"><canvas id=c>Achievement unlocked - 449! Next goal -- 440)) You should offer this game for non-exculsive licensing 1$ per byte - best price evar! Quote Link to comment Share on other sites More sharing options...
Ezelia Posted January 28, 2014 Author Share Posted January 28, 2014 440 will be hard You should offer this game for non-exculsive licensing 1$ per byte - best price evar! if you find sponsors for that I'll share every $ with you Edit : Achievement unlocked - 449! your count is wrong, we are still at 450 Quote Link to comment Share on other sites More sharing options...
benny! Posted January 28, 2014 Share Posted January 28, 2014 lol .. nice one. I onced created a pong clone called p1nKpong as a Windows executable in just 1KB. Quote Link to comment Share on other sites More sharing options...
Ezelia Posted January 28, 2014 Author Share Posted January 28, 2014 here is a new optimisation the value witch determine AI behaviour(Math.random()+.5)*200 can actually be replaced with thisDate.now()%150+150 witch can be replaced with thisDate.now()%h+h but we can do better !since we only need pseudo random number, we can simply doy*x%h+h at this point I noticed that the AI allways lost the first round, so instead of initializing X with 150 I initialize it with 20 to let AI the time to calculate ball trajectory final code <body onkeydown=e=event onload="t=c.getContext('2d');t.T=t.fillText;t.R=t.fillRect;e=a=b=0;w=300;h=150,y=l=10,v=1,z=2;x=A=B=j=20;setInterval('(W=e.which)?A+=(W-39)*2:0;v>0&&x>y*x%h+h&&(y<B+l?B-=2:B+=2);x<8&&y>A&&y<A+j||x>290&&y>B&&y<B+j?v=-v:0;y>h||y<0?z=-z:0;x<0?b++:(x>w)?a++:0;x>w||x<0?x=h:0;A<0?A=0:(A>130?A=130:0);x+=v;y+=z;t.clearRect(0,0,w,h);t.R(x,y,4,4);t.R(2,A,6,j);t.R(294,B,6,j);t.T(a,j,l);t.T(b,280,l)',16)"><canvas id=c>434 bytes !Editone more byte :instead of initializing y=l=10 I initialize them as y=l=16 and use l in setInterval calltwo more byte by replacing this condition (x>w)?a++:0 with this x>w?a++:0 <body onkeydown=e=event onload="t=c.getContext('2d');t.T=t.fillText;t.R=t.fillRect;e=a=b=0;w=300;h=150,y=l=16,v=1,z=2;x=A=B=j=20;setInterval('(W=e.which)?A+=(W-39)*2:0;v>0&&x>y*x%h+h&&(y<B+l?B-=2:B+=2);x<8&&y>A&&y<A+j||x>290&&y>B&&y<B+j?v=-v:0;y>h||y<0?z=-z:0;x<0?b++:x>w?a++:0;x>w||x<0?x=h:0;A<0?A=0:(A>130?A=130:0);x+=v;y+=z;t.clearRect(0,0,w,h);t.R(x,y,4,4);t.R(2,A,6,j);t.R(294,B,6,j);t.T(a,j,l);t.T(b,280,l)',l)"><canvas id=c>431 bytes ! Quote Link to comment Share on other sites More sharing options...
MarkW Posted February 10, 2014 Share Posted February 10, 2014 Impressive! I was 3-0 up before the CPU managed to hit the ball back Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.