Advertisement
Guest User

Untitled

a guest
Feb 20th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Chars
  5.  
  6. 0 => Off
  7. 1 => A traiter
  8. 2 => Ok
  9. */
  10. $chars = array(
  11. 0 => array(
  12. 1, 1, 1, 1, 1, 1,
  13. 1, 1, 1, 1, 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, 0, 0, 1, 1,
  20. 1, 1, 1, 1, 1, 1,
  21. 1, 1, 1, 1, 1, 1
  22. ),
  23. );
  24.  
  25. $blocs = array(
  26. // I vertical
  27. 0 => array(
  28. 1, 0, 0, 0,
  29. 1, 0, 0, 0,
  30. 1, 0, 0, 0,
  31. 1, 0, 0, 0
  32. ),
  33. // I horizontal
  34. 1 => array(
  35. 0, 0, 0, 0,
  36. 0, 0, 0, 0,
  37. 0, 0, 0, 0,
  38. 1, 1, 1, 1
  39. ),
  40. // Carré
  41. 2 => array(
  42. 0, 0, 0, 0,
  43. 0, 0, 0, 0,
  44. 1, 1, 0, 0,
  45. 1, 1, 0, 0
  46. )
  47. );
  48.  
  49. class TetrisChar
  50. {
  51. /**
  52. * La nombre à traiter
  53. */
  54. private $number = 0;
  55.  
  56. /**
  57. * Tableau contenant la map de notre nombre
  58. */
  59. private $map = array();
  60.  
  61. private $status = array();
  62.  
  63. /**
  64. * Historique de l'algo (pour le backtracking)
  65. */
  66. private $history = array();
  67.  
  68.  
  69. private $cols = array(); // 0 à 5
  70. private $rows = array(); // 0 à 9
  71.  
  72. // private $firstIndex = 0;
  73.  
  74. /**
  75. * La largeur des caractères
  76. */
  77. private $charWidth = 6;
  78. /**
  79. * La hauteur des caractères
  80. */
  81. private $charHeight = 10;
  82.  
  83. private $blocSize = 4;
  84.  
  85. function __construct($number)
  86. {
  87. $this->number = intval($number);
  88.  
  89. // $this->status = array_pad(array(), $this->charWidth * $this->charHeight, 0);
  90. // $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);
  91.  
  92. $this->getCharMap();
  93. $this->splitChar();
  94.  
  95. $this->generate();
  96. }
  97.  
  98. /**
  99. * On génère les tableaux des lignes et des colonnes en fonction de notre map
  100. */
  101. function splitChar()
  102. {
  103. foreach($this->map AS $i => $c)
  104. $this->rows[$i%$this->charWidth][] = $c;
  105.  
  106. foreach($this->map AS $i => $c)
  107. $this->cols[floor($i/$this->charWidth)][] = $c;
  108. }
  109.  
  110. function debug()
  111. {
  112. // var_dump($this->rows);
  113. // var_dump($this->cols);
  114. // $this->getRandomBlock();
  115. }
  116.  
  117. function getRandomBlock($ignore = array())
  118. {
  119. global $blocs;
  120.  
  121. $rand = rand(0, count($blocs) - 1);
  122.  
  123. return new Bloc($rand);
  124. }
  125.  
  126. function generate()
  127. {
  128. $ignoreBlocks = array();
  129.  
  130. $bloc = $this->getRandomBlock();
  131.  
  132. // list($blocRows, $blocCols) = $this->splitBloc($bloc);
  133.  
  134. $pixels = $this->getFirstPixel();
  135.  
  136. $startCol = -1;
  137. $blocStartCol = -1;
  138. $pass = 0;
  139.  
  140. foreach(array_reverse($this->cols) AS $coli => $cols)
  141. {
  142. if($coli > $pixels['cols'])
  143. break;
  144.  
  145. if($blocStartCol == -1)
  146. $blocStartCol = $coli;
  147.  
  148. foreach($cols AS $i => $c)
  149. {
  150. if($coli > $pixels['cols'])
  151. break;
  152.  
  153. $index = $this->blocSize - 1 - ($coli - $blocStartCol);
  154.  
  155. if($index < 0)
  156. break;
  157.  
  158. $blocCol = $bloc->getCols($index);
  159.  
  160. if(!$blocCol || !isset($blocCol[$i]))
  161. break;
  162.  
  163. if($c == 0 && $blocCol[$i] == 1)
  164. {
  165. // $ignoreBlocks[] =
  166. echo 'Erreur !';
  167. }
  168.  
  169. echo $coli.'-'.$i.' = '.$c.' ('.$blocCol[$i].')<br />';
  170. }
  171. }
  172. }
  173.  
  174. function check()
  175. {
  176. // if(count($this->map) == $this->charWidth * $this->charHeight)
  177. // return true;
  178.  
  179. $getIndex = $this->getFirstPixel();
  180.  
  181. if($getIndex['cols'] > 0)
  182. {
  183. // On vérifie si c'est bien un pixel à remplir
  184. if(in_array($this->cols[$getIndex['cols']-1][$getIndex['rows']], array(0, 2)))
  185. echo 'PB !';
  186. }
  187.  
  188. foreach($this->getChar() AS $i => $char)
  189. {
  190. if(isset($this->map[$i]) && $this->map[$i] != $char)
  191. return false;
  192. }
  193.  
  194. return true;
  195. }
  196.  
  197. /**
  198. *
  199. */
  200. function hardCheck()
  201. {
  202. foreach($this->map AS $i => $v)
  203. {
  204. if($v == 1)
  205. {
  206. $top = $right = $left = false;
  207.  
  208. $row = floor($i / $this->charWidth);
  209. $col = $i % $this->charWidth;
  210.  
  211. // On vérifie à gauche
  212. if($row == 0 || $this->rows[$col][$row - 1] == 1)
  213. $top = true;
  214. // On vérifie à droite
  215. if($col == 5 || $this->cols[$row][$col + 1] == 1)
  216. $right = true;
  217.  
  218. // Il y a un soucis
  219. if(!$top && !$right)
  220. echo 'PB !';
  221. }
  222. }
  223.  
  224. return true;
  225. }
  226.  
  227. function next()
  228. {
  229. if($this->hardCheck())
  230. {
  231. // Récupération d'un élément du tableau
  232. $sh = array_rand($this->blockList);
  233.  
  234. $blocs = $this->getBlocMap('');
  235. $pixels = $this->getFirstPixel();
  236.  
  237. var_dump($pixels);
  238. foreach($blocs AS $bloc)
  239. {
  240. list($bcW, $bcH) = $this->getBlocSizes($bloc);
  241.  
  242. // Le bloc passe en largeur ?
  243. if($this->charWidth - $pixels['rows'] < $bcW)
  244. echo 'PASSE PAS !';
  245.  
  246. }
  247.  
  248. // $this->history[] = '';
  249.  
  250.  
  251. }
  252. else
  253. $this->back();
  254. }
  255.  
  256. private function getBlocMap($bloc)
  257. {
  258. global $tetrisI;
  259.  
  260. return $tetrisI;
  261. }
  262.  
  263. private function getBlocSizes($bloc)
  264. {
  265. if(is_array($bloc))
  266. {
  267. $x = $y = 0;
  268. $linesX = $linesY = array(
  269. 1 => 0,
  270. 2 => 0,
  271. 3 => 0,
  272. 4 => 0
  273. );
  274.  
  275. foreach($bloc AS $i => $c)
  276. {
  277. $m = $i % 4;
  278. $d = $i / 4;
  279.  
  280. if($c == 1)
  281. {
  282. // Max X
  283. if($m > $x)
  284. $x = $m;
  285.  
  286. // Max Y
  287. if($d > $y)
  288. $y = floor($d);
  289.  
  290. // Line X
  291.  
  292. }
  293. }
  294.  
  295. return array('maxx' => $x + 1, 'maxy' => $y + 1, 'linesx' => $linesX, 'linesy' => $linesY);
  296. }
  297. }
  298.  
  299. public function merge($bloc)
  300. {
  301.  
  302. }
  303.  
  304. /**
  305. * Liste des bloc qu'il est possible de placer
  306. */
  307. function getPossibleBloc()
  308. {
  309. // Max size : 4x4
  310.  
  311. }
  312.  
  313. function getCharMap()
  314. {
  315. global $chars;
  316.  
  317. $this->map = $chars[$this->number];
  318. }
  319.  
  320. /**
  321. * Récupération du premier "pixel" non traité
  322. **/
  323. function getFirstPixel()
  324. {
  325. $temp = array();
  326.  
  327. foreach($this->map AS $i => $c)
  328. $temp[floor($i/$this->charWidth)][] = $c;
  329.  
  330. $temp = array_reverse($temp);
  331.  
  332. foreach($temp AS $l => $data)
  333. {
  334. foreach($data AS $i => $t)
  335. {
  336. if($t == 1)
  337. return array('cols' => ($this->charHeight - 1 - $l), 'rows' => $i, 'index' => $l * $this->charWidth + $i);
  338. }
  339. }
  340.  
  341. return false;
  342. }
  343.  
  344. /**
  345. * Retour arrière
  346. */
  347. function back()
  348. {
  349.  
  350. }
  351.  
  352. // TEST REVERSE
  353. public function rotate($bloc = array(), $state = 1)
  354. {
  355. // 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
  356. // 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
  357. $test = array(
  358. 0, 1, 0, 0,
  359. 1, 1, 1, 0,
  360. 0, 0, 0, 0,
  361. 0, 0, 0, 0
  362. );
  363.  
  364. // 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0
  365. $test2 = array(
  366. 1, 0, 0, 0,
  367. 1, 0, 0, 0,
  368. 1, 0, 0, 0,
  369. 1, 0, 0, 0
  370. );
  371.  
  372. $new = array_pad(array(), 16, 0);
  373.  
  374. foreach($test2 AS $i => $v)
  375. {
  376. if($v == 1)
  377. {
  378. $k = $i % 4 * 4 - 4;
  379. echo $i.' => '.$k.'<br />';
  380. // $new
  381. }
  382. }
  383. }
  384.  
  385. }
  386.  
  387. class Bloc
  388. {
  389. private $bloc;
  390. private $map;
  391.  
  392. private $cols;
  393. private $rows;
  394.  
  395. private $blocSizes;
  396.  
  397. private $blocSize = 4;
  398.  
  399. public function __construct($bloc)
  400. {
  401. $this->bloc = $bloc;
  402.  
  403. $this->setMap();
  404. }
  405.  
  406. private function setMap()
  407. {
  408. global $blocs;
  409.  
  410. $this->map = $blocs[$this->bloc];
  411.  
  412. $this->splitBloc();
  413. $this->setBlocSizes();
  414. }
  415.  
  416. function splitBloc()
  417. {
  418. foreach($this->map AS $i => $c)
  419. $this->rows[$i%$this->blocSize][] = $c;
  420.  
  421. foreach($this->map AS $i => $c)
  422. $this->cols[floor($i/$this->blocSize)][] = $c;
  423. }
  424.  
  425. private function setBlocSizes()
  426. {
  427. $x = $y = 0;
  428. $linesX = $linesY = array(
  429. 1 => 0,
  430. 2 => 0,
  431. 3 => 0,
  432. 4 => 0
  433. );
  434.  
  435. foreach($this->map AS $i => $c)
  436. {
  437. $m = $i % 4;
  438. $d = $i / 4;
  439.  
  440. if($c == 1)
  441. {
  442. // Max X
  443. if($m > $x)
  444. $x = $m;
  445.  
  446. // Max Y
  447. if($d > $y)
  448. $y = floor($d);
  449.  
  450. // Line X
  451.  
  452. }
  453. }
  454.  
  455. $this->blocSizes = array('maxx' => $x + 1, 'maxy' => $y + 1, 'linesx' => $linesX, 'linesy' => $linesY);
  456. }
  457.  
  458. public function getRows($index = -1)
  459. {
  460. if($index >= 0)
  461. {
  462. if(!isset($this->rows[$index]))
  463. return false;
  464.  
  465. return $this->rows[$index];
  466. }
  467.  
  468. return $this->rows;
  469. }
  470.  
  471. public function getCols($index = -1)
  472. {
  473. if($index >= 0)
  474. {
  475. if(!isset($this->cols[$index]))
  476. return false;
  477.  
  478. return $this->cols[$index];
  479. }
  480.  
  481. return $this->cols;
  482. }
  483.  
  484. public function getBlocMaxWidth()
  485. {
  486. return $this->blocSizes['maxx'];
  487. }
  488. }
  489.  
  490. $t = new TetrisChar(0);
  491. $t->debug();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement