我有一个board.php
文件在我的网页上显示一个看板。该文件包括一次boardEngine.php
文件,该文件初始化了每个变量和矩阵,以及计算所需的每个函数。
我在board.php
上放了一个表格,这样我就可以在黑板上输入我的下一步行动。 board.php
代码是这样的:
<!doctype html>
<html>
<body>
<?php
include_once('boardEngine.php');
?>
<div id='board'>
<?php
if (isset($_GET['move'] )) {
checkMove($_POST['queryMove']); // checkMove is from boardEngine.php
}
printBoard(); // function from boardEngine.php
?>
</div>
<form id="moveForm" action="board.php?move" method="post" >
<input type="text" name="queryMove" placeholder="form: 'e2f3' (e2 to f3)" required> </p>
<input type="submit" value=">move!<" >
</form>
</body>
问题是当我提交移动时,board.php
会重新加载一组$_GET['move']
。由于它被重新加载,似乎boardEngine.php
再次被包含,并且矩阵中的每个位置都被初始化。
据我了解,到目前为止,移动被提交,board.php
被重新加载,boardEngine.php
被包含另一个时间,每个位置都被重置,然后因为$_GET['move']
变量是通过表单设置的,所以将计算一个移动。提交新移动后,将重置板并考虑最后一次移动,依此类推。
我错了吗?如何解决这个问题?
编辑1:这是我的板引擎.php代码的外观:
<?php
define("PAWN", 10);
define("KNIGHT", 20);
define("BISHOP", 30);
define("ROOK", 40);
define("QUEEN", 50);
define("KING", 100);
define("B_PAWN", -10);
define("B_KNIGHT", -20);
define("B_BISHOP", -30);
define("B_ROOK", -40);
define("B_QUEEN", -50);
define("B_KING", -100);
$board = array(
array("", 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'),
array( 1, B_ROOK, B_KNIGHT, B_BISHOP, B_QUEEN, B_KING, B_BISHOP, B_KNIGHT, B_ROOK),
array(2, B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN),
array(3, 0, 0, 0, 0, 0, 0, 0, 0),
array(4, 0, 0, 0, 0, 0, 0, 0, 0),
array(5, 0, 0, 0, 0, 0, 0, 0, 0),
array(6, 0, 0, 0, 0, 0, 0, 0, 0),
array(7, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN),
array(8, ROOK, KNIGHT, BISHOP, QUEEN, KING, BISHOP, KNIGHT, ROOK)
);
function checkMove($query) {
global $board;
if(strlen($query) != 4) {
return "Wrong query!";
}
//...
// Next modfy the $board positions according to rules
}
function printBoard() {
// ...
}
http 是一种无状态协议,这意味着脚本将针对每个请求重新运行。发布表单会创建一个新请求。
您将不得不以某种方式保持游戏的状态。 $_SESSION
是个好主意,正如巴马尔所建议的那样。
编辑:既然你发布了你的板引擎,只是为了开始,请执行以下操作:
1( 在代码开头添加session_start();
2( 将$board=....
部分替换为">
if(!isset($_SESSION['board']))
$_SESSION['board']=.......
3( 将代码中出现的每个$board
替换为$_SESSION['board']
> BoardEngine.php
看起来如何?
为什么不把它变成一个类呢?像这样:
class BoardEngine {
//You can remove the construct function below, if you don't need it
function __construct(argument)
{
//Constructor code here if needed
}
public function printBoard()
{
# function code here...
}
public function checkMove($var)
{
# function code here...
}
public function yetanotherone()
{
# function code here...
}
}
然后,您可以将所有"引擎"逻辑放在那里。
在你的board.php
:
<?php
require_once('BoardEngine.php');
$boardEngine = New BoardEngine();
if (isset($_GET['move'] )) {
$boardEngine->checkMove($_POST['queryMove']); // checkMove is from boardEngine.php
}
$boardEngine->printBoard(); // function from boardEngine.php
?>
<div id='board'>
</div>
<form id="moveForm" action="board.php?move" method="post" >
<input type="text" name="queryMove" placeholder="form: 'e2f3' (e2 to f3)" required> </p>
<input type="submit" value=">move!<" >
</form>
</body>
为了在每次重新加载时保存移动,我还建议使用 $_SESSION
.或者在BoardEngine
课上:
private $lastmove;
public function setLastMove($value)
{
$this->lastmove = $value;
}
public function getLastMove($value)
{
return $this->lastmove;
}
现在在您的板上.php您可以使用以下方法设置最后一步:
$boardEngine->setLastmove($var);
通过以下方法进行最后一步:
$boardEngine->getLastmove();
编辑:要编辑:要将最后一步另存为$_SESSION
并将其回显:
$_SESSION['lastmove'] = $boardEngine->getLastmove();
echo $_SESSION['lastmove'];
首先,不要说
board.php?move
在行动中。最好指定带有值的 get 变量。将其更改为board.php?move=yes
然后
if(isset($_GET['move'])) if($_GET['move']==="yes") include_once('boardEngine.php');