Coder Social home page Coder Social logo

shell_snake's Introduction

shell_snake in 574 chars (without shebang)

portable posix shell snake game without bashisms, inspired by http://codegolf.stackexchange.com/questions/4480/recreate-a-snake-game-in-a-console-terminal

coded during http://2014.revision-party.net/

I tried to be POSIX compliant (being as much portable as possible and avoid bashisms, even the random-number-generator does not need /proc). You can e.g. play it (using keys WASD) in your native terminal or via a SSH-session. There is also an unuglyfied/readable variant.

Some notes: shell-scripting is not suited for coding games 8-)

  • to be fair, we only used so called 'builtins', which means:
    • no external calls of programs like 'clear', 'stty' or 'tput'
    • because of that, we redraw the whole screen on every move
    • the only used builtins (aka native commands) are:
      • echo, eval, while-loop, let, break, true, read, case, test, set, shift, alias, source
  • there is no random number generator (PRNG), so we have to built our own
  • getting a keystroke blocks, so we have to spawn another thread
    • for getting the event in parent-task we use a tempfile (ugly!)
  • the snake itself is a list, which is cheap:
    • each element is a (x,y)-tuple
    • loosing the tail means: shift the list by 1
    • adding a (new) head means: append a string
  • the grid is internally an array, but shell/sh does not know this:
    • we "emulated" array(x,y) via an ugly eval-call with global vars
  • and finally: we had a lot of fun!

TODO for POSIX:

  • rewrite 'while let $expression' calls (i like the endcondition)
  • replace 'echo' with 'printf', e.g.: printf "\033[H"
  • find a workaround for 'read -n'
  • better kill 2nd thread at gameend

shell_snake's People

Stargazers

Timothy Spann avatar StuckDuck avatar Tony Heckmann avatar

Watchers

Bastian Bittorf avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.