I'm learning PHP and I'm programming a sinking fleet game with a single ship and a hundred squares. The problem I am having is that when I request the server the value of the session that stores the value of the box in which the ship is, it does not find it and I do not finish finding the solution to the problem no matter how hard I try to search for it . I have three files:
- index.php: It is from where the missile is "launched" to try to guess the square of the ship.
- trigger.php: This is where the grid is generated from and a box value is assigned to the ship, storing it within a session.
- control.php: It is from where the user's response is processed, the value of the ship's session is read and compared, returning the result.
Here are the codes for each:
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hundir La Flota</title>
</head>
<body>
<form action="./control.php" method="post">
¿Dónde vas a golpear? <br/><input type='text' name='misil'>
<input type='submit' value='Comprobar'>
</form>
</body>
</html>
trigger.php
<?php
$cuadriculas = 100;
$letras = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
function generarCuadricula($cuadriculas, $letras) {
global $posiciones;
$posiciones = array();
$numActual = 1;
$siguiente = 0;
while(count($posiciones) < $cuadriculas){
if($numActual == 11) {
$numActual = 1;
$siguiente += 1;
};
array_push($posiciones, $letras[$siguiente] . $numActual);
$numActual++;
};
};
function asignarCuadricula($posiciones) {
session_start();
$posBarco = $posiciones[rand(0, 99)];
$_SESSION['barco'] = $posBarco;
};
generarCuadricula($cuadriculas, $letras);
asignarCuadricula($posiciones);
?>
control.php
<?php
session_start();
$posBarco = $_SESSION["barco"];
$uHit = $_POST["misil"];
function comprobarSiGolpe($uHit){
echo "Has golpeado en: " . $uHit . "<br/>";
echo "Posición barco: " . $posBarco . "<br/>";
if($uHit == $_SESSION["barco"]){
echo "¡Enhorabuena! Has hundido el barco." . "<br/>";
} else {
echo "Has fallado, vuelve a intentarlo." . "<br/>";
echo '<a href="./index.php">Volver</a>';
};
};
comprobarSiGolpe($uHit);
?>
This is how it works for me (explanations below):
index.php
trigger.php
control.php
Explanations:
Changes in the index.php
where I start a session that will later be used in the trigger.php (it could also be put in the trigger.php, it doesn't matter in this case).
which will be parsed into the control.php for it.
Changes in the trigger.php
I have reordered some lines as I like to separate the functions of the algorithm, putting the functions first and then the script logic
I have put everything together in a single function, because as it is, it is not necessary to separate it into two. (unless you later need it for other things, in which case you separate it yourself).
Since trigger.php will always be called in the index.php, I check if it already has a value or not in $_SESSION["barco"] to regenerate or not the grid and the mapping
Changes in control.php
As before, I have reordered the script putting the functions first and then their logic
I check if the new Start again button has been given , and if it is, I kill the session completely with session_destroy() and return to index.php, where, in this case, when I include the trigger.php, it will recalculate the entire grid and the assignment will not have the $_SESSION['barco'] variable defined, starting the game again
Otherwise, we start the session, we validate if the data $_POST['missile'] is received, and if not, we stop the script because it indicates that this file has been opened directly, and we do not have to do anything.
You will see other minor changes without too much importance, but if you don't understand them ask.
You will tell us if it has worked and understood.