-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemoryGameLwc.js
142 lines (135 loc) · 5.26 KB
/
memoryGameLwc.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { LightningElement } from 'lwc';
import {loadStyle} from 'lightning/platformResourceLoader'
import fontawesome from '@salesforce/resourceUrl/fontawesome'
export default class MemoryGameLwc extends LightningElement {
isLibLoaded = false
openedCards =[]
matchedCard=[]
totalTime='00:00'
moves=0
timerRef
showCongratulations = false
cards= [
{id:1, listClass:"card", type:'diamond', icon:'fa fa-diamond'},
{id:2, listClass:"card", type:'plane', icon:'fa fa-paper-plane-o'},
{id:3, listClass:"card", type:'anchor', icon:'fa fa-anchor'},
{id:4, listClass:"card", type:'bolt', icon:'fa fa-bolt'},
{id:5, listClass:"card", type:'cube', icon:'fa fa-cube'},
{id:6, listClass:"card", type:'anchor', icon:'fa fa-anchor'},
{id:7, listClass:"card", type:'leaf', icon:'fa fa-leaf'},
{id:8, listClass:"card", type:'bicycle', icon:'fa fa-bicycle'},
{id:9, listClass:"card", type:'diamond', icon:'fa fa-diamond'},
{id:10, listClass:"card", type:'bomb', icon:'fa fa-bomb'},
{id:11, listClass:"card", type:'leaf', icon:'fa fa-leaf'},
{id:12, listClass:"card", type:'bomb', icon:'fa fa-bomb'},
{id:13, listClass:"card", type:'bolt', icon:'fa fa-bolt'},
{id:14, listClass:"card", type:'bicycle', icon:'fa fa-bicycle'},
{id:15, listClass:"card", type:'plane', icon:'fa fa-paper-plane-o'},
{id:16, listClass:"card", type:'cube', icon:'fa fa-cube'},
]
get gameRating(){
let stars = this.moves<12 ? [1,2,3]:this.moves>=13 ? [1,2]:[1]
return this.matchedCard.length ===16 ? stars :[]
}
displayCard(event){
let currCard = event.target
currCard.classList.add("open", "show", "disabled")
this.openedCards = this.openedCards.concat(event.target)
const len = this.openedCards.length
if(len === 2){
this.moves = this.moves+1
if(this.moves === 1){
this.timer()
}
if(this.openedCards[0].type === this.openedCards[1].type){
this.matchedCard = this.matchedCard.concat(this.openedCards[0], this.openedCards[1])
this.matched()
} else {
this.unmatched()
}
}
}
matched(){
this.openedCards[0].classList.add("match", "disabled")
this.openedCards[1].classList.add("match", "disabled")
this.openedCards[0].classList.remove("show", "open")
this.openedCards[1].classList.remove("show", "open")
this.openedCards=[]
if(this.matchedCard.length === 16){
window.clearInterval(this.timerRef)
this.showCongratulations = true
}
}
unmatched(){
this.openedCards[0].classList.add("unmatched")
this.openedCards[1].classList.add("unmatched")
this.action('DISABLE')
setTimeout(()=>{
this.openedCards[0].classList.remove("show", "open", "unmatched")
this.openedCards[1].classList.remove("show", "open", "unmatched")
this.action('ENABLE')
this.openedCards=[]
},1100)
}
action(action){
let cards = this.template.querySelectorAll('.card')
Array.from(cards).forEach(item=>{
if(action === 'ENABLE'){
let isMatch = item.classList.contains('match')
if(!isMatch){
item.classList.remove('disabled')
}
}
if(action === 'DISABLE'){
item.classList.add('disabled')
}
})
}
timer(){
let startTime = new Date()
this.timerRef = setInterval(()=>{
let diff = new Date().getTime() - startTime.getTime()
let d = Math.floor(diff/1000)
const m = Math.floor(d % 3600 / 60);
const s = Math.floor(d % 3600 % 60);
const mDisplay = m>0 ? m+(m===1? "minute, ":" minutes, "):""
const sDisplay = s>0 ? s+(s===1? "second":" seconds"):""
this.totalTime = mDisplay + sDisplay
}, 1000)
}
shuffle(){
this.showCongratulations = false
this.openedCards =[]
this.matchedCard=[]
this.totalTime='00:00'
this.moves=0
window.clearInterval(this.timerRef)
let elem = this.template.querySelectorAll('.card')
Array.from(elem).forEach(item=>{
item.classList.remove("show", "open", "match", "disabled")
})
/***shuffling and swaping logic */
let array = [...this.cards]
let counter = array.length
while(counter>0){
let index = Math.floor(Math.random()*counter)
counter--
let temp = array[counter]
array[counter] = array[index]
array[index] = temp
}
this.cards = [...array]
}
renderedCallback(){
if(this.isLibLoaded){
return
} else {
loadStyle(this,fontawesome+'/fontawesome/css/font-awesome.min.css').then(()=>{
console.log("loaded successfully")
}).catch(error=>{
console.error(error)
})
this.isLibLoaded = true
}
}
}