Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var numbers = [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8];
- var lastClickedButtonNumberId = undefined;
- var lastClickedButtonNumber = undefined;
- var wait = false;
- var matches = 0;
- shuffle(numbers);
- $("button").click(function(event) {
- var turnable = $(this).attr("data-turnable");
- console.log(turnable);
- if (turnable == "true" && !wait && lastClickedButtonNumberId == undefined) {//first click`
- $(this).attr(''game2.js'
- data-turnable', 'false');
- $(this).text($(this).data("number")).css('background-color', 'orange');
- lastClickedButtonNumberId = this.id;
- lastClickedButtonNumber = $(this).data("number");
- } else if (turnable == "true" && !wait && lastClickedButtonNumberId != undefined && lastClickedButtonNumberId != this.id) {//second click
- $(this).attr('data-turnable', 'false');
- $(this).text($(this).data("number"));
- if ($(this).data('number') == lastClickedButtonNumber) {//if id matches
- console.log("its a match");
- $(this).css('background-color', 'green');
- $("#" + lastClickedButtonNumberId).css('background-color', 'green');
- lastClickedButtonNumberId = undefined;
- lastClickedButtonNumber = undefined;
- matches++;
- if (matches == 8) {
- $(".win-container").css('display', 'block');
- $("#6").hide();
- $("#7").hide();
- $("#10").hide();
- $("#11").hide();
- }
- } else {//if id does not match
- $(this).css('background-color', 'orange');
- console.log("it doesn't match");
- wait = true;
- var thisVar = $(this);
- setTimeout(function () {
- thisVar.attr('data-turnable', 'true').css('background-color', 'white').text("");
- $("#" + lastClickedButtonNumberId).attr('data-turnable', 'true').css('background', 'white').text("");
- lastClickedButtonNumberId = undefined;
- lastClickedButtonNumber = undefined;
- wait = false;
- }, 1000);
- }
- }
- });
- $("button").each(function(index, el) {
- $(this).attr('data-number', numbers[index]);
- });
- function shuffle(array) {
- var j, x, i;
- for (i = array.length - 1; i > 0; i--) {
- j = Math.floor(Math.random() * (i + 1));
- x = array[i];
- array[i] = array[j];
- array[j] = x;
- }
- return array;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement