I am trying to solve 8 puzzle problem using breadth first search in PHP, but I keep on getting an error. I tried to find what's the syntax error for the error showing T_Variable in my code, I don't see one.
Parse error: syntax error, unexpected '$this' (T_VARIABLE) in C:\xampp\htdocs\8BFS.php on line 9
My code is
<?php
class Puzzle {
var $pos;
var $sequence;
var $depth;
function __construct($current_pos) {
$this->pos = $current_pos;
$this->sequence = array();
$this->depth=1;
}
function goalTest($goal) {
if ($this->pos === $goal) {
return True;
} else {
return False;
}
}
function possibleMoves() {
$Moves = array();
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
if ($this->pos[$i][$j] == 0) {
break 2;
}
}
}
$this->checkMove($i, $j, $i - 1, $j, $Moves);
$this->checkMove($i, $j, $i + 1, $j, $Moves);
$this->checkMove($i, $j, $i, $j - 1, $Moves);
$this->checkMove($i, $j, $i, $j + 1, $Moves);
return $Moves;
}
function moveBlank($srcRow, $srcCol, $destRow, $destCol) {
$newpos = $this->pos;
$tmp = $newpos[$destRow][$destCol];
$newpos[$destRow][$destCol] = $newpos[$srcRow][$srcCol];
$newpos[$srcRow][$srcCol] = $tmp;
return $newpos;
}
function InSequence($pos) {
for ($i = 0; $i < count($this->sequence); $i++) {
if ($this->sequence === $pos) {
return TRUE;
}
}
return FALSE;
}
function canMove($srcRow, $srcCol, $destRow, $destCol) {
if ($srcRow < 0 or $srcCol < 0 or $destRow < 0 or $destCol < 0) {
return FALSE;
}
if ($srcRow > 2 or $srcCol > 2 or $destRow > 2 or $destCol > 2) {
return FALSE;
}
return TRUE;
}
function checkMove($srcRow, $srcCol, $destRow, $destCol, & $Moves) {
if ($this->canMove($srcRow, $srcCol, $destRow, $destCol)) {
$newpos = $this->moveBlank($srcRow, $srcCol, $destRow, $destCol);
if (!$this->InSequence($newpos)) {
$newMove = new Puzzle($newpos);
$newMove->sequence = array_merge($this->sequence);
$newMove->sequence[] = $newpos;
$Moves[] = $newMove;
}
}
}
function printSequence() {
for ($i = 0; $i < count($this−>sequence); $i++) {
print ("Step $i −−−−−−−<br> ");
$this−>printPos($this−>sequence[$i]);
print("<br>");
}
}
function printPos($pos) {
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
print(" " . $pos[$i][$j] . " ");
}
print("<br>");
}
}
}
$start_time = microtime();
$initial_pos = array(
array(2, 8, 3),
array(1, 6, 4),
array(7, 0, 5)
);
$goal_pos = array(
array(1, 2, 3),
array(8, 0, 4),
array(7, 6, 5)
);
$initial_state = new Puzzle($initial_pos);
$initial_state−>sequence[] = $initial_pos;
$nodeQue = new SplQueue();
$nodeQue−>enqueue($initial_state);
$nodeQue−>rewind();
$i = 1;
while ($nodeQue−>valid()) {
$i++;
if ($i % 10000 == 0) {
print("$i steps have been finished<br>");
print("Unopened Nodes left in the Que = " . $nodeQue−>count() . "<br>");
$end_time = microtime();
$exec_time = $end_time − $start_time;
print ("Time executed to $i steps is $end_time <br>");
}
$current_state = new Puzzle();
$current_state = $nodeQue−>dequeue();
if ($current_state−>goalTest($goal_pos) == TRUE) {
print("Solution found in $i steps<br>");
print("Unopened Nodes left in the Que=" . $nodeQue−>count() . "<br>");
$current_state−>printSequence();
break;
}
$moves = $current_state−>possibleMoves();
foreach ($moves as $move) {
$nodeQue−>enqueue($move);
}
$nodeQue−>rewind();
}
print ("<br>Maximum Memory used " . memory_get_peak_usage());
print ("<br>Current Memory used" . memory_get_usage());
$end_time = microtime();
$time_exec = $end_time − $start_time;
print("<br>Execution time used =" . $time_exec);
?>
I'm stuck at the beginning only, I couldn't find y that error keeps on coming. It used to be unexpected ' ' error , after I removed all the extra spaces, now it shows unexpected '$this'.
What might be the syntax error here, I asked my friend to run the same program and she says it runs perfectly fine and she says she has the same php version installed, I have this installed:
wampserver2.5-Apache-2.4.9-Mysql-5.6.17-php5.5.12-64b