我正在尝试在 PHP 中使用广度优先搜索来解决 8 个难题,但我不断收到错误消息。我试图找出在我的代码中显示 T_Variable 的错误的语法错误,但我没有看到。
解析错误:语法错误,第 9 行 C:\xampp\htdocs\8BFS.php 中的意外 '$this' (T_VARIABLE)
我的代码是
<?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);
?>
我只停留在开始,我找不到那个错误不断出现。它曾经是意外的''错误,在我删除了所有多余的空格后,现在它显示了意外的'$this'。
这里的语法错误可能是什么,我让我的朋友运行相同的程序,她说它运行得很好,她说她安装了相同的 php 版本,我安装了这个:
wampserver2.5-Apache-2.4.9-Mysql-5.6.17-php5.5.12-64b