Skip to content

Instantly share code, notes, and snippets.

@ecto
Last active December 28, 2015 22:59

Revisions

  1. ecto revised this gist Nov 21, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ Still needs score, losing state, and collision detection for walls.
    If you have node installed, you can do this to play:

    ````
    wget http://bit.ly/1jo72pa
    curl -Lo snake.js http://bit.ly/1jo72pa
    #make sure you got the right file:
    cat snake.js
    node snake
  2. ecto revised this gist Nov 21, 2013. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,9 @@ Still needs score, losing state, and collision detection for walls.

    If you have node installed, you can do this to play:

    curl https://gist.github.com/ecto/7575441/raw/3db9de0a7232f51ecca64108a71e40514b659fc0/snake.js | node
    ````
    wget http://bit.ly/1jo72pa
    #make sure you got the right file:
    cat snake.js
    node snake
    ````
  3. ecto revised this gist Nov 21, 2013. 2 changed files with 3 additions and 3 deletions.
    File renamed without changes.
    6 changes: 3 additions & 3 deletions snake.js
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,6 @@ function at(){var e=ps[ps.length-1];var f;var b={x:e.x,y:e.y};var a={u:"d",r:"l"
    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(true);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);
    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);
  4. ecto revised this gist Nov 21, 2013. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # Snake in your terminal!

    Still needs score, losing state, and collision detection for walls.

    If you have node installed, you can do this to play:

    curl https://gist.github.com/ecto/7575441/raw/3db9de0a7232f51ecca64108a71e40514b659fc0/snake.js | node
  5. ecto revised this gist Nov 21, 2013. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions snake.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,7 @@
    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 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 c(){p("\033[2J")}
    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}
  6. ecto revised this gist Nov 21, 2013. 1 changed file with 19 additions and 1 deletion.
    20 changes: 19 additions & 1 deletion snake.js
    Original file line number Diff line number Diff line change
    @@ -1 +1,19 @@
    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 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 c(){p("\033[2J")}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(true);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);
    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 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 c(){p("\033[2J")}
    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(true);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);
  7. ecto created this gist Nov 21, 2013.
    1 change: 1 addition & 0 deletions snake.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    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 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 c(){p("\033[2J")}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(true);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);