PHP Classes

PHP Snake Game: Manage and process the moves in a snake game

Recommend this page to a friend!
  Info   View files Example   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-06-02 (3 months ago) RSS 2.0 feedStarStarStarStar 67%Total: 551 All time: 5,487 This week: 65Up
Version License PHP version Categories
php-snake-game 1.0.7GNU Lesser Genera...5.4PHP 5, Games


This class can manage and process the moves in a snake game.

It can initialize the snake game and process sequences of moves in given directions.

The class returns the result of whether the snake survived traversing the given path of directions.

Innovation Award
PHP Programming Innovation award nominee
March 2015
Number 8

Prize: One copy of VS.PHP
The snake game is a very old game from the time of the first arcade machines.

This class can simulate the moves of the snake game to determine if the snake would survive.

Manuel Lemos
Picture of Tomas Pavlatka
  Performance   Level  
Name: Tomas Pavlatka <contact>
Classes: 3 packages by
Country: Czech Republic Czech Republic
Age: 40
All time rank: 220725 in Czech Republic Czech Republic
Week rank: 387 Up1 in Czech Republic Czech Republic Up
Innovation award
Innovation award
Nominee: 2x



Small and simple class to play with old game called Snake. You can initiate a snake and instruct him, where to go. The system will check the path and will let you know whether snake survived or he aet himself on the way.

If everything goes well, system will return -1. If snake aet himself, system will return a number of a step when this happened.


require_once './bootstrap.php';
use \Ptx\Snake;

$snake_obj = new Snake();
$moves_2_test = [

foreach($moves_2_test as $move) {
    try {
        $result = $snake_obj->move_snake($move);

        echo $move . ': ';
        echo ($result == -1) ? 'YES' : $result;
        echo "\r\n";
    } catch(\Ptx\SnakeException $e) {
        echo $e->getMessage();

// Output

Console application

There is also a console application using the same logic. To start it, call this command

php snake.php FLERFF

System will check and will let you know.

  Files folder image Files  
File Role Description
Files folder imageclasses (2 files)
Files folder imagetest (1 file)
Accessible without login Plain text file bootstrap.php Aux. Configuration script
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file Doc. Auxiliary data
Accessible without login Plain text file snake.php Example Example script

  Files folder image Files  /  classes  
File Role Description
  Plain text file Snake.php Class Class source
  Plain text file SnakeException.php Class Class source

  Files folder image Files  /  test  
File Role Description
  Accessible without login Plain text file SnakeTest.php Test Unit test script

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:5,487
This week:65Up
 User Ratings  
 All time