javascriptjqueryhtmlajax

Javascript game confusion


Can someone help me to finish my game, please? It turns out I'm doing a game in JavaScript, HTML and AJAX; is "minefield". The only problem I have is that when I throw more than 1 request, rebuilds all my variables and by that, rebuilds another random field and everything returns to not make sense.

I want to create this game, and the algorithm is this:

When the user joins into the web, the user puts values in "X" and "Y". The code will put the position according the user coords. Then, the program will generate a random field, if the user position is red, the user WILL LOSE, in another case, if the position is green the user WILL WIN and if is GREEN, the user can continue playing and if is RED the user cannot continue playing. In the right position of the web page, there is a box that shows the times that the user survive.

The problem is: if the user wins the first time, he can continue gaming and when the user puts again another coords, the game generates another random field, and all of the variables of the another play are lose (i mean: variable that register the times that the user survive, the variables that generate the field and all of the variables). I want to conserve that variables. And if the user lose, the only option that the user has if he wants to play again is the way of refresh the page.


Solution

  • Cant you just disable/hide the button on click and re-enable/show it when ajax returns success?

    $(".play").on("click", function(){
       $(this).hide();
       $.ajax({
          ...
          ... Whatever ajax configs here
          ...,
          success:function(data){
               ...validate
               ...redraw map
               $(".play").show();
          }
       });
    });