Ver Mensaje Individual
  #2 (permalink)  
Antiguo 14/06/2015, 17:05
Avatar de marlanga
marlanga
 
Fecha de Ingreso: enero-2011
Ubicación: Murcia
Mensajes: 1.024
Antigüedad: 13 años, 3 meses
Puntos: 206
Respuesta: [-CONCURSO-] PHP Maraton script #1 - Desafios

NIVEL 1 - A

Código PHP:
Ver original
  1. <?php
  2. abstract class Char
  3. {
  4.     private $charMap;
  5.     private $map;
  6.  
  7.     public function __construct($map)
  8.     {
  9.         $this->map = $map;
  10.         $this->charMap = array(
  11.             0 => ' ',
  12.             1 => '_',
  13.             2 => '|',
  14.             3 => "'",
  15.             4 => '"',
  16.             5 => '/',
  17.             6 => '\\',
  18.             7 => 'U',
  19.             8 => '(',
  20.             9 => ')',
  21.             10 => '>',
  22.             11 => '<',
  23.             12 => '.',
  24.             13 => '-',
  25.             14 => ','
  26.         );
  27.     }
  28.  
  29.     final protected function getChar($index)
  30.     {
  31.         return $this->charMap[$index];
  32.     }
  33.  
  34.     final public function getTotalLines()
  35.     {
  36.         return count($this->map);
  37.     }
  38.  
  39.     final public function getLine($index)
  40.     {
  41.         $str = '';
  42.         foreach ($this->map[$index] as $character) {
  43.             $str .= $this->getChar($character);
  44.         }
  45.         return $str;
  46.     }
  47. }
  48.  
  49. class CharSpace extends Char
  50. {
  51.     public function __construct()
  52.     {
  53.         parent::__construct(array(
  54.             array( 0, 0, 0, 0, 0, 0, 0),
  55.             array( 0, 0, 0, 0, 0, 0, 0),
  56.             array( 0, 0, 0, 0, 0, 0, 0),
  57.             array( 0, 0, 0, 0, 0, 0, 0),
  58.             array( 0, 0, 0, 0, 0, 0, 0),
  59.             array( 0, 0, 0, 0, 0, 0, 0),
  60.             array( 0, 0, 0, 0, 0, 0, 0)
  61.         ));
  62.     }
  63. }
  64.  
  65. class CharH extends Char
  66. {
  67.     public function __construct()
  68.     {
  69.         parent::__construct(array(
  70.             array( 0, 0, 1, 0, 0, 0, 1, 0, 0),
  71.             array( 0, 2, 3, 2, 0, 2, 3, 2, 0),
  72.             array( 5, 2, 0, 2, 1, 2, 0, 2, 6),
  73.             array( 7, 2, 0, 0, 1, 0, 0, 2, 7),
  74.             array( 0, 2, 1, 2, 0, 2, 1, 2, 0),
  75.             array( 0, 5, 5, 0, 0, 0, 6, 6, 0),
  76.             array( 8, 1, 4, 9, 0, 8, 4, 1, 9)
  77.         ));
  78.     }
  79. }
  80.  
  81. class CharO extends Char
  82. {
  83.     public function __construct()
  84.     {
  85.         parent::__construct(array(
  86.             array( 0, 0, 0, 7, 0, 0, 1, 1, 1, 0, 7),
  87.             array( 0, 0, 0, 0, 6, 5, 4, 0, 0, 6, 5),
  88.             array( 0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 2),
  89.             array(12,13,14, 1, 2, 0, 2, 1, 2, 0, 2),
  90.             array( 0, 6, 1, 9,13, 6, 1, 1, 1, 5, 0),
  91.             array( 0, 0, 0, 0, 0, 0, 6, 6, 0, 0, 0),
  92.             array( 0, 0, 0, 0, 0, 8, 4, 1, 9, 0, 0)
  93.         ));
  94.     }
  95. }
  96.  
  97. class CharL extends Char
  98. {
  99.     public function __construct()
  100.     {
  101.         parent::__construct(array(
  102.             array( 0, 0, 0, 1, 0, 0, 0, 0, 0),
  103.             array( 0, 0, 2, 4, 2, 0, 0, 0, 0),
  104.             array( 7, 0, 2, 0, 2, 0, 7, 0, 0),
  105.             array( 0, 6, 2, 0, 2, 5, 1, 1, 0),
  106.             array( 0, 0, 2, 1, 1, 1, 1, 1, 2),
  107.             array( 0, 0, 5, 5, 0, 0, 6, 6, 0),
  108.             array( 0, 8, 1, 4, 9, 8, 4, 1, 9)
  109.         ));
  110.     }
  111. }
  112.  
  113. class CharA extends Char
  114. {
  115.     public function __construct()
  116.     {
  117.         parent::__construct(array(
  118.             array( 0, 0, 0, 0, 1, 0, 0, 0, 0, 0),
  119.             array( 7, 0, 0, 5, 4, 6, 0, 0, 7, 0),
  120.             array( 0, 6, 5, 0, 1, 0, 6, 5, 0, 0),
  121.             array( 0, 5, 0, 1, 1, 1, 0, 6, 0, 0),
  122.             array( 5, 1, 5, 0, 0, 0, 6, 1, 6, 0),
  123.             array( 0, 6, 6, 0, 0, 0, 0, 6, 6, 0),
  124.             array( 8, 4, 1, 9, 0, 0, 8, 4, 1, 9)
  125.         ));
  126.     }
  127. }
  128.  
  129. class CharM extends Char
  130. {
  131.     public function __construct()
  132.     {
  133.         parent::__construct(array(
  134.             array( 0, 0, 1, 1, 0, 0, 1, 1, 0, 0),
  135.             array( 7, 2, 3, 0, 6, 5, 0, 3, 2, 7),
  136.             array( 6, 2, 0, 2, 6, 5, 2, 0, 2, 5),
  137.             array( 0, 2, 0, 2, 0, 0, 2, 0, 2, 0),
  138.             array( 0, 2, 1, 2, 0, 0, 2, 2, 2, 0),
  139.             array( 0,11,11, 0, 0, 0, 0,10,10, 0),
  140.             array( 0, 8,12, 5, 0, 0, 6,12, 9, 0)
  141.         ));
  142.     }
  143. }
  144.  
  145. class CharU extends Char
  146. {
  147.     public function __construct()
  148.     {
  149.         parent::__construct(array(
  150.             array( 0, 0, 0, 1, 0, 0, 0, 1, 0),
  151.             array( 7, 0, 2, 4, 2, 7, 2, 0, 2),
  152.             array( 0, 6, 2, 0, 2, 6, 2, 0, 2),
  153.             array( 0, 0, 2, 0, 2, 1, 2, 0, 2),
  154.             array( 0,11,11, 6, 1, 1, 1, 5, 0),
  155.             array( 0, 8, 1, 1, 9, 2, 2, 0, 0),
  156.             array( 0, 0, 0, 0, 8, 1, 1, 9, 0)
  157.         ));
  158.     }
  159. }
  160.  
  161. class CharN extends Char
  162. {
  163.     public function __construct()
  164.     {
  165.         parent::__construct(array(
  166.             array( 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0),
  167.             array( 0, 2, 0, 6, 0, 2, 4, 2, 0, 0, 0),
  168.             array(11, 2, 0, 0, 6, 2, 0, 2,10, 0, 0),
  169.             array( 7, 2, 0, 2, 6, 0, 0, 2, 7, 0, 0),
  170.             array( 0, 2, 1, 2, 0, 6, 1, 2, 0, 0, 0),
  171.             array( 0, 2, 2, 0, 0, 0, 6, 6,14,13,12),
  172.             array( 0, 8, 1, 4, 9, 0, 0, 8, 1, 5, 0)
  173.         ));
  174.     }
  175. }
  176.  
  177. class CharD extends Char
  178. {
  179.     public function __construct()
  180.     {
  181.         parent::__construct(array(
  182.             array( 0, 0, 1, 1, 1, 1, 0, 0, 0),
  183.             array( 0, 2, 0, 0, 1, 4, 6, 0, 0),
  184.             array( 5, 2, 0, 2, 0, 2, 0, 2, 0),
  185.             array( 7, 2, 0, 2, 1, 2, 0, 2, 6),
  186.             array( 0, 2, 1, 1, 1, 1, 5, 0, 7),
  187.             array( 0, 0, 2, 2, 2, 1, 0, 0, 0),
  188.             array( 0, 8, 1, 1, 9, 1, 9, 0, 0)
  189.         ));
  190.     }
  191. }
  192.  
  193. class CharCollection
  194. {
  195.     private $list;
  196.  
  197.     public function __construct()
  198.     {
  199.         $this->list = array();
  200.     }
  201.  
  202.     public function add(Char $character)
  203.     {
  204.         $this->list[] = $character;
  205.     }
  206.  
  207.     public function toArrayString()
  208.     {
  209.         if (empty($this->list)) {
  210.             return array();
  211.         }
  212.         $strArr = array();
  213.         for ($i = 0, $n = $this->list[0]->getTotalLines(); $i < $n; $i++) {
  214.             $str = '';
  215.             foreach ($this->list as $character) {
  216.                 $str .= $character->getLine($i) . '  ';
  217.             }
  218.             $strArr[] = $str;
  219.         }
  220.         return $strArr;
  221.     }
  222.  
  223.     public function toImage()
  224.     {
  225.         $arr = $this->toArrayString();
  226.         $w = 1050;
  227.         $h = 140;
  228.         $img = imagecreatetruecolor($w, $h);
  229.         $white = imagecolorallocate($img, 255, 255, 255);
  230.         $black = imagecolorallocate($img, 0, 0, 0);
  231.         imagefilledrectangle($img, 0, 0, $w, $h, $white);
  232.         foreach ($arr as $i => $line) {
  233.             imagestring($img, 5, 10, $i * 15, $line, $black);
  234.         }
  235.         return $img;
  236.     }
  237. }
  238.  
  239. $hm = new CharCollection();
  240. $hm->add(new CharH());
  241. $hm->add(new CharO());
  242. $hm->add(new CharL());
  243. $hm->add(new CharA());
  244. $hm->add(new CharSpace());
  245. $hm->add(new CharM());
  246. $hm->add(new CharU());
  247. $hm->add(new CharN());
  248. $hm->add(new CharD());
  249. $hm->add(new CharO());
  250.  
  251. $img = $hm->toImage();
  252.  
  253. header('Content-Type: image/png');
  254. imagepng($img);

http://www.jlabstudio.com/webgl/ejem...dw/nivel1a.php