PHP游戏不识别位置


PHP Game doesn't recognize location

我有一个基于php文本的游戏,我正在工作,但有些东西是错误的。我实在无法描述它有多难闻,但是如果我是在位置和我输入一个命令"北"去位置B,它打印位置B的细节和适当的命令位置B,但就像我在位置A .如果我输入"北"位置,位置B和类型的"西方"在命令框中,它会去西部的位置,而不是位置B可能更好理解这一点,看我的(工作)的游戏。如有任何帮助,我将不胜感激。

这是游戏的代码。

<?php
include_once 'index.php';
print($input);
$World = simplexml_load_file("gameworld.xml");
$CurrentPos = 0;
$Done = 0;
print "<br>";
printplace();
function printplace() {
    GLOBAL $World, $CurrentPos;
    $Room = $World->ROOM[$CurrentPos];
    $Name = $Room->NAME;
    $Desc = wordwrap((string)$Room->DESC);
    print "$Name<br>";
    print str_repeat('-', strlen($Name));
    print "<br>$Desc<br>";
    if ((string)$Room->NORTH != '-') {
        $index = (int)$Room->NORTH;
        print "North: {$World->ROOM[$index]->NAME}<br>";
    }
    if ((string)$Room->SOUTH != '-') {
        $index = (int)$Room->SOUTH;
        print "South: {$World->ROOM[$index]->NAME}<br>";
    }
    if ((string)$World->ROOM[$CurrentPos]->WEST != '-') {
        $index = (int)$Room->WEST;
        print "West: {$World->ROOM[$index]->NAME}<br>";
    }
    if ((string)$World->ROOM[$CurrentPos]->EAST != '-') {
        $index = (int)$Room->EAST;
        print "East: {$World->ROOM[$index]->NAME}<br>";
    }
    print "<br>";
}
$input = explode(' ', $input);
print "<br>";
foreach ($input as $command) {
    switch ($command) {
        case 'north':
            if ((string)$World->ROOM[$CurrentPos]->NORTH != '-') {
                $CurrentPos = (int)$World->ROOM[$CurrentPos]->NORTH;
                printplace() ;
            } else {
                print "You cannot go north!<br>";
            }
            break;
        case 'south':
            if ((string)$World->ROOM[$CurrentPos]->SOUTH != '-') {
                $CurrentPos = (int)$World->ROOM[$CurrentPos]->SOUTH;
                printplace() ;
            } else {
                print "You cannot go south!<br>";
            }
            break;
        case 'west':
            if ((string)$World->ROOM[$CurrentPos]->WEST != '-') {
                $CurrentPos = (int)$World->ROOM[$CurrentPos]->WEST;
                printplace() ;
            } else {
                print "You cannot go west!<br>";
            }
            break;
        case 'east':
            if ((string)$World->ROOM[$CurrentPos]->EAST != '-') {
                $CurrentPos = (int)$World->ROOM[$CurrentPos]->EAST;
                printplace() ;
            } else {
                print "You cannot go east!<br>";
            }
            break;
        case 'look':
            printplace() ;
            break;
        default:
            print "not a valid command... <br>";
            break;
    }
}
print "<br>Thanks for playing!<br>";
?>

您应该阅读PHP会话处理:http://php.net/manual/en/intro.session.php

你的游戏参数(如$CurrentPos)会因每个新请求而重置。您需要在请求之间持久化游戏状态。会话就是用来做这些的。

试试这样写:

if (!isset($_SESSION['CurrentPos'])) {
    $_SESSION['CurrentPos'] = 0;
}
$CurrentPos = $_SESSION['CurrentPos'];