<?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), "", "", ""));
?>
preferences:
67.22 ms | 402 KiB | 5 Q