PHP Classes

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

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-10 (8 months ago) RSS 2.0 feedStarStarStarStar 67%Total: 559 All time: 5,458 This week: 47Up
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
Age: 42
All time rank: 220225 in Czech Republic Czech Republic
Week rank: 178 Up1 in Czech Republic Czech Republic Up
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 (7)  
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 (7)  /  classes  
File Role Description
  Plain text file Snake.php Class Class source
  Plain text file SnakeException.php Class Class source

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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:559
This week:0
All time:5,458
This week:47Up
 User Ratings  
 
 All time
Utility:81%StarStarStarStarStar
Consistency:68%StarStarStarStar
Documentation:62%StarStarStarStar
Examples:62%StarStarStarStar
Tests:75%StarStarStarStar
Videos:-
Overall:67%StarStarStarStar
Rank:443