similar to UNO. The games works pretty good, at least according to the rules. But the rules somewhat break down whenever the you finish the first game and decide to play a second by not refreshing the browser, but rather by clicking
either the "quit" or "play again" link that pops up at the end of every game. i strongly feel that the reason is because there are some global variables(I used a great deal of them) which I haven't been able to locate, that are still hanging around the script
when each game concludes. I have made an effor to fish out all the ones I can find and explicitly delete them upon the end of each game round, but in a nearly 3000 line long script, there are bound to be some that would go unnoticed. So is there any javasript
function that can dump all the global variables in a script? Or alternatively, is there any function that deletes all global variables in the script? I have presented a skeleton of the script below, just in case there is something about the structure of the script itself that anyone can
noticed and that i have overlooked. Hopefully, the comments do a good job of explaining what is going on.
Code: Select all
$(document).ready(function(){
$("#start_game").click(function(){//Set things into motion when the start game link is clicked
var pattern = "^([0-9]|-|')+$";
var num_cards = $("#num_cards").val();
if( (!num_cards.match(pattern)) ||(num_cards > 13) ){ //If the current value doesn't match accepted pattern ie only numbers or exceeds 13
$("#start_game").hide();//Hide the start game button
$("#warning_number").show();//show red warning box
return;
}//End if clause
var game_number = 1;//Initialize a variable to indicate if this is the first or second or third... etc time we are playing
main()//Parse the main function, which is defined below.
function main(){
if(game_number == 1){//If this is the first game
$("#pre_game_dialog").hide();//Hide the form
$("#play_check").fadeIn(500);//Display the game playing area
$("body").css({'overflow':'hidden'});//Remove scroll bars
}//End if game number is 1
//All functions and variables that make the game possible, go here. When either side(computer or you)is done, a global variable called "gamed"
//is declared which can either take value "me"(in case you won) or "computer"(in case computer won). This variable is then use in the next function(game over) defined below to reset the game
function game_over(){
//Remove all elements from play area for example
$("#player2_container").children().remove()...etc
//Flush all active globals for example
delete window.p1card_to_play
delete window.card_on_board...etc
window.setTimeout(function(){
if(gamed == "me"){//Create a game over message as well as the quit and play again links on the fly
var game_over = $("<div style = 'margin:auto;width:500px;position:relative;top:30px;z-index:1000;'> <div style = 'position:relative;top:100px;color:blue;font-size:26px;font-weight:600;font-family:verdana;' align = 'center'> You Win. Congratulations! </div> <div style = 'margin:auto;width:220px;height:100px;position:relative;top:150px;'> <div id ='game_over_play_again' style = 'float:left;cursor:pointer;color:green;font-size:16px;font-weight:400;font-family:verdana;'>Play Again</div> <div id = 'game_over_quit' style = 'float:right;cursor:pointer;color:red;font-size:16px;font-weight:500;font-family:verdana;'>Quit</div> </div> </div>");
$("#play_check").prepend(game_over);
}//End if game is me
if(gamed == "computer"){//Create a game over message as well as the quit and play again links on the fly
var game_over = $("<div style = 'margin:auto;width:500px;position:relative;top:30px;z-index:1000;'> <div style = 'position:relative;top:100px;color:blue;font-size:26px;font-weight:600;font-family:verdana;' align = 'center'> Computer Wins! Sorry. </div> <div style = 'margin:auto;width:220px;height:100px;position:relative;top:150px;'> <div id = 'game_over_play_again' style = 'float:left;cursor:pointer;color:green;font-size:16px;font-weight:400;font-family:verdana;'>Play Again</div> <div id = 'game_over_quit' style = 'float:right;cursor:pointer;color:red;font-size:16px;font-weight:500;font-family:verdana;'>Quit</div> </div> </div>");
$("#play_check").prepend(game_over);
}//End if game is computer
$("#game_over_quit").click(function(){//Reset game and display form again if quit link is clicked
game_over.remove();
delete window.gamed;//Delete this last global
$("#play_check").hide();//Hide the play area
$("#pre_game_dialog").show();//Display the form
$("body").css({'overflow':'visible'});//Make scroll bars visible again
});//End game over quit click function
$("#game_over_play_again").click(function(){//If play again likn is clicked
game_number++//Increment this variable
game_over.remove();
delete window.gamed;
//Run the main function to play again without having to refill the form
main();
});//End play_again click function
},300);//End window set timeout
}//End of game over
}//End main
});//End start game click method
});//End document ready method