Still needs score, losing state, and collision detection for walls.
If you have node installed, you can do this to play:
curl -Lo snake.js http://bit.ly/1jo72pa
#make sure you got the right file:
cat snake.js
node snake
var pr=process;var si="stdin";var so="stdout";var psi=pr[si];var pso=pr[so];var w=pso.columns;var h=pso.rows; | |
var d="r";var as=[];var pi=parseInt;var ps=[{x:pi(w/2,10),y:pi(h/2,10)}]; | |
function p(a){process.stdout.write(a)}var mr=Math.random;function c(){p("\033[2J")} | |
function ga(e){for(var a=0;a<e;a++){var b={x:pi(mr()*w,10),y:pi(mr()*h,10)};as.push(b)}} | |
function dr(){c();for(var a in as){dp(as[a].x,as[a].y,"41")}for(var a in ps){dp(ps[a].x,ps[a].y,"42")}} | |
function dp(b,e,a){p("\033["+e+";"+b+"H");p("\033[0;"+a+"m");p(" ");p("\033[0;0m")} | |
function t(){for(var a=ps.length-1;a>=0;a--){if(a!=0){ps[a].x=ps[a-1].x;ps[a].y=ps[a-1].y} | |
else{switch(d){case"u":ps[0].y--;break;case"r":ps[0].x++;break;case"d":ps[0].y++;break;case"l":ps[0].x--;break} | |
var b=cc();if(b==1){ra(ps[0].x,ps[0].y);at();at();ga(1)}if(b==2){}}}} | |
function ra(b,e){for(var a in as){if(as[a].x==b&&as[a].y==e){delete as[a]}}} | |
function cc(){var a=ps[0];for(var b in as){if(a.x==as[b].x&&a.y==as[b].y){return 1}}} | |
function at(){var e=ps[ps.length-1];var f;var b={x:e.x,y:e.y};var a={u:"d",r:"l",d:"u",l:"r"}; | |
if(ps.length==1){f=a[d]}else{}switch(f){case"u":b.y--;break;case"r":b.x++;break;case"d":b.y++;break;case"l":b.x--;break}ps.push(b)} | |
function tg(a){if(ps.length==1){d=a;return}var b;switch(a){case"u":b=ps[0].y-ps[1].y-1;break; | |
case"r":b=ps[0].x-ps[1].x+1;break;case"d":b=ps[0].y-ps[1].y+1;break;case"l":b=ps[0].x-ps[1].x-1;break}if(b!=0){d=a}} | |
ga(1);psi.resume();psi.setRawMode(!0);psi.setEncoding("utf8");psi.on("data", | |
function(a){switch(a){case"\u001b[A":tg("u");break;case"\u001b[B":tg("d");break;case"\u001b[C":tg("r");break; | |
case"\u001b[D":tg("l");break;case"\u0003":pr.exit();break;default:p(a)}});setInterval(function(){t();dr()},50); |
This can definitely be further minified