Php Math Game [closed]

-1

I have this code that a programmer did in his day, This code does that through other php when selecting a game selects the one of mates. What I want to do is make two more games of subtraction and multiplication

<?php
session_start();
$host_name = "xxxx";
$database = 'xxxx';
$user_name = "xxxx";
$password = "xxxxx";
$conexion = mysqli_connect($host_name, $user_name, $password, $database) or die     ("No se puede conectar a la base de datos");
if(isset($_SESSION['session'][2])) {

$usuario = $_SESSION['session'][2];
$guardar = "si";

}else{

$_SESSION['noregistrado'] = 'Invitado'.time();
$usuario = $_SESSION['noregistrado'];
$guardar = "no";
}


if(isset($_GET['acabado'])){

$respuesta2 = new \stdClass();
$respuesta2->final = $_SESSION['puntuacion'];
$respuesta2->usuario = $usuario;
$comprobar = "SELECT * FROM 'USUARIOS' WHERE 'usuario'='$usuario'";
$comprobar_q = mysqli_query($conexion,$comprobar) or die ("FALLA LA CONSULTA DE COMPROBACION");
$num = mysqli_num_rows($comprobar_q);
$puntuacion = $_SESSION['puntuacion'];

if($num==0){
    $query = "INSERT INTO 'USUARIOS' VALUES ('$usuario','$puntuacion','SUMA 2 DIGITOS')";

}else{
    $query = "UPDATE 'USUARIOS' SET 'puntuacion1'='$puntuacion' WHERE 'usuario'='$usuario'";
}

mysqli_query($conexion,$query) OR DIE ("FALLA LA CONSULTA DE INSERTAR");

$posicion = "SELECT 'usuario', 'puntuacion1', rank
FROM
(
  SELECT 'usuario', 'puntuacion1', @n := IF(@g = puntuacion1, @n, @n + 1) rank, @g := puntuacion1
FROM 'USUARIOS', (SELECT @n := 0) i
ORDER BY 'puntuacion1' DESC) q WHERE 'puntuacion1' = '$puntuacion'";

$pos = mysqli_query($conexion,$posicion);

$posi = mysqli_fetch_assoc($pos);

$posic = $posi['rank'];

$respuesta2->posicion = $posic;
$respuesta2->guardado = $guardar;

if($guardar == 'no'){

    $borrar = "DELETE FROM 'USUARIOS' WHERE 'usuario'='$usuario'";
    mysqli_query($conexion,$borrar);
        }




echo json_encode($respuesta2);

}else {


if (isset($_GET['respuesta'])) {


        //TODO: comprobar tiempo
    $clires = $_GET['respuesta'];
        $respuesta = new \stdClass();

    if ($clires == $_SESSION['num1'] + $_SESSION['num2']) {

        $_SESSION['puntuacion']++;
        $respuesta->mal = 1;

    } else {

        $respuesta->mal = 0;
    }


    $resultado = $_SESSION['num1'] + $_SESSION['num2'];
    $respuesta->resultado = $resultado;

    $_SESSION['num1'] = rand(10, 99);
    $_SESSION['num2'] = rand(10, 99);

    $operacion = $_SESSION['num1'] . '+' . $_SESSION['num2'] . '=';

    $respuesta->operacion = $operacion;
    $respuesta->ok = 1;
    $respuesta->puntuacion = $_SESSION['puntuacion'];
    echo json_encode($respuesta);



} else {

    $_SESSION['puntuacion'] = 0;
    //$_SESSION['gameStart']= timestamp();
    $_SESSION['num1'] = rand(10, 99);
    $_SESSION['num2'] = rand(10, 99);
    $operacion = $_SESSION['num1'] . '+' . $_SESSION['num2'] . '=';
    $respuesta = new \stdClass();
    $respuesta->operacion = $operacion;
    $respuesta->ok = 1;
    echo json_encode($respuesta);


}


}

How can I make the correct operations when I select the subtraction game with id X?

    
asked by AitorUdabe 23.01.2018 в 20:37
source

1 answer

1

The choice of variable names (and values) is not readable, for example, $ answer-> bad = 1 when the answer is correct and $ answer-> bad = 0 when it is incorrect. Or maybe the missing code does not allow me to understand what the program does. What I understand is:

The first part, until

echo json_encode($respuesta2);

What the program does is obtain the player's score and ranking data. From there comes the game, and what it does is present a sum of two random two-digit integers. When the user enters the data, check the operation and if it is correct, add 1 to the score: $_SESSION['puntuacion']++;

What I do not see anywhere is that the user can select the game of addition, subtraction or anything. But anyway, you have two options: 1.- Each game is in a different html with different functions 2.- That there is a control where the user can choose the game and according to the value entered execute this code (of sums) or another (subtraction, multiplication)

In both cases you must write the functions that calculate subtraction and multiplication, which basically is to copy what you have there and change the + of: if ($clires == $_SESSION['num1'] + $_SESSION['num2']) { and $operacion = $_SESSION['num1'] . '+' . $_SESSION['num2'] . '='; by - and * Of course, if you only do that, there will be negative results when num1 is greater than num2.

Greetings

    
answered by 24.01.2018 в 11:55