Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2013
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.54 KB | None | 0 0
  1. <?php
  2.  
  3. ini_set('max_execution_time', 90);
  4.  
  5. header('Content-type: text/html; charset=utf-8');
  6.  
  7. class Tetris {
  8.     /**
  9.      * THE number
  10.      */
  11.     private $number;
  12.  
  13.     /**
  14.      * The number matrice
  15.      */
  16.     private $matrice;
  17.  
  18.     /**
  19.      *
  20.      */
  21.     private $ignore = array();
  22.  
  23.     const NUMBER_WIDTH = 6;
  24.     const NUMBER_HEIGHT = 10;
  25.  
  26.     private $numbers = array(
  27.         0 => array(
  28.             'matrice' => array(
  29.                 1, 1, 1, 1, 1, 1,
  30.                 1, 1, 1, 1, 1, 1,
  31.                 1, 1, 0, 0, 1, 1,
  32.                 1, 1, 0, 0, 1, 1,
  33.                 1, 1, 0, 0, 1, 1,
  34.                 1, 1, 0, 0, 1, 1,
  35.                 1, 1, 0, 0, 1, 1,
  36.                 1, 1, 0, 0, 1, 1,
  37.                 1, 1, 1, 1, 1, 1,
  38.                 1, 1, 1, 1, 1, 1
  39.             )
  40.         ),
  41.         1 => array(
  42.             'matrice' => array(
  43.                 0, 0, 0, 0, 1, 1,
  44.                 0, 0, 0, 0, 1, 1,
  45.                 0, 0, 0, 0, 1, 1,
  46.                 0, 0, 0, 0, 1, 1,
  47.                 0, 0, 0, 0, 1, 1,
  48.                 0, 0, 0, 0, 1, 1,
  49.                 0, 0, 0, 0, 1, 1,
  50.                 0, 0, 0, 0, 1, 1,
  51.                 0, 0, 0, 0, 1, 1,
  52.                 0, 0, 0, 0, 1, 1
  53.             )
  54.         ),
  55.         2 => array(
  56.             'matrice' => array(
  57.                 1, 1, 1, 1, 1, 1,
  58.                 1, 1, 1, 1, 1, 1,
  59.                 0, 0, 0, 0, 1, 1,
  60.                 0, 0, 0, 0, 1, 1,
  61.                 1, 1, 1, 1, 1, 1,
  62.                 1, 1, 1, 1, 1, 1,
  63.                 1, 1, 0, 0, 0, 0,
  64.                 1, 1, 0, 0, 0, 0,
  65.                 1, 1, 1, 1, 1, 1,
  66.                 1, 1, 1, 1, 1, 1
  67.             )
  68.         ),
  69.         3 => array(
  70.             'matrice' => array(
  71.                 1, 1, 1, 1, 1, 1,
  72.                 1, 1, 1, 1, 1, 1,
  73.                 0, 0, 0, 0, 1, 1,
  74.                 0, 0, 0, 0, 1, 1,
  75.                 1, 1, 1, 1, 1, 1,
  76.                 1, 1, 1, 1, 1, 1,
  77.                 0, 0, 0, 0, 1, 1,
  78.                 0, 0, 0, 0, 1, 1,
  79.                 1, 1, 1, 1, 1, 1,
  80.                 1, 1, 1, 1, 1, 1
  81.             )
  82.         ),
  83.     );
  84.  
  85.     private $historique = array();
  86.     private $hashs = array();
  87.  
  88.     public function __construct($number)
  89.     {
  90.         if(!is_numeric($number) || $number < 0 || $number > 9)
  91.             throw new Exception('Please check your number.');
  92.  
  93.         $this->number = $number;
  94.  
  95.         $this->matrice = $this->getMatrice();
  96.     }
  97.  
  98.     private function getMatrice()
  99.     {
  100.         return $this->numbers[$this->number]['matrice'];
  101.     }
  102.  
  103.     private function getRandomBloc()
  104.     {
  105.         return $this->blocs[rand(0, count($this->blocs) - 1)];
  106.     }
  107.  
  108.     public function proceed()
  109.     {
  110.         $continue = true;
  111.         $pass = 0;
  112.  
  113.         do {
  114.             if(count($this->ignore) == count(Bloc::$blocs))
  115.             {
  116.                 echo 'PAS DE BLOC DISPOS<br />';
  117.                 $this->back();
  118.                 continue;
  119.             }
  120.  
  121.             $pixels = $this->getFirstPixel();
  122.  
  123.             if(!$pixels)
  124.             {
  125.                 echo 'FINISH !';
  126.                 break;
  127.             }
  128. //var_dump($pixels);
  129. // exit();
  130.             $bloc = Bloc::getRandom($this->ignore); // $this->getRandomBloc();
  131.  
  132.             // Can't move : get another bloc
  133.             if(!$bloc->moveX($pixels['x']) || !$bloc->moveY(Tetris::NUMBER_HEIGHT - $pixels['y']) ||
  134.                     $pixels['x'] == 0 && $bloc->getMatricePos($pixels['y'] * Tetris::NUMBER_WIDTH - (Tetris::NUMBER_WIDTH + $pixels['x'])) == 0)
  135.             {
  136.                 echo 'Bloc : '.$bloc->getTitle().' can\'t move or push.<br />'."\n";
  137.                 $this->ignore[] = $bloc->getIndex();
  138.                 continue;
  139.             }
  140.  
  141. Tetris::printMatrice($bloc->getMatrice());
  142.  
  143.             $count1 = 0;
  144.             foreach($this->matrice AS $mk => $mv)
  145.             {
  146.                 // if($mv == 3)
  147.                     // continue;
  148.  
  149.                 $this->matrice[$mk] += $bloc->getMatricePos($mk);
  150.  
  151.                 if($this->matrice[$mk] == 1)
  152.                     $count1++;
  153.             }
  154.  
  155.             $this->historique[] = array('x' => $pixels['x'], 'y' => $pixels['y'], 'bloc' => $bloc->getIndex(), 'ignore' => $this->ignore);
  156.  
  157. Tetris::printMatrice($this->matrice);
  158.             if($count1 == 0 || $pass >= 500)
  159.             {
  160.                 echo 'FINISH OR CANCEL : '.$pass;
  161.                 $continue = false;
  162.             }
  163.  
  164.             $pass++;
  165.  
  166.             if($this->check())
  167.                 $this->ignore = array();
  168.  
  169.             echo '<hr />';
  170.  
  171.         } while($continue);
  172.     }
  173.  
  174.     public function check()
  175.     {
  176.         foreach($this->matrice AS $mk => $mv)
  177.         {
  178.             // > 3 OR == 2
  179.             if($mv > 3 || $mv == 2)
  180.             {
  181.                 // TODO: cancel last action and get another bloc
  182.                 // return false;
  183.                 echo 'ITEM is wrong : '.$mk.' => '.$mv.'<br />';
  184.                 $this->back();
  185.                 return false;
  186.  
  187.             //  throw new Exception('Error at index '.$mk.' => '.$mv.' (1)');
  188.             }
  189.  
  190.             // Check if index top or index right equal 1
  191.             if($mv == 1 && $mk > Tetris::NUMBER_WIDTH && $this->matrice[$mk-Tetris::NUMBER_WIDTH] != 1 && $mk < Tetris::NUMBER_WIDTH && $this->matrice[$mk+1] != 1)
  192.             {
  193.                 echo 'ITEM is isolated : '.$mk.' => '.$mv.'<br />';
  194.                 $this->back();
  195.                 return false;
  196.  
  197.             //  throw new Exception('Error at index '.$mk.' => '.$mv.' (2)');
  198.             }
  199.  
  200.             // Add another check
  201.         }
  202.  
  203.         return true;
  204.     }
  205.  
  206.     private function back()
  207.     {
  208.     //  var_dump($this->historique);
  209.  
  210.         // Get the last added
  211.         foreach(array_reverse($this->historique) AS $key => $value)
  212.         {
  213.             if(isset($value['remove']))
  214.                 continue;
  215.  
  216.             echo 'CANCEL : ';
  217.             var_dump($value);
  218.  
  219.             $bloc = new Bloc($value['bloc']);
  220.             $bloc->moveX($value['x']);
  221.             $bloc->moveY(Tetris::NUMBER_HEIGHT - $value['y']);
  222.  
  223.             foreach($this->matrice AS $mk => $mv)
  224.                 $this->matrice[$mk] -= $bloc->getMatricePos($mk);
  225.  
  226.             $this->historique[$key]['remove'] = 1;
  227.  
  228.             $this->ignore = $value['ignore']; // array(); // $bloc->getIndex();
  229.  
  230.             break;
  231.         }
  232.     }
  233.  
  234.     /**
  235.      * R�cup�ration du premier "pixel" non trait�
  236.      */
  237.     function getFirstPixel()
  238.     {
  239.         $temp = array();
  240.  
  241.         foreach($this->matrice AS $i => $c)
  242.             $temp[floor($i/Tetris::NUMBER_WIDTH)][] = $c;
  243.  
  244.         $temp = array_reverse($temp);
  245.  
  246.         foreach($temp AS $l => $data)
  247.         {
  248.             foreach($data AS $i => $t)
  249.             {
  250.                 if($t == 1)
  251.                     return array('y' => (Tetris::NUMBER_HEIGHT - $l), 'x' => $i, 'index' => $l * Tetris::NUMBER_WIDTH + $i);
  252.             }
  253.         }
  254.  
  255.         return false;
  256.     }
  257.  
  258.     public static function printMatrice($matrice)
  259.     {
  260.         echo '<pre>';
  261.         foreach($matrice AS $i => $v)
  262.         {
  263.             echo $v.'&nbsp;';
  264.  
  265.             if(($i + 1) % Tetris::NUMBER_WIDTH == 0)
  266.                 echo '<br />';
  267.         }
  268.         echo '</pre>';
  269.     }
  270. }
  271.  
  272.  
  273. class Bloc {
  274.  
  275.     private $index;
  276.     private $matrice;
  277.     private $title;
  278.     private $width;
  279.     private $height;
  280.  
  281.     public static $blocs = array(
  282.         0 => array(
  283.             'title' => 'I horitontal',
  284.             'matrice' => array(
  285.                 0,0,0,0,0,0,
  286.                 0,0,0,0,0,0,
  287.                 0,0,0,0,0,0,
  288.                 0,0,0,0,0,0,
  289.                 0,0,0,0,0,0,
  290.                 0,0,0,0,0,0,
  291.                 0,0,0,0,0,0,
  292.                 0,0,0,0,0,0,
  293.                 0,0,0,0,0,0,
  294.                 2,2,2,2,0,0
  295.             ),
  296.             'width' => 4,
  297.             'height' => 1
  298.         ),
  299.         1 => array(
  300.             'title' => 'I vertical',
  301.             'matrice' => array(
  302.                 0,0,0,0,0,0,
  303.                 0,0,0,0,0,0,
  304.                 0,0,0,0,0,0,
  305.                 0,0,0,0,0,0,
  306.                 0,0,0,0,0,0,
  307.                 0,0,0,0,0,0,
  308.                 2,0,0,0,0,0,
  309.                 2,0,0,0,0,0,
  310.                 2,0,0,0,0,0,
  311.                 2,0,0,0,0,0
  312.             ),
  313.             'width' => 1,
  314.             'height' => 4
  315.         ),
  316.         2 => array(
  317.             'title' => 'L droit',
  318.             'matrice' => array(
  319.                 0,0,0,0,0,0,
  320.                 0,0,0,0,0,0,
  321.                 0,0,0,0,0,0,
  322.                 0,0,0,0,0,0,
  323.                 0,0,0,0,0,0,
  324.                 0,0,0,0,0,0,
  325.                 0,0,0,0,0,0,
  326.                 2,0,0,0,0,0,
  327.                 2,0,0,0,0,0,
  328.                 2,2,0,0,0,0
  329.             ),
  330.             'width' => 2,
  331.             'height' => 3
  332.         ),
  333.         3 => array(
  334.             'title' => 'J 90°',
  335.             'matrice' => array(
  336.                 0,0,0,0,0,0,
  337.                 0,0,0,0,0,0,
  338.                 0,0,0,0,0,0,
  339.                 0,0,0,0,0,0,
  340.                 0,0,0,0,0,0,
  341.                 0,0,0,0,0,0,
  342.                 0,0,0,0,0,0,
  343.                 0,0,0,0,0,0,
  344.                 2,0,0,0,0,0,
  345.                 2,2,2,0,0,0
  346.             ),
  347.             'width' => 3,
  348.             'height' => 2
  349.         ),
  350.         4 => array(
  351.             'title' => 'L 180°',
  352.             'matrice' => array(
  353.                 0,0,0,0,0,0,
  354.                 0,0,0,0,0,0,
  355.                 0,0,0,0,0,0,
  356.                 0,0,0,0,0,0,
  357.                 0,0,0,0,0,0,
  358.                 0,0,0,0,0,0,
  359.                 0,0,0,0,0,0,
  360.                 0,2,0,0,0,0,
  361.                 0,2,0,0,0,0,
  362.                 2,2,0,0,0,0
  363.             ),
  364.             'width' => 2,
  365.             'height' => 3
  366.         ),
  367.         5 => array(
  368.             'title' => 'L -90°',
  369.             'matrice' => array(
  370.                 0,0,0,0,0,0,
  371.                 0,0,0,0,0,0,
  372.                 0,0,0,0,0,0,
  373.                 0,0,0,0,0,0,
  374.                 0,0,0,0,0,0,
  375.                 0,0,0,0,0,0,
  376.                 0,0,0,0,0,0,
  377.                 0,0,0,0,0,0,
  378.                 0,0,2,0,0,0,
  379.                 2,2,2,0,0,0
  380.             ),
  381.             'width' => 3,
  382.             'height' => 2
  383.         ),
  384.         6 => array(
  385.             'title' => 'T droit',
  386.             'matrice' => array(
  387.                 0,0,0,0,0,0,
  388.                 0,0,0,0,0,0,
  389.                 0,0,0,0,0,0,
  390.                 0,0,0,0,0,0,
  391.                 0,0,0,0,0,0,
  392.                 0,0,0,0,0,0,
  393.                 0,0,0,0,0,0,
  394.                 0,0,0,0,0,0,
  395.                 0,2,0,0,0,0,
  396.                 2,2,2,0,0,0
  397.             ),
  398.             'width' => 3,
  399.             'height' => 2
  400.         ),
  401.         7 => array(
  402.             'title' => 'O',
  403.             'matrice' => array(
  404.                 0,0,0,0,0,0,
  405.                 0,0,0,0,0,0,
  406.                 0,0,0,0,0,0,
  407.                 0,0,0,0,0,0,
  408.                 0,0,0,0,0,0,
  409.                 0,0,0,0,0,0,
  410.                 0,0,0,0,0,0,
  411.                 0,0,0,0,0,0,
  412.                 2,2,0,0,0,0,
  413.                 2,2,0,0,0,0
  414.             ),
  415.             'width' => 2,
  416.             'height' => 2
  417.         ),
  418.         8 => array(
  419.             'title' => 'T 90',
  420.             'matrice' => array(
  421.                 0,0,0,0,0,0,
  422.                 0,0,0,0,0,0,
  423.                 0,0,0,0,0,0,
  424.                 0,0,0,0,0,0,
  425.                 0,0,0,0,0,0,
  426.                 0,0,0,0,0,0,
  427.                 0,0,0,0,0,0,
  428.                 2,0,0,0,0,0,
  429.                 2,2,0,0,0,0,
  430.                 2,0,0,0,0,0
  431.             ),
  432.             'width' => 2,
  433.             'height' => 3
  434.         ),
  435.         9 => array(
  436.             'title' => 'L 90°',
  437.             'matrice' => array(
  438.                 0,0,0,0,0,0,
  439.                 0,0,0,0,0,0,
  440.                 0,0,0,0,0,0,
  441.                 0,0,0,0,0,0,
  442.                 0,0,0,0,0,0,
  443.                 0,0,0,0,0,0,
  444.                 0,0,0,0,0,0,
  445.                 0,0,0,0,0,0,
  446.                 2,2,2,0,0,0,
  447.                 2,0,0,0,0,0
  448.             ),
  449.             'width' => 3,
  450.             'height' => 2
  451.         ),
  452.         10 => array(
  453.             'title' => 'J 180°',
  454.             'matrice' => array(
  455.                 0,0,0,0,0,0,
  456.                 0,0,0,0,0,0,
  457.                 0,0,0,0,0,0,
  458.                 0,0,0,0,0,0,
  459.                 0,0,0,0,0,0,
  460.                 0,0,0,0,0,0,
  461.                 0,0,0,0,0,0,
  462.                 2,2,0,0,0,0,
  463.                 2,0,0,0,0,0,
  464.                 2,0,0,0,0,0
  465.             ),
  466.             'width' => 2,
  467.             'height' => 3
  468.         ),
  469.     );
  470.  
  471.     public function __construct($index)
  472.     {
  473.         $this->index = $index;
  474.  
  475.         $this->matrice = self::$blocs[$index]['matrice'];
  476.         $this->title = self::$blocs[$index]['title'];
  477.         $this->width = self::$blocs[$index]['width'];
  478.         $this->height = self::$blocs[$index]['height'];
  479.     }
  480.  
  481.     public function getIndex()
  482.     {
  483.         return $this->index;
  484.     }
  485.  
  486.     public function getMatrice()
  487.     {
  488.         return $this->matrice;
  489.     }
  490.  
  491.     public function getMatricePos($pos)
  492.     {
  493.         return $this->matrice[$pos];
  494.     }
  495.  
  496.     public function getTitle()
  497.     {
  498.         return $this->title;
  499.     }
  500.  
  501.     public function moveX($x)
  502.     {
  503.         if($x == 0)
  504.             return true;
  505.  
  506.         if($this->width + $x > Tetris::NUMBER_WIDTH)
  507.         {
  508.             // TODO: Get another bloc
  509.             return false;
  510.         }
  511.  
  512.         if($x == 1)
  513.             array_unshift($this->matrice, 0);
  514.         else
  515.         {
  516.             for($i=0;$i<$x;$i++)
  517.                 array_unshift($this->matrice, 0);
  518.         }
  519.  
  520.         array_splice($this->matrice, Tetris::NUMBER_WIDTH * Tetris::NUMBER_HEIGHT);
  521.  
  522.         return true;
  523.     }
  524.  
  525.     public function moveY($y)
  526.     {
  527.         if($y == 0)
  528.             return true;
  529.  
  530.         if($this->height + $y > Tetris::NUMBER_HEIGHT)
  531.         {
  532.             // TODO: Get another bloc
  533.             return false;
  534.         }
  535.  
  536.         $count = $y * Tetris::NUMBER_WIDTH;
  537.  
  538.         array_splice($this->matrice, 0, $count);
  539.  
  540.         $this->matrice = array_pad($this->matrice, Tetris::NUMBER_WIDTH * Tetris::NUMBER_HEIGHT, 0);
  541.  
  542.         return true;
  543.     }
  544.  
  545.     public static function getRandom($ignore = array())
  546.     {
  547.         if(count($ignore) == count(self::$blocs))
  548.             throw new Exception('Plus de blocs disponibles !');
  549.  
  550.         $continue = true;
  551.  
  552.         do {
  553.             $rand = rand(0, count(self::$blocs) - 1);
  554.  
  555.             if(isset(self::$blocs[$rand]) && !in_array($rand, $ignore))
  556.                 $continue = false;
  557.  
  558.         } while($continue);
  559.        
  560.         return new Bloc($rand);
  561.     }
  562. }
  563.  
  564. $tetris = new Tetris(3);
  565. $tetris->proceed();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement