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    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 520 All time: 5,643 This week: 239Up
Version License PHP version Categories
php-snake-game 1.0.0GNU Lesser Genera...5.4PHP 5, Games
Description Author

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: 38
All time rank: 224126 in Czech Republic Czech Republic
Week rank: 523 Up5 in Czech Republic Czech Republic Up
Innovation award
Innovation award
Nominee: 2x

Details

PTX-Snake

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.

Example

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

$snake_obj = new Snake();
$moves_2_test = [
    'FLERFF', 'EEEELLLL'
];

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

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

// Output
FLERFF: YES
EEEELLLL: 7

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 README.md 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  
 100%
Total:520
This week:0
All time:5,643
This week:239Up

For more information send a message to info at phpclasses dot org.