Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Chars
- 0 => Off
- 1 => A traiter
- 2 => Ok
- */
- $chars = array(
- 0 => 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
- ),
- );
- $blocs = array(
- // I vertical
- 0 => array(
- 1, 0, 0, 0,
- 1, 0, 0, 0,
- 1, 0, 0, 0,
- 1, 0, 0, 0
- ),
- // I horizontal
- 1 => array(
- 0, 0, 0, 0,
- 0, 0, 0, 0,
- 0, 0, 0, 0,
- 1, 1, 1, 1
- ),
- // Carré
- 2 => array(
- 0, 0, 0, 0,
- 0, 0, 0, 0,
- 1, 1, 0, 0,
- 1, 1, 0, 0
- )
- );
- class TetrisChar
- {
- /**
- * La nombre à traiter
- */
- private $number = 0;
- /**
- * Tableau contenant la map de notre nombre
- */
- private $map = array();
- private $status = array();
- /**
- * Historique de l'algo (pour le backtracking)
- */
- private $history = array();
- private $cols = array(); // 0 à 5
- private $rows = array(); // 0 à 9
- // private $firstIndex = 0;
- /**
- * La largeur des caractères
- */
- private $charWidth = 6;
- /**
- * La hauteur des caractères
- */
- private $charHeight = 10;
- private $blocSize = 4;
- function __construct($number)
- {
- $this->number = intval($number);
- // $this->status = array_pad(array(), $this->charWidth * $this->charHeight, 0);
- // $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);
- $this->getCharMap();
- $this->splitChar();
- $this->generate();
- }
- /**
- * On génère les tableaux des lignes et des colonnes en fonction de notre map
- */
- function splitChar()
- {
- foreach($this->map AS $i => $c)
- $this->rows[$i%$this->charWidth][] = $c;
- foreach($this->map AS $i => $c)
- $this->cols[floor($i/$this->charWidth)][] = $c;
- }
- function debug()
- {
- // var_dump($this->rows);
- // var_dump($this->cols);
- // $this->getRandomBlock();
- }
- function getRandomBlock($ignore = array())
- {
- global $blocs;
- $rand = rand(0, count($blocs) - 1);
- return new Bloc($rand);
- }
- function generate()
- {
- $ignoreBlocks = array();
- $bloc = $this->getRandomBlock();
- // list($blocRows, $blocCols) = $this->splitBloc($bloc);
- $pixels = $this->getFirstPixel();
- $startCol = -1;
- $blocStartCol = -1;
- $pass = 0;
- foreach(array_reverse($this->cols) AS $coli => $cols)
- {
- if($coli > $pixels['cols'])
- break;
- if($blocStartCol == -1)
- $blocStartCol = $coli;
- foreach($cols AS $i => $c)
- {
- if($coli > $pixels['cols'])
- break;
- $index = $this->blocSize - 1 - ($coli - $blocStartCol);
- if($index < 0)
- break;
- $blocCol = $bloc->getCols($index);
- if(!$blocCol || !isset($blocCol[$i]))
- break;
- if($c == 0 && $blocCol[$i] == 1)
- {
- // $ignoreBlocks[] =
- echo 'Erreur !';
- }
- echo $coli.'-'.$i.' = '.$c.' ('.$blocCol[$i].')<br />';
- }
- }
- }
- function check()
- {
- // if(count($this->map) == $this->charWidth * $this->charHeight)
- // return true;
- $getIndex = $this->getFirstPixel();
- if($getIndex['cols'] > 0)
- {
- // On vérifie si c'est bien un pixel à remplir
- if(in_array($this->cols[$getIndex['cols']-1][$getIndex['rows']], array(0, 2)))
- echo 'PB !';
- }
- foreach($this->getChar() AS $i => $char)
- {
- if(isset($this->map[$i]) && $this->map[$i] != $char)
- return false;
- }
- return true;
- }
- /**
- *
- */
- function hardCheck()
- {
- foreach($this->map AS $i => $v)
- {
- if($v == 1)
- {
- $top = $right = $left = false;
- $row = floor($i / $this->charWidth);
- $col = $i % $this->charWidth;
- // On vérifie à gauche
- if($row == 0 || $this->rows[$col][$row - 1] == 1)
- $top = true;
- // On vérifie à droite
- if($col == 5 || $this->cols[$row][$col + 1] == 1)
- $right = true;
- // Il y a un soucis
- if(!$top && !$right)
- echo 'PB !';
- }
- }
- return true;
- }
- function next()
- {
- if($this->hardCheck())
- {
- // Récupération d'un élément du tableau
- $sh = array_rand($this->blockList);
- $blocs = $this->getBlocMap('');
- $pixels = $this->getFirstPixel();
- var_dump($pixels);
- foreach($blocs AS $bloc)
- {
- list($bcW, $bcH) = $this->getBlocSizes($bloc);
- // Le bloc passe en largeur ?
- if($this->charWidth - $pixels['rows'] < $bcW)
- echo 'PASSE PAS !';
- }
- // $this->history[] = '';
- }
- else
- $this->back();
- }
- private function getBlocMap($bloc)
- {
- global $tetrisI;
- return $tetrisI;
- }
- private function getBlocSizes($bloc)
- {
- if(is_array($bloc))
- {
- $x = $y = 0;
- $linesX = $linesY = array(
- 1 => 0,
- 2 => 0,
- 3 => 0,
- 4 => 0
- );
- foreach($bloc AS $i => $c)
- {
- $m = $i % 4;
- $d = $i / 4;
- if($c == 1)
- {
- // Max X
- if($m > $x)
- $x = $m;
- // Max Y
- if($d > $y)
- $y = floor($d);
- // Line X
- }
- }
- return array('maxx' => $x + 1, 'maxy' => $y + 1, 'linesx' => $linesX, 'linesy' => $linesY);
- }
- }
- public function merge($bloc)
- {
- }
- /**
- * Liste des bloc qu'il est possible de placer
- */
- function getPossibleBloc()
- {
- // Max size : 4x4
- }
- function getCharMap()
- {
- global $chars;
- $this->map = $chars[$this->number];
- }
- /**
- * Récupération du premier "pixel" non traité
- **/
- function getFirstPixel()
- {
- $temp = array();
- foreach($this->map AS $i => $c)
- $temp[floor($i/$this->charWidth)][] = $c;
- $temp = array_reverse($temp);
- foreach($temp AS $l => $data)
- {
- foreach($data AS $i => $t)
- {
- if($t == 1)
- return array('cols' => ($this->charHeight - 1 - $l), 'rows' => $i, 'index' => $l * $this->charWidth + $i);
- }
- }
- return false;
- }
- /**
- * Retour arrière
- */
- function back()
- {
- }
- // TEST REVERSE
- public function rotate($bloc = array(), $state = 1)
- {
- // 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
- // 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
- $test = array(
- 0, 1, 0, 0,
- 1, 1, 1, 0,
- 0, 0, 0, 0,
- 0, 0, 0, 0
- );
- // 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0
- $test2 = array(
- 1, 0, 0, 0,
- 1, 0, 0, 0,
- 1, 0, 0, 0,
- 1, 0, 0, 0
- );
- $new = array_pad(array(), 16, 0);
- foreach($test2 AS $i => $v)
- {
- if($v == 1)
- {
- $k = $i % 4 * 4 - 4;
- echo $i.' => '.$k.'<br />';
- // $new
- }
- }
- }
- }
- class Bloc
- {
- private $bloc;
- private $map;
- private $cols;
- private $rows;
- private $blocSizes;
- private $blocSize = 4;
- public function __construct($bloc)
- {
- $this->bloc = $bloc;
- $this->setMap();
- }
- private function setMap()
- {
- global $blocs;
- $this->map = $blocs[$this->bloc];
- $this->splitBloc();
- $this->setBlocSizes();
- }
- function splitBloc()
- {
- foreach($this->map AS $i => $c)
- $this->rows[$i%$this->blocSize][] = $c;
- foreach($this->map AS $i => $c)
- $this->cols[floor($i/$this->blocSize)][] = $c;
- }
- private function setBlocSizes()
- {
- $x = $y = 0;
- $linesX = $linesY = array(
- 1 => 0,
- 2 => 0,
- 3 => 0,
- 4 => 0
- );
- foreach($this->map AS $i => $c)
- {
- $m = $i % 4;
- $d = $i / 4;
- if($c == 1)
- {
- // Max X
- if($m > $x)
- $x = $m;
- // Max Y
- if($d > $y)
- $y = floor($d);
- // Line X
- }
- }
- $this->blocSizes = array('maxx' => $x + 1, 'maxy' => $y + 1, 'linesx' => $linesX, 'linesy' => $linesY);
- }
- public function getRows($index = -1)
- {
- if($index >= 0)
- {
- if(!isset($this->rows[$index]))
- return false;
- return $this->rows[$index];
- }
- return $this->rows;
- }
- public function getCols($index = -1)
- {
- if($index >= 0)
- {
- if(!isset($this->cols[$index]))
- return false;
- return $this->cols[$index];
- }
- return $this->cols;
- }
- public function getBlocMaxWidth()
- {
- return $this->blocSizes['maxx'];
- }
- }
- $t = new TetrisChar(0);
- $t->debug();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement