-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_backup.js
66 lines (62 loc) · 1.79 KB
/
main_backup.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
$(document).ready(function() {
var app = {
cards: [1,1,2,2,3,3,4,4,5,5,6,6],
init: function() {
app.shuffle()
},
shuffle: function() {
var random = 0
var temp = 0
for(var i = 0; i < app.cards.length; i++) {
random = Math.floor((Math.random() * 12))
temp = app.cards[i]
app.cards[i] = app.cards[random]
app.cards[random] = temp
}
app.assignCards()
console.log('Shuffled Card Array: ' + app.cards)
},
assignCards: function() {
$('.card').each(function(index) {
$(this).attr('data-card-value', app.cards[index])
})
app.clickHandlers()
},
clickHandlers: function() {
$('.card').on('click', function() {
$(this).html('<p>' + $(this).data('cardValue') + '</p>' ).addClass('selected')
app.checkMatch()
})
},
checkMatch: function() {
if ($('.selected').length == 2) {
if ($('.selected').first().data('cardValue') === $('.selected').last().data('cardValue')) {
// cards matched
// remove cards
$('.selected').each(function() {
$(this).animate({opacity: 0}).removeClass('unmatched')
})
$('.selected').each(function() {
$(this).removeClass('selected')
})
app.checkWin()
} else {
// cards didn't match
// flip cards back over
console.log('tester')
setTimeout(function() {
$('.selected').each(function() {
$(this).html(' ').removeClass('selected')
})
}, 1000)
}
}
},
checkWin: function() {
if ( $('.unmatched').length === 0 ) {
$('.container').html('<h1>You Won!</h1>')
}
}
}
app.init()
})