Advertisement
Guest User

Untitled

a guest
Feb 20th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.19 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.     Chars
  5.  
  6.     0 => Off
  7.     1 => A traiter
  8.     2 => Ok
  9. */
  10. $char0 = array(
  11.     1, 1, 1, 1, 1, 1,
  12.     1, 1, 1, 1, 1, 1,
  13.     1, 1, 0, 0, 1, 1,
  14.     1, 1, 0, 0, 1, 1,
  15.     1, 1, 0, 0, 1, 1,
  16.     1, 1, 0, 0, 1, 1,
  17.     1, 1, 0, 0, 1, 1,
  18.     1, 1, 0, 0, 1, 1,
  19.     1, 1, 1, 1, 1, 1,
  20.     1, 1, 1, 1, 1, 1,
  21. );
  22.  
  23. $tetrisI = array(1 => array(
  24.     1, 0, 0, 0,
  25.     1, 0, 0, 0,
  26.     1, 0, 0, 0,
  27.     1, 0, 0, 0
  28. ), 2 => array(
  29.     1, 1, 1, 1,
  30.     0, 0, 0, 0,
  31.     0, 0, 0, 0,
  32.     0, 0, 0, 0
  33. ));
  34.  
  35. $tetrisO = array(1 => array(
  36.     1, 1, 0, 0,
  37.     1, 1, 0, 0,
  38.     0, 0, 0, 0,
  39.     0, 0, 0, 0
  40. ));
  41.  
  42. $tetrisT = array(1 => array(
  43.     0, 1, 0, 0,
  44.     1, 1, 1, 0,
  45.     0, 0, 0, 0,
  46.     0, 0, 0, 0
  47. ), 2 => array(
  48.     1, 1, 1, 0,
  49.     0, 1, 0, 0,
  50.     0, 0, 0, 0,
  51.     0, 0, 0, 0
  52. ), 3 => array(
  53.     0, 1, 0, 0,
  54.     1, 1, 0, 0,
  55.     0, 1, 0, 0,
  56.     0, 0, 0, 0
  57. ), 4 => array(
  58.     1, 0, 0, 0,
  59.     1, 1, 0, 0,
  60.     1, 0, 0, 0,
  61.     0, 0, 0, 0
  62. ));
  63.  
  64. $blocs = array(
  65.     1 => array(
  66.         1, 0, 0, 0,
  67.         1, 0, 0, 0,
  68.         1, 0, 0, 0,
  69.         1, 0, 0, 0
  70.     ),
  71.     2 => array(
  72.         1, 1, 1, 1,
  73.         0, 0, 0, 0,
  74.         0, 0, 0, 0,
  75.         0, 0, 0, 0
  76.     ),
  77.     3 => array(
  78.         1, 1, 0, 0,
  79.         1, 1, 0, 0,
  80.         0, 0, 0, 0,
  81.         0, 0, 0, 0
  82.     )
  83. );
  84.  
  85. class TetrisChar
  86. {
  87.     /**
  88.      * La nombre à traiter
  89.      */
  90.     private $number = 0;
  91.  
  92.     /**
  93.      * Tableau contenant la map de notre nombre
  94.      */
  95.     private $map = array();
  96.  
  97.     private $status = array();
  98.  
  99.     /**
  100.      * Historique de l'algo (pour le backtracking)
  101.      */
  102.     private $history = array();
  103.  
  104.    
  105.     private $cols = array(); // 0 à 5
  106.     private $rows = array(); // 0 à 9
  107.  
  108.     /**
  109.      * La liste des bloc disponibles
  110.      */
  111.     private $blockList = array('tetrisI', 'tetrisO', 'tetrisT');
  112.  
  113.     // private $firstIndex = 0;
  114.  
  115.     /**
  116.      * La largeur des caractères
  117.      */
  118.     private $charWidth = 6;
  119.     /**
  120.      * La hauteur des caractères
  121.      */
  122.     private $charHeight = 10;
  123.  
  124.     function __construct($number)
  125.     {
  126.         $this->number = intval($number);
  127.  
  128.         // $this->status = array_pad(array(), $this->charWidth * $this->charHeight, 0);
  129.         // $this->status = 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, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1);
  130.  
  131.         $this->genMap();
  132.         $this->splitChar();
  133.     }
  134.  
  135.     /**
  136.      * On génère les tableaux des lignes et des colonnes en fonction de notre map
  137.      */
  138.     function splitChar()
  139.     {
  140.         foreach($this->map AS $i => $c)
  141.             $this->rows[$i%$this->charWidth][] = $c;
  142.  
  143.         foreach($this->map AS $i => $c)
  144.             $this->cols[floor($i/$this->charWidth)][] = $c;
  145.     }
  146.  
  147.     function check()
  148.     {
  149.         // if(count($this->map) == $this->charWidth * $this->charHeight)
  150.             // return true;
  151.  
  152.         $getIndex = $this->getFirstPixel();
  153.  
  154.         if($getIndex['cols'] > 0)
  155.         {
  156.             // On vérifie si c'est bien un pixel à remplir
  157.             if(in_array($this->cols[$getIndex['cols']-1][$getIndex['rows']], array(0, 2)))
  158.                 echo 'PB !';
  159.         }
  160.  
  161.         foreach($this->getChar() AS $i => $char)
  162.         {
  163.             if(isset($this->map[$i]) && $this->map[$i] != $char)
  164.                 return false;
  165.         }
  166.  
  167.         return true;
  168.     }
  169.  
  170.     /**
  171.      *
  172.      */
  173.     function hardCheck()
  174.     {
  175.         foreach($this->map AS $i => $v)
  176.         {
  177.             if($v == 1)
  178.             {
  179.                 $top = $right = $left = false;
  180.  
  181.                 $row = floor($i / $this->charWidth);
  182.                 $col = $i % $this->charWidth;
  183.  
  184.                 // On vérifie à gauche
  185.                 if($row == 0 || $this->rows[$col][$row - 1] == 1)
  186.                     $top = true;
  187.                 // On vérifie à droite
  188.                 if($col == 5 || $this->cols[$row][$col + 1] == 1)
  189.                     $right = true;
  190.  
  191.                 // Il y a un soucis
  192.                 if(!$top && !$right)
  193.                     echo 'PB !';
  194.             }
  195.         }
  196.  
  197.         return true;
  198.     }
  199.  
  200.     function next()
  201.     {
  202.         if($this->hardCheck())
  203.         {
  204.             // Récupération d'un élément du tableau
  205.             $sh = array_rand($this->blockList);
  206.  
  207.             $blocs = $this->getBlocMap('');
  208.             $pixels = $this->getFirstPixel();
  209.  
  210.             var_dump($pixels);
  211.             foreach($blocs AS $bloc)
  212.             {
  213.                 list($bcW, $bcH) = $this->getBlocSizes($bloc);
  214.  
  215.                 // Le bloc passe en largeur ?
  216.                 if($this->charWidth - $pixels['rows'] < $bcW)
  217.                     echo 'PASSE PAS !';
  218.                
  219.             }
  220.  
  221.             // $this->history[] = '';
  222.  
  223.            
  224.         }
  225.             else
  226.                 $this->back();
  227.     }
  228.  
  229.     private function getBlocMap($bloc)
  230.     {
  231.         global $tetrisI;
  232.  
  233.         return $tetrisI;
  234.     }
  235.  
  236.     private function getBlocSizes($bloc)
  237.     {
  238.         if(is_array($bloc))
  239.         {
  240.             $x = $y = 0;
  241.             $linesX = $linesY = array(
  242.                 1 => 0,
  243.                 2 => 0,
  244.                 3 => 0,
  245.                 4 => 0
  246.             );
  247.  
  248.             foreach($bloc AS $i => $c)
  249.             {
  250.                 $m = $i % 4;
  251.                 $d = $i / 4;
  252.  
  253.                 if($c == 1)
  254.                 {
  255.                     // Max X
  256.                     if($m > $x)
  257.                         $x = $m;
  258.  
  259.                     // Max Y
  260.                     if($d > $y)
  261.                         $y = floor($d);
  262.  
  263.                     // Line X
  264.                    
  265.                 }
  266.             }
  267.  
  268.             return array('maxx' => $x + 1, 'maxy' => $y + 1, 'linesx' => $linesX, 'linesy' => $linesY);
  269.         }
  270.     }
  271.  
  272.     public function merge($bloc)
  273.     {
  274.    
  275.     }
  276.  
  277.     /**
  278.      * Liste des bloc qu'il est possible de placer
  279.      */
  280.     function getPossibleBloc()
  281.     {
  282.         // Max size : 4x4
  283.        
  284.     }
  285.  
  286.     function genMap()
  287.     {
  288.         $this->map = $this->getChar();
  289.  
  290.         // foreach($this->map AS $i => $v)
  291.             // $this->map[$i] = ($v == 1) ? 2 : 0;
  292.     }
  293.  
  294.     /**
  295.     * Récupération du premier "pixel" non traité
  296.     **/
  297.     function getFirstPixel()
  298.     {
  299.         $temp = array();
  300.  
  301.         foreach($this->map AS $i => $c)
  302.             $temp[floor($i/$this->charWidth)][] = $c;
  303.  
  304.         $temp = array_reverse($temp);
  305.  
  306.         foreach($temp AS $l => $data)
  307.         {
  308.             foreach($data AS $i => $t)
  309.             {
  310.                 if($t == 1)
  311.                     return array('cols' => ($this->charHeight - 1 - $l), 'rows' => $i, 'index' => $l * $this->charWidth + $i);
  312.             }
  313.         }
  314.  
  315.         return false;
  316.     }
  317.  
  318.     /**
  319.      * Retour arrière
  320.      */
  321.     function back()
  322.     {
  323.    
  324.     }
  325.  
  326.     function getChar()
  327.     {
  328.         global $char0;
  329.  
  330.         $char = 'char'.$this->number;
  331.  
  332.         return $$char;
  333.     }
  334.  
  335.     // TEST REVERSE
  336.     public function rotate($bloc = array(), $state = 1)
  337.     {
  338.         // 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
  339.         // 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
  340.         $test = array(
  341.             0, 1, 0, 0,
  342.             1, 1, 1, 0,
  343.             0, 0, 0, 0,
  344.             0, 0, 0, 0
  345.         );
  346.  
  347.         // 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0
  348.         $test2 = array(
  349.             1, 0, 0, 0,
  350.             1, 0, 0, 0,
  351.             1, 0, 0, 0,
  352.             1, 0, 0, 0
  353.         );
  354.  
  355.         $new = array_pad(array(), 16, 0);
  356.  
  357.         foreach($test2 AS $i => $v)
  358.         {
  359.             if($v == 1)
  360.             {
  361.             $k = $i % 4 * 4 - 4;
  362. echo $i.' => '.$k.'<br />';
  363.             // $new
  364.             }
  365.         }
  366.     }
  367. }
  368.  
  369. $t = new TetrisChar(0);
  370. // $t->hardCheck();
  371. // echo 'Check : '.$t->check();
  372. // $t->next();
  373. // $t->rotate();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement