3v4l.org

run code in 300+ PHP versions simultaneously
<?php echo PHP_INT_MIN; echo PHP_INT_MIN * -1; class Cell { var $pos; var $val; public function __construct($pos, $val){ $this->pos = $pos; $this->val = $val; } } class Board { var $cells; public function __construct($raw_array){ $this->cells = array(); $i = 0; foreach($raw_array as $row){ $j = 0; $this->cells[$i] = array(); foreach(str_split($row) as $cell){ $this->cells[$i][$j] = new Cell(new Pos($i, $j), $cell); } } } } class Pos { var $x; var $y; public function __construct($x, $y){ $this->x = $x; $this->y = $y; } public function dist($pos){ return (sqrt(pow($this->x - $pos->x, 2) + pow($this->y - $pos-> y, 2))); } } class Move { var $direction; var $value; var $pos; public static function initializeEmpty(){ return (new Move(array(0, 0), "NONE", -1)); } public function __construct($pos, $direction, $value){ $this->pos = $pos; $this->value = $value; $this->direction = $direction; } public function compare($move){ return $move->value > $this->value ? $move : $this; } public static function maxValDirection($moves, $board, $player, $pos, $opPos){ $best = Move::initializeEmpty(); foreach($moves as $m){ $m->setValue($board, $player, $pos, $opPos); $best = $m->compare($best); } return $best; } public function setValue($board, $player, $pos, $opPos){ if ($board->cells[$this->pos->y][$this->pos->x] != CLEAR) $this->value = -10; else{ if ($this->pos->dist($opPos) == 1){ $this->value = 1; } else{ $this->value = 2; } } } } define("CLEAR", chr(45)); define("P1", chr(114)); define("P2", chr(103)); define("WALL", chr(35)); function nextMove($player, $pos, $opPos, $board){ $moves = array(); array_push($moves, new Move(new Pos($pos->x, $pos->y - 1), "UP", -1)); array_push($moves, new Move(new Pos($pos->x, $pos->y + 1), "DOWN", -1)); array_push($moves, new Move(new Pos($pos->x - 1, $pos->y), "LEFT", -1)); array_push($moves, new Move(new Pos($pos->x + 1, $pos->y), "RIGHT", -1)); return Move::maxValDirection($moves, $board, $player, $pos, $opPos)->direction; } function getPlayerPos($player, $pos){ return ord($player) == ord(P1) ? new Pos($pos[1], $pos[0]) : new Pos($pos[3], $pos[2]); } $fp = fopen("php://stdin", "r"); $player = fgets($fp); //echo $player; $pos = trim(fgets($fp)); $pos = split(' ', $pos); $playerPos = getPlayerPos($player, $pos); $opPos = getPlayerPos(ord($player) == ord(P1) ? P2 : P1, $pos); $board = array(); for ($i=0; $i<15; $i++) { fscanf($fp, "%s", $board[$i]); } print_r($board); $board = new Board($board); print_r($board); $move = nextMove($player,$playerPos,$opPos, $board); echo $move; ?>

preferences:
56.11 ms | 402 KiB | 5 Q