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; } public function distance($pos){ return (abs($this->x - $pos->x) + abs($this->y - $pos->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->y; $this->x = $pos->x; $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[$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, $opPos){ $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]); } $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.6
Warning: Attempt to read property "y" on array in /in/EqDLZ on line 28 Warning: Attempt to read property "x" on array in /in/EqDLZ on line 29 Warning: Attempt to read property "y" on array in /in/EqDLZ on line 28 Warning: Attempt to read property "x" on array in /in/EqDLZ on line 29 Fatal error: Uncaught ArgumentCountError: Too few arguments to function Move::maxValDirection(), 4 passed in /in/EqDLZ on line 77 and exactly 5 expected in /in/EqDLZ:38 Stack trace: #0 /in/EqDLZ(77): Move::maxValDirection(Array, '', '', '') #1 {main} thrown in /in/EqDLZ on line 38
Process exited with code 255.
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Notice: Trying to get property 'y' of non-object in /in/EqDLZ on line 28 Notice: Trying to get property 'x' of non-object in /in/EqDLZ on line 29 Notice: Trying to get property 'y' of non-object in /in/EqDLZ on line 28 Notice: Trying to get property 'x' of non-object in /in/EqDLZ on line 29 Fatal error: Uncaught ArgumentCountError: Too few arguments to function Move::maxValDirection(), 4 passed in /in/EqDLZ on line 77 and exactly 5 expected in /in/EqDLZ:38 Stack trace: #0 /in/EqDLZ(77): Move::maxValDirection(Array, '', '', '') #1 {main} thrown in /in/EqDLZ on line 38
Process exited with code 255.
Output for 7.3.32 - 7.3.33
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Move::maxValDirection(), 4 passed in /in/EqDLZ on line 77 and exactly 5 expected in /in/EqDLZ:38 Stack trace: #0 /in/EqDLZ(77): Move::maxValDirection(Array, '', '', '') #1 {main} thrown in /in/EqDLZ on line 38
Process exited with code 255.
Output for 7.1.0 - 7.1.33
Notice: Trying to get property of non-object in /in/EqDLZ on line 28 Notice: Trying to get property of non-object in /in/EqDLZ on line 29 Notice: Trying to get property of non-object in /in/EqDLZ on line 28 Notice: Trying to get property of non-object in /in/EqDLZ on line 29 Fatal error: Uncaught ArgumentCountError: Too few arguments to function Move::maxValDirection(), 4 passed in /in/EqDLZ on line 77 and exactly 5 expected in /in/EqDLZ:38 Stack trace: #0 /in/EqDLZ(77): Move::maxValDirection(Array, '', '', '') #1 {main} thrown in /in/EqDLZ on line 38
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33
Notice: Trying to get property of non-object in /in/EqDLZ on line 28 Notice: Trying to get property of non-object in /in/EqDLZ on line 29 Notice: Trying to get property of non-object in /in/EqDLZ on line 28 Notice: Trying to get property of non-object in /in/EqDLZ on line 29 Warning: Missing argument 5 for Move::maxValDirection(), called in /in/EqDLZ on line 77 and defined in /in/EqDLZ on line 38 Notice: Trying to get property of non-object in /in/EqDLZ on line 28 Notice: Trying to get property of non-object in /in/EqDLZ on line 29 Notice: Undefined variable: opPos in /in/EqDLZ on line 41 Notice: String offset cast occurred in /in/EqDLZ on line 48 Notice: Uninitialized string offset: 0 in /in/EqDLZ on line 48 Notice: String offset cast occurred in /in/EqDLZ on line 48 Notice: Uninitialized string offset: 0 in /in/EqDLZ on line 48 Notice: Undefined variable: opPos in /in/EqDLZ on line 41 Notice: String offset cast occurred in /in/EqDLZ on line 48 Notice: Uninitialized string offset: 0 in /in/EqDLZ on line 48 Notice: String offset cast occurred in /in/EqDLZ on line 48 Notice: Uninitialized string offset: 0 in /in/EqDLZ on line 48 Move Object ( [direction] => NONE [value] => -1 [x] => [y] => )

preferences:
239.17 ms | 404 KiB | 330 Q