Git Product home page Git Product logo

rustautosnake's Introduction

RustAutoSnake

Rust auto snake simulates a perfect game of snake using various pathing AIs. The project uses ncurses to enable simple commandline rendering.

Usage

Rust auto snake depends on ncurses: sudo apt-get install libncurses5-dev libncursesw5-dev

Once ncurses is installed, the project can be built and run using cargo cargo run

The user should be presented with a screen like the following:

Controls

  • F1 Exit
  • Space Pause/Unpause
  • w increase time between ticks, slowing down the game speed
  • s decrease time between ticks, speeding up game speed

Pathing

The snake's pathing algorithm is based on randomly generated hamiltonian cycles. At the start of each game, a new random hamiltonian cycle is generated using a randomly weighted undirected graph, prim's algorithm, and a maze following system to translate the resulting glyph into a hamiltonian cycle. This cycle is then used to direct the snake so as to avoid collisions or block-ins. To improve the pathing, sections of the cycle can skipped so long as it moves the head closer to the apple without potentially causing a collission.

Hamiltonian Cycle Generation

The algorithm is outlined by Pascal Sommer in his median article: Generating Hamiltonian Cycles in Rectangular Grid Graphs.

The basic idea follows two basic steps.

  1. Generate a random maze using prim's algorithm on a random graph. So long as the graph represents a grid and the weights for the edges are well distributed, Prim's algorithm generates a maze with no internal loops.
  2. The maze can then be "solved" by following one wall through the entire structure. In this implementation, the right wall is used. This produces a path that outlines the maze and incidentally produces a complete hamiltonian cycle when imprinted on a grid of twice the length and twice the height.

Cycle Skipping

To improve the speed of the snake, the cycle can be shortcut when possible. The hamiltonian cycle described above is stored in a matrix of increasing values. These values can be seen as steps withing the cycle. So long as the snake can only skip segments of the cycle in a strictly increasing manner, there is no concern that the head will skip into a loop already bisected by some segment of the body. In addition, this system allows the snake to ensure that it does not skip past the section of the cycle which contains the apple.

Attempts with A*

A star is also implemented for testing, it can be enabled using cargo run astar. The algorithm is efficient at pathing around the existing structure of the snake, however it lacks many of the strengths of the hamiltonian cycle based system. A* has a tendency to path itself into a corner without realizing and the needed logic to prevent this shortcoming is to complex to be recalculated in real time. For these reasons, the A* algorithm is left strictly as a comparison to its hamiltonian counterpart.

rustautosnake's People

Contributors

donoa avatar

Watchers

James Cloos 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.