3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Pos { var $x; var $y; public function __construct($x, $y){ $this->x = $x; $this->y = $y; } } class Move { var $direction; var $value; var $x; var $y; public static function initializeEmpty(){ return (new Move(array(0, 0), "NONE", -1)); } public function __construct($pos, $direction, $value){ $this->y = $pos[0]; $this->x = $pos[1]; $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){ $best = Move::initializeEmpty(); foreach($moves as $m){ $m->setValue($board, $player, $pos); $best = $m->compare($best); } return $best; } public function setValue($board, $player, $pos){ if ($board[$this->y][$this->x] != CLEAR) $this->value = -10; else $this->value = 1; } } define("CLEAR", chr(45)); define("P1", chr(114)); define("P2", chr(103)); define("WALL", chr(35)); function nextMove($player,$pos,$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)->direction; } function getPlayerPos($player, $pos){ return ord($player) == ord(P1) ? new Pos($pos[1], $pos[0]) : new Pos($pos[3], $pos[2]); } $m1 = Move::initializeEmpty(); $m2 = new Move(array(0, 1), "RIGHT", 10); print_r(Move::maxValDirection(array($m1, $m2), "", "", "")); ?>
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Warning: Uninitialized string offset 0 in /in/Kkrt6 on line 44 Warning: Uninitialized string offset 0 in /in/Kkrt6 on line 44 Warning: Uninitialized string offset 0 in /in/Kkrt6 on line 44 Warning: Uninitialized string offset 1 in /in/Kkrt6 on line 44 Move Object ( [direction] => NONE [value] => -1 [x] => 0 [y] => 0 )
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Warning: Uninitialized string offset 0 in /in/Kkrt6 on line 44 Warning: Uninitialized string offset 0 in /in/Kkrt6 on line 44 Warning: Uninitialized string offset 0 in /in/Kkrt6 on line 44 Warning: Uninitialized string offset 1 in /in/Kkrt6 on line 44 Move Object ( [direction] => NONE [value] => -1 [x] => 0 [y] => 0 )
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Notice: Uninitialized string offset: 0 in /in/Kkrt6 on line 44 Notice: Uninitialized string offset: 0 in /in/Kkrt6 on line 44 Notice: Uninitialized string offset: 0 in /in/Kkrt6 on line 44 Notice: Uninitialized string offset: 1 in /in/Kkrt6 on line 44 Move Object ( [direction] => NONE [value] => -1 [x] => 0 [y] => 0 )
Output for 7.3.32 - 7.3.33
Move Object ( [direction] => NONE [value] => -1 [x] => 0 [y] => 0 )

preferences:
240.78 ms | 401 KiB | 330 Q