Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1);
- $base = '/cell/';
- session_start();
- if( isset( $_GET['kill'] ) )
- {
- session_destroy();
- header( 'Location: '.$base );
- }
- class World
- {
- var $grid = array();
- var $x = 70;
- var $y = 20;
- var $cell = array();
- function __construct()
- {
- for ($i=0; $i < 10; $i++) {
- $this->cell[$i] = new Cell( $i );
- }
- }
- function tick()
- {
- // Construir tablero
- for( $y = 0; $y > (-1*$this->y); $y-- )
- {
- $this->grid[$y] = array();
- for ($x = 0; $x < $this->x; $x++ )
- {
- $this->grid[$y][$x] = null;//'('.$x.','.$y.')';
- }
- }
- $world = array();
- $world['map'] = '';
- // añadir celula al grid
- foreach( $this->cell as $key => $value )
- {
- $this->grid[$value->y][$value->x] = $value->symbol;
- }
- // dibujar grid
- foreach ($this->grid as $key => $value)
- {
- foreach ($value as $k => $v)
- {
- $world['map'] .= ( $v === null ) ? '·' : $v;
- }
- $world['map'] .= '<br>';
- }
- // Mostrar coordenadas de cada celula
- $world['coords'] = '';
- foreach( $this->cell as $key => $value ):
- $world['coords'] .= $value->symbol.': ( '.$value->x.', '.$value->y.' )<br>';
- endforeach;
- // mover celula
- foreach( $this->cell as $key => $value )
- {
- $move = $value->move();
- //echo $value->symbol.': '.$move['direction'].'<br>';
- if( isset($move['x']) )
- {
- if( $move['x'] >= 0 && $move['x'] <= ($this->x-1) )
- {
- $value->x = $move['x'];
- }
- }
- if( isset($move['y']) )
- {
- if( $move['y'] <= 0 && $move['y'] >= -1*($this->y-1) )
- {
- $value->y = $move['y'];
- }
- }
- }
- return $world;
- }
- }
- class Cell
- {
- var $x;
- var $y;
- var $symbol;
- function __construct( $id )
- {
- $this->x = 0;
- $this->y = 0;
- $this->symbol = $id;
- }
- function move()
- {
- $move = rand(1,8);
- switch ($move) {
- case 1:
- // Norte
- $direction = 'N';
- $newY = $this->y+1;
- break;
- case 2:
- // Sur
- $direction = 'S';
- $newY = $this->y-1;
- break;
- case 3:
- // Este
- $direction = 'E';
- $newX = $this->x+1;
- break;
- case 4:
- // Oeste
- $direction = 'W';
- $newX = $this->x-1;
- break;
- case 5:
- // NE
- $direction = 'NE';
- $newX = $this->x+1;
- $newY = $this->y+1;
- break;
- case 6:
- // NO
- $direction = 'NW';
- $newX = $this->x-1;
- $newY = $this->y+1;
- break;
- case 7:
- // SE
- $direction = 'SE';
- $newX = $this->x+1;
- $newY = $this->y-1;
- break;
- case 8:
- // SO
- $direction = 'SW';
- $newX = $this->x-1;
- $newY = $this->y-1;
- break;
- }
- $move = array();
- if( isset($newX) )
- {
- $move['x'] = $newX;
- }
- if( isset($newY) )
- {
- $move['y'] = $newY;
- }
- $move['direction'] = $direction;
- return $move;
- }
- }
- ?>
- <html>
- <head>
- <? if( !isset( $_GET['stop'] ) ): ?>
- <meta http-equiv="REFRESH" content=".2"/>
- <? endif; ?>
- <meta base="<?=$base?>">
- <meta charset="utf-8">
- </head>
- <body style="text-align: center; font-family: 'monospace';">
- <h1>CELL</h1>
- <p><a href="?stop">detener</a> | <a href="?play">reanudar</a> | <a href="?kill">reiniciar</a></p>
- <? $world = ( !isset( $_SESSION['world'] ) ) ? $world = new World() : $world = unserialize($_SESSION['world']); ?>
- <? $tick = $world->tick(); ?>
- <div style="display: inline-block; width: 60%;">
- <? echo $tick['map']; ?>
- </div>
- <div style="display: inline-block; width: 20%; text-align: left; vertical-align: top;">
- <? echo $tick['coords']; ?>
- </div>
- <? $_SESSION['world'] = serialize($world); ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement