-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
108 lines (83 loc) · 2.13 KB
/
game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
function generateLetters(){
var array = [];
var cols = Number($('.col').val()) || 4;
var rows = Number($('.row').val()) || 4;
if((cols*rows) % 2 === 0){
$('.oddGridError').hide();
var gridCellsCount = cols*rows;
var letter = 65 + Math.round((gridCellsCount/2)), i, g = 2;
while(g > 0){
for(i=65;i<letter;i++){
var _char = String.fromCharCode(i);
array.push(_char);
}
g--;
}
arr = shuffle(array);
generateGrid(arr, cols);
} else {
$('.oddGridError').show();
return false;
}
}
function generateGrid(arr, cols){
var output = '<ul>', i;
for ( i = 0; i < arr.length; i++) {
output += "<li onclick='clickCard(this);'><span class='layer'></span>";
output += "<span class='alphabet'>"+arr[i]+"</span>";
output += "</li>";
}
output += "</ul>";
var s = $('.gameWrapper').html(output);
s.find("ul").width(($('li').width()+20)*cols);
$("#container").html(s);
}
var clickCount = 0,
matchCount = 0,
cardCount = 0,
check1 = '',
check2 = '';
function clickCard(ele){
if(!$(ele).find('.layer').hasClass('flipped') && !$(ele).find('.layer').hasClass('.matched') && cardCount < 2){
clickCount++;
cardCount++;
$(ele).find('.layer').addClass('flipped');
if(cardCount === 1){
check1 = $(ele).find('.alphabet').text();
}else{
check2 = $(ele).find('.alphabet').text();
if(check1 === check2){
$('li').each(function(){
if($(this).find('.alphabet').text() === check2){
$(this).find('.layer').addClass('matched');
}
});
matchCount++;
} else{
setTimeout(function() {
$(".layer").not(".matched").show();
$(".layer").not(".matched").removeClass("flipped");
}, 1000);
}
cardCount = 0;
}
}
$('.clickCount').text(clickCount);
$('.matchCount').text(matchCount);
}
$(function(){
generateLetters();
});
function shuffle(array) {
var m = array.length, t, i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
}