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