Advertisement
hombretao

Cell

Sep 11th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.66 KB | None | 0 0
  1. <?
  2. ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1);
  3.  
  4. $base = '/cell/';
  5.  
  6. session_start();
  7.  
  8. if( isset( $_GET['kill'] ) )
  9. {
  10.     session_destroy();
  11.     header( 'Location: '.$base );
  12. }
  13.  
  14. class World
  15. {
  16.     var $grid = array();
  17.     var $x = 70;
  18.     var $y = 20;
  19.     var $cell = array();
  20.  
  21.     function __construct()
  22.     {
  23.         for ($i=0; $i < 10; $i++) {
  24.             $this->cell[$i] = new Cell( $i );
  25.         }
  26.     }
  27.  
  28.     function tick()
  29.     {
  30.         // Construir tablero
  31.         for( $y = 0; $y > (-1*$this->y); $y-- )
  32.         {
  33.             $this->grid[$y] = array();
  34.  
  35.             for ($x = 0; $x < $this->x; $x++ )
  36.             {
  37.                 $this->grid[$y][$x] = null;//'('.$x.','.$y.')';
  38.             }
  39.         }
  40.  
  41.         $world = array();
  42.         $world['map'] = '';
  43.  
  44.         // añadir celula al grid
  45.         foreach( $this->cell as $key => $value )
  46.         {
  47.             $this->grid[$value->y][$value->x] = $value->symbol;
  48.         }
  49.  
  50.         // dibujar grid
  51.         foreach ($this->grid as $key => $value)
  52.         {
  53.             foreach ($value as $k => $v)
  54.             {
  55.                 $world['map'] .= ( $v === null ) ? '·' : $v;
  56.             }
  57.  
  58.             $world['map'] .= '<br>';
  59.         }
  60.  
  61.         // Mostrar coordenadas de cada celula
  62.         $world['coords'] = '';
  63.         foreach( $this->cell as $key => $value ):
  64.             $world['coords'] .= $value->symbol.': ( '.$value->x.', '.$value->y.' )<br>';
  65.         endforeach;
  66.  
  67.         // mover celula
  68.         foreach( $this->cell as $key => $value )
  69.         {
  70.             $move = $value->move();
  71.             //echo $value->symbol.': '.$move['direction'].'<br>';
  72.  
  73.             if( isset($move['x']) )
  74.             {
  75.                 if( $move['x'] >= 0 && $move['x'] <= ($this->x-1) )
  76.                 {
  77.                     $value->x = $move['x'];
  78.                 }
  79.             }
  80.            
  81.             if( isset($move['y']) )
  82.             {
  83.                 if( $move['y'] <= 0 && $move['y'] >= -1*($this->y-1) )
  84.                 {
  85.                     $value->y = $move['y'];
  86.                 }
  87.             }
  88.         }
  89.  
  90.         return $world;
  91.     }
  92. }
  93.  
  94. class Cell
  95. {
  96.     var $x;
  97.     var $y;
  98.     var $symbol;
  99.  
  100.  
  101.     function __construct( $id )
  102.     {
  103.         $this->x = 0;
  104.         $this->y = 0;
  105.         $this->symbol = $id;
  106.     }
  107.  
  108.     function move()
  109.     {
  110.         $move = rand(1,8);
  111.  
  112.         switch ($move) {
  113.             case 1:
  114.                 // Norte
  115.                 $direction = 'N';
  116.                 $newY = $this->y+1;
  117.                 break;
  118.  
  119.             case 2:
  120.                 // Sur
  121.                 $direction = 'S';
  122.                 $newY = $this->y-1;
  123.                 break;
  124.  
  125.             case 3:
  126.                 // Este
  127.                 $direction = 'E';
  128.                 $newX = $this->x+1;
  129.                 break;
  130.  
  131.             case 4:
  132.                 // Oeste
  133.                 $direction = 'W';
  134.                 $newX = $this->x-1;
  135.                 break;
  136.  
  137.             case 5:
  138.                 // NE
  139.                 $direction = 'NE';
  140.                 $newX = $this->x+1;
  141.                 $newY = $this->y+1;
  142.                 break;
  143.  
  144.             case 6:
  145.                 // NO
  146.                 $direction = 'NW';
  147.                 $newX = $this->x-1;
  148.                 $newY = $this->y+1;
  149.                 break;
  150.  
  151.             case 7:
  152.                 // SE
  153.                 $direction = 'SE';
  154.                 $newX = $this->x+1;
  155.                 $newY = $this->y-1;
  156.                 break;
  157.  
  158.             case 8:
  159.                 // SO
  160.                 $direction = 'SW';
  161.                 $newX = $this->x-1;
  162.                 $newY = $this->y-1;
  163.                 break;
  164.         }
  165.  
  166.         $move = array();
  167.  
  168.         if( isset($newX) )
  169.         {
  170.             $move['x'] = $newX;
  171.         }
  172.        
  173.         if( isset($newY) )
  174.         {
  175.             $move['y'] = $newY;
  176.         }
  177.  
  178.         $move['direction'] = $direction;
  179.  
  180.         return $move;
  181.     }
  182. }
  183. ?>
  184. <html>
  185. <head>
  186. <? if( !isset( $_GET['stop'] ) ): ?>
  187. <meta http-equiv="REFRESH" content=".2"/>
  188. <? endif; ?>
  189. <meta base="<?=$base?>">
  190. <meta charset="utf-8">
  191. </head>
  192. <body style="text-align: center; font-family: 'monospace';">
  193.     <h1>CELL</h1>
  194.     <p><a href="?stop">detener</a> | <a href="?play">reanudar</a> | <a href="?kill">reiniciar</a></p>
  195.     <? $world = ( !isset( $_SESSION['world'] ) ) ? $world = new World() : $world = unserialize($_SESSION['world']); ?>
  196.     <? $tick = $world->tick(); ?>
  197.     <div style="display: inline-block; width: 60%;">
  198.         <? echo $tick['map']; ?>
  199.     </div>
  200.     <div style="display: inline-block; width: 20%; text-align: left; vertical-align: top;">
  201.         <? echo $tick['coords']; ?>
  202.     </div>
  203.     <? $_SESSION['world'] = serialize($world); ?>
  204. </body>
  205. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement