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
  • 1
    btw. why do you need an extra $_GET parameter? is ìf (isset($_POST['queryMove']))` not good enough? Commented Oct 10, 2013 at 16:29
  • Every time you load a PHP script, it starts fresh. All variables are reset, include files are reloaded, etc. The only thing that persists between runs are session variables. Commented Oct 10, 2013 at 16:29
  • bwoebi: the post method is fairly enough but doesn't solve my problem Commented Oct 10, 2013 at 16:37
  • barmar: yeah I want to solve that problem so I can get a web site working only with php. THen I will make it with javascript. Commented Oct 10, 2013 at 16:38

3 Answers 3

1

http is a stateless protocol, which means that the script will run all over again for each request. And posting a form creates a new request.

You're gonna have to persist your game's state somehow. $_SESSION is a good idea, as Barmar suggested too.

EDIT: Since you posted your board engine, and just to get started, do the following:

1) Add a session_start(); at the beginning of your code

2) Replace the $board=.... part with `

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

3) Replace every occurence of $board in your code with $_SESSION['board']

Sign up to request clarification or add additional context in comments.

1 Comment

THanks I am new to php and thought php files would not be loaded each times because of the include_once(). Using $_SESSION works perfectly :)
1

How does BoardEngine.php look?

Why not make it a Class? Something like this:

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...
}

}

You can then put all your "engine" logic in there.

And in your 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>

To save moves on each reload, I would also suggest using $_SESSION. Or in your BoardEngine class:

private $lastmove;

public function setLastMove($value)
{
    $this->lastmove = $value;
}
public function getLastMove($value)
{
    return $this->lastmove;
}

Now in your board.php you can set the last move with:

$boardEngine->setLastmove($var);

get it last move with:

$boardEngine->getLastmove();

EDIT: To clerify: To save last move as a $_SESSION and echo it out:

$_SESSION['lastmove'] = $boardEngine->getLastmove();
echo $_SESSION['lastmove'];

6 Comments

The thing is, 'action="board.php?move" ' will reload my page, and thus require boardEngine.php again and creating a boardEngine class anew, will it not? What can I do with $_SESSION? Saving the board position in it?
It will only instatiate the class. I updated my post how you could use the class to remember last move.
To use $_SESSION simply put $_SESSION = $yourvalue; Or if you need more $_SESSION variables, use it like so: $_SESSION['one'] = "one"; $_SESSION['two'] = "two"; And so on
@Bolli: Nice and tidy (I personally love OOP) but, alas, irrelevant. Doing it OOP-style doesn't persist anything.
@geomagas True true - but this way he could store the lastmove and use it again. I'm not gonna write the entire game though :) $_SESSION would be the way to go. I'm not expert on OOP (yet), I'm still learning.
|
0

Firstly, don't say

board.php?move in the action. It is better to specify get variables with values. Change it to board.php?move=yes

Then,

if(isset($_GET['move'])) if($_GET['move']==="yes")  include_once('boardEngine.php');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.