Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set('max_execution_time', 90);
- header('Content-type: text/html; charset=utf-8');
- class Tetris {
- /**
- * THE number
- */
- private $number;
- /**
- * The number matrice
- */
- private $matrice;
- /**
- *
- */
- private $ignore = array();
- const NUMBER_WIDTH = 6;
- const NUMBER_HEIGHT = 10;
- private $numbers = array(
- 0 => array(
- 'matrice' => array(
- 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1,
- 1, 1, 0, 0, 1, 1,
- 1, 1, 0, 0, 1, 1,
- 1, 1, 0, 0, 1, 1,
- 1, 1, 0, 0, 1, 1,
- 1, 1, 0, 0, 1, 1,
- 1, 1, 0, 0, 1, 1,
- 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1
- )
- ),
- 1 => array(
- 'matrice' => array(
- 0, 0, 0, 0, 1, 1,
- 0, 0, 0, 0, 1, 1,
- 0, 0, 0, 0, 1, 1,
- 0, 0, 0, 0, 1, 1,
- 0, 0, 0, 0, 1, 1,
- 0, 0, 0, 0, 1, 1,
- 0, 0, 0, 0, 1, 1,
- 0, 0, 0, 0, 1, 1,
- 0, 0, 0, 0, 1, 1,
- 0, 0, 0, 0, 1, 1
- )
- ),
- 2 => array(
- 'matrice' => array(
- 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1,
- 0, 0, 0, 0, 1, 1,
- 0, 0, 0, 0, 1, 1,
- 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1,
- 1, 1, 0, 0, 0, 0,
- 1, 1, 0, 0, 0, 0,
- 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1
- )
- ),
- 3 => array(
- 'matrice' => array(
- 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1,
- 0, 0, 0, 0, 1, 1,
- 0, 0, 0, 0, 1, 1,
- 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1,
- 0, 0, 0, 0, 1, 1,
- 0, 0, 0, 0, 1, 1,
- 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1
- )
- ),
- );
- private $historique = array();
- private $hashs = array();
- public function __construct($number)
- {
- if(!is_numeric($number) || $number < 0 || $number > 9)
- throw new Exception('Please check your number.');
- $this->number = $number;
- $this->matrice = $this->getMatrice();
- }
- private function getMatrice()
- {
- return $this->numbers[$this->number]['matrice'];
- }
- private function getRandomBloc()
- {
- return $this->blocs[rand(0, count($this->blocs) - 1)];
- }
- public function proceed()
- {
- $continue = true;
- $pass = 0;
- do {
- if(count($this->ignore) == count(Bloc::$blocs))
- {
- echo 'PAS DE BLOC DISPOS<br />';
- $this->back();
- continue;
- }
- $pixels = $this->getFirstPixel();
- if(!$pixels)
- {
- echo 'FINISH !';
- break;
- }
- //var_dump($pixels);
- // exit();
- $bloc = Bloc::getRandom($this->ignore); // $this->getRandomBloc();
- // Can't move : get another bloc
- if(!$bloc->moveX($pixels['x']) || !$bloc->moveY(Tetris::NUMBER_HEIGHT - $pixels['y']) ||
- $pixels['x'] == 0 && $bloc->getMatricePos($pixels['y'] * Tetris::NUMBER_WIDTH - (Tetris::NUMBER_WIDTH + $pixels['x'])) == 0)
- {
- echo 'Bloc : '.$bloc->getTitle().' can\'t move or push.<br />'."\n";
- $this->ignore[] = $bloc->getIndex();
- continue;
- }
- Tetris::printMatrice($bloc->getMatrice());
- $count1 = 0;
- foreach($this->matrice AS $mk => $mv)
- {
- // if($mv == 3)
- // continue;
- $this->matrice[$mk] += $bloc->getMatricePos($mk);
- if($this->matrice[$mk] == 1)
- $count1++;
- }
- $this->historique[] = array('x' => $pixels['x'], 'y' => $pixels['y'], 'bloc' => $bloc->getIndex(), 'ignore' => $this->ignore);
- Tetris::printMatrice($this->matrice);
- if($count1 == 0 || $pass >= 500)
- {
- echo 'FINISH OR CANCEL : '.$pass;
- $continue = false;
- }
- $pass++;
- if($this->check())
- $this->ignore = array();
- echo '<hr />';
- } while($continue);
- }
- public function check()
- {
- foreach($this->matrice AS $mk => $mv)
- {
- // > 3 OR == 2
- if($mv > 3 || $mv == 2)
- {
- // TODO: cancel last action and get another bloc
- // return false;
- echo 'ITEM is wrong : '.$mk.' => '.$mv.'<br />';
- $this->back();
- return false;
- // throw new Exception('Error at index '.$mk.' => '.$mv.' (1)');
- }
- // Check if index top or index right equal 1
- if($mv == 1 && $mk > Tetris::NUMBER_WIDTH && $this->matrice[$mk-Tetris::NUMBER_WIDTH] != 1 && $mk < Tetris::NUMBER_WIDTH && $this->matrice[$mk+1] != 1)
- {
- echo 'ITEM is isolated : '.$mk.' => '.$mv.'<br />';
- $this->back();
- return false;
- // throw new Exception('Error at index '.$mk.' => '.$mv.' (2)');
- }
- // Add another check
- }
- return true;
- }
- private function back()
- {
- // var_dump($this->historique);
- // Get the last added
- foreach(array_reverse($this->historique) AS $key => $value)
- {
- if(isset($value['remove']))
- continue;
- echo 'CANCEL : ';
- var_dump($value);
- $bloc = new Bloc($value['bloc']);
- $bloc->moveX($value['x']);
- $bloc->moveY(Tetris::NUMBER_HEIGHT - $value['y']);
- foreach($this->matrice AS $mk => $mv)
- $this->matrice[$mk] -= $bloc->getMatricePos($mk);
- $this->historique[$key]['remove'] = 1;
- $this->ignore = $value['ignore']; // array(); // $bloc->getIndex();
- break;
- }
- }
- /**
- * R�cup�ration du premier "pixel" non trait�
- */
- function getFirstPixel()
- {
- $temp = array();
- foreach($this->matrice AS $i => $c)
- $temp[floor($i/Tetris::NUMBER_WIDTH)][] = $c;
- $temp = array_reverse($temp);
- foreach($temp AS $l => $data)
- {
- foreach($data AS $i => $t)
- {
- if($t == 1)
- return array('y' => (Tetris::NUMBER_HEIGHT - $l), 'x' => $i, 'index' => $l * Tetris::NUMBER_WIDTH + $i);
- }
- }
- return false;
- }
- public static function printMatrice($matrice)
- {
- echo '<pre>';
- foreach($matrice AS $i => $v)
- {
- echo $v.' ';
- if(($i + 1) % Tetris::NUMBER_WIDTH == 0)
- echo '<br />';
- }
- echo '</pre>';
- }
- }
- class Bloc {
- private $index;
- private $matrice;
- private $title;
- private $width;
- private $height;
- public static $blocs = array(
- 0 => array(
- 'title' => 'I horitontal',
- 'matrice' => array(
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 2,2,2,2,0,0
- ),
- 'width' => 4,
- 'height' => 1
- ),
- 1 => array(
- 'title' => 'I vertical',
- 'matrice' => array(
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 2,0,0,0,0,0,
- 2,0,0,0,0,0,
- 2,0,0,0,0,0,
- 2,0,0,0,0,0
- ),
- 'width' => 1,
- 'height' => 4
- ),
- 2 => array(
- 'title' => 'L droit',
- 'matrice' => array(
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 2,0,0,0,0,0,
- 2,0,0,0,0,0,
- 2,2,0,0,0,0
- ),
- 'width' => 2,
- 'height' => 3
- ),
- 3 => array(
- 'title' => 'J 90°',
- 'matrice' => array(
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 2,0,0,0,0,0,
- 2,2,2,0,0,0
- ),
- 'width' => 3,
- 'height' => 2
- ),
- 4 => array(
- 'title' => 'L 180°',
- 'matrice' => array(
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,2,0,0,0,0,
- 0,2,0,0,0,0,
- 2,2,0,0,0,0
- ),
- 'width' => 2,
- 'height' => 3
- ),
- 5 => array(
- 'title' => 'L -90°',
- 'matrice' => array(
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,2,0,0,0,
- 2,2,2,0,0,0
- ),
- 'width' => 3,
- 'height' => 2
- ),
- 6 => array(
- 'title' => 'T droit',
- 'matrice' => array(
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,2,0,0,0,0,
- 2,2,2,0,0,0
- ),
- 'width' => 3,
- 'height' => 2
- ),
- 7 => array(
- 'title' => 'O',
- 'matrice' => array(
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 2,2,0,0,0,0,
- 2,2,0,0,0,0
- ),
- 'width' => 2,
- 'height' => 2
- ),
- 8 => array(
- 'title' => 'T 90',
- 'matrice' => array(
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 2,0,0,0,0,0,
- 2,2,0,0,0,0,
- 2,0,0,0,0,0
- ),
- 'width' => 2,
- 'height' => 3
- ),
- 9 => array(
- 'title' => 'L 90°',
- 'matrice' => array(
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 2,2,2,0,0,0,
- 2,0,0,0,0,0
- ),
- 'width' => 3,
- 'height' => 2
- ),
- 10 => array(
- 'title' => 'J 180°',
- 'matrice' => array(
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 0,0,0,0,0,0,
- 2,2,0,0,0,0,
- 2,0,0,0,0,0,
- 2,0,0,0,0,0
- ),
- 'width' => 2,
- 'height' => 3
- ),
- );
- public function __construct($index)
- {
- $this->index = $index;
- $this->matrice = self::$blocs[$index]['matrice'];
- $this->title = self::$blocs[$index]['title'];
- $this->width = self::$blocs[$index]['width'];
- $this->height = self::$blocs[$index]['height'];
- }
- public function getIndex()
- {
- return $this->index;
- }
- public function getMatrice()
- {
- return $this->matrice;
- }
- public function getMatricePos($pos)
- {
- return $this->matrice[$pos];
- }
- public function getTitle()
- {
- return $this->title;
- }
- public function moveX($x)
- {
- if($x == 0)
- return true;
- if($this->width + $x > Tetris::NUMBER_WIDTH)
- {
- // TODO: Get another bloc
- return false;
- }
- if($x == 1)
- array_unshift($this->matrice, 0);
- else
- {
- for($i=0;$i<$x;$i++)
- array_unshift($this->matrice, 0);
- }
- array_splice($this->matrice, Tetris::NUMBER_WIDTH * Tetris::NUMBER_HEIGHT);
- return true;
- }
- public function moveY($y)
- {
- if($y == 0)
- return true;
- if($this->height + $y > Tetris::NUMBER_HEIGHT)
- {
- // TODO: Get another bloc
- return false;
- }
- $count = $y * Tetris::NUMBER_WIDTH;
- array_splice($this->matrice, 0, $count);
- $this->matrice = array_pad($this->matrice, Tetris::NUMBER_WIDTH * Tetris::NUMBER_HEIGHT, 0);
- return true;
- }
- public static function getRandom($ignore = array())
- {
- if(count($ignore) == count(self::$blocs))
- throw new Exception('Plus de blocs disponibles !');
- $continue = true;
- do {
- $rand = rand(0, count(self::$blocs) - 1);
- if(isset(self::$blocs[$rand]) && !in_array($rand, $ignore))
- $continue = false;
- } while($continue);
- return new Bloc($rand);
- }
- }
- $tetris = new Tetris(3);
- $tetris->proceed();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement