3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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), "", "", "")); ?>
based on RdMl0
Output for 7.2.0
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.1.0 - 7.1.10
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.4.9 - 7.0.20
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] => )
Output for 5.3.19 - 5.3.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 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: Uninitialized string offset: 0 in /in/EqDLZ on line 48 Fatal error: Cannot use string offset as an array in /in/EqDLZ on line 48
Process exited with code 255.