tomarrell / snake

Parallel, abstract Snake engine :snake:

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Snake

A parallel, abstract, completely overengineered snake game engine, with pre-built adapters for supporting different clients.

Simulate multiple snake games in parallel as a large set of finite state machines. Abstracted over directional input returning state changes. Each game has a unique board size and tick rate. Allowing it to support games across varied clients using custom adapters.

Demos

Demo of the web adapter: here

Note: The server for the web demo hosted in us-east region, so if you are located too far from the region latency will start to play a role. If you don't want to spin up a machine for each region, have a look at the validator adapter, which is capable of verifying a client side simulation.

Demo of the term-snake adapter: Term-snake

Overview

The core of the project can be found within ./engine, with the adapters occupying the sibling directories. This models allows for arbitrary clients to interface with the engine.

Depending on your use case, a particular adapter may already be applicable.

web - The web adapter implements a websocket interface on top of the engine. This spins up a server which runs a game per connection. Each connection is isolated, and cannot affect the game of any other connection. The adapter provides a set of messages to provide game input, and send state messages back to the client. The entire simulation occurs on the server, with the client only being required to implement the render and input relay logic, an example in <100 lines can be found here. This however does leave you at the mercy of the latency between the client and server. Anecdotal testing finds an acceptable range of up to ~150ms.

validator - The validator adapter provides the ability for the engine to confirm a series of "ticks" given to it by a client. A tick represents a single state change within the game (the snake moving a single position in any direction). The adapter is built to receive a description of the start of a game, which it generates fruit for, signs, and returns. This signature must be returned in all further requests. The client then records the ticks taken to move to the piece of randomly generated fruit, and sends this along with other information and the signature back to the server for validation. The benefit of this approach is that the server remains stateless regardless of how many games are being played simultaneously.

term-snake - Proof of concept in the terminal. Not a particularly useful adapter, but a good example of the versatility of the adapters on top of the engine.

Installation

You can build your own adapter by importing github.com/tomarrell/snake and then using the exported methods there to build your own adapter on top of the simulation. Just make sure you have Go >= v1.11 with go modules support enabled.

Alternatively, you can use one of the pre-built adapters. Currently built adapters are:

  • Terminal
  • Web
  • Validator

In order to play around with the terminal adapter, run the following.

> go get github.com/tomarrell/snake/term-snake
> term-snake

To run a copy of of the included web adapter, run the following.

> go get github.com/tomarrell/snake/term-snake
> web

This will run the web adapter on port 8080. You can test out the basic client in your browser by navigating to http://localhost:8080. API and socket documentation for implementing your own client can be found in web/README.md.

TODO

Core

  • Engine creation
  • Game creation
  • Game ending
  • Snake logic
  • Input handling
  • State output
  • Fruit
  • Snake collisions with itself

Adapters

  • Basic terminal interface
  • Web server
  • Validator
  • Leaderboard

Contributing

Contributing to this project is more than welcome! Please feel free to propose features as well by filing an issue here.

License

Licensed under MIT or GPLv3.0, whichever you prefer.

About

Parallel, abstract Snake engine :snake:

License:GNU General Public License v3.0


Languages

Language:Go 89.0%Language:HTML 8.7%Language:Makefile 2.3%