0

I have one board.php file that displays a board on my web page. This file includes once a boardEngine.php file which has every variables and matrix initialized, plus every function needed for computing.

I put a form in my board.php so that I can enter my next move on the board. board.php code goes like this:

<!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>

The problem is that when I submit the move, board.php is reloaded with a set $_GET['move']. Since it is reloaded, it seems like boardEngine.php gets included again, and every positions in the matrix are initialized.

As I understand the thing so far, the move is submitted, board.php is reloaded, boardEngine.php is included another time with every position being reset, then because the $_GET['move'] variable has been set through the form, one move will be computed. Once a new move is submitted, the board will be reset and the last move will be considered, and so on.

Am I wrong? How can I solve this problem?

Edit 1: Here is the look of my boardEngine.php code:

 <?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() {
  // ...
}
4

3 回答 3

1

http 是一种无状态协议,这意味着脚本将针对每个请求重新运行。发布表单会创建一个新请求。

你将不得不以某种方式坚持你的游戏状态。$_SESSION正如 Barmar 所建议的那样,这是一个好主意。

编辑:由于您发布了您的板引擎,并且刚刚开始,请执行以下操作:

1)session_start();在代码的开头添加一个

$board=....2)用`替换部分

if(!isset($_SESSION['board']))
  $_SESSION['board']=.......

$board3)将代码中的每一个出现替换为$_SESSION['board']

于 2013-10-10T16:38:54.547 回答
1

看起来怎么样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;
}

现在在您的 board.php 中,您可以设置最后一步:

$boardEngine->setLastmove($var);

最后一步:

$boardEngine->getLastmove();

编辑:澄清:将最后一步保存为 a$_SESSION并回显:

$_SESSION['lastmove'] = $boardEngine->getLastmove();
echo $_SESSION['lastmove'];
于 2013-10-10T16:28:20.813 回答
0

首先,不要说

board.php?move在行动中。最好用值指定获取变量。将其更改为board.php?move=yes

然后,

if(isset($_GET['move'])) if($_GET['move']==="yes")  include_once('boardEngine.php');
于 2013-10-10T16:30:44.607 回答