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
2024-01-10 (3 days ago) RSS 2.0 feedStarStarStarStar 67%Total: 556 This week: 2All time: 5,462 This week: 220Up
Version License PHP version Categories
php-snake-game 1.0.9GNU 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
Innovation award
Innovation award
Nominee: 2x

Example

<?php
header
('content-type: text/plain; charset=utf-8');

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();
    }
}


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:556
This week:2
All time:5,462
This week:220Up
 User Ratings  
 
 All time
Utility:81%StarStarStarStarStar
Consistency:68%StarStarStarStar
Documentation:62%StarStarStarStar
Examples:62%StarStarStarStar
Tests:75%StarStarStarStar
Videos:-
Overall:67%StarStarStarStar
Rank:616