-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
198 lines (172 loc) · 5.59 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const GAME_STATE = {
FirstCardAwaits: 'FirstCardAwaits',
SecondCardAwaits: 'SecondCardAwaits',
CardsMatchFailed: 'CardsMatchFailed',
CardsMatched: 'CardsMatched',
GameFinished: 'GameFinished'
}
const Symbols = [
'https://assets-lighthouse.alphacamp.co/uploads/image/file/17989/__.png', // 黑桃
'https://assets-lighthouse.alphacamp.co/uploads/image/file/17992/heart.png', // 愛心
'https://assets-lighthouse.alphacamp.co/uploads/image/file/17991/diamonds.png', // 方塊
'https://assets-lighthouse.alphacamp.co/uploads/image/file/17988/__.png' // 梅花
]
const view = {
getCardsElement(index) {
return `<div data-index="${index}" class = "cards back">
</div>`
},
getCardsContent(index) {
const number = this.transformNumber((index % 13) + 1)
const symbol = Symbols[Math.floor(index / 13)]
return `
<p>${number}</p>
<img src=${symbol} alt="">
<p>${number}</p>`
},
transformNumber(number) {
switch (number) {
case 1:
return 'A'
case 11:
return 'J'
case 12:
return 'Q'
case 13:
return 'K'
default:
return number
}
},
displayCards(indexes) {//放indexes,讓程式碼用輸入值去呼叫
const rootElement = document.querySelector("#cards")
rootElement.innerHTML = indexes.map(index => this.getCardsElement(index)).join('')
},
//flipCard(card)
//flipCards(1,2,3,4,5)
//cards = [1,2,3,4,5]
flipCards(...cards) {//...可以變成陣列
cards.map(card => {
if (card.classList.contains('back')) {
// 回傳正面
card.classList.remove('back')
card.innerHTML = this.getCardsContent(Number(card.dataset.index)) // 暫時給定 10 Number(card.dataset.index
return
}
// 回傳背面
card.classList.add('back')
card.innerHTML = null
})
},
pairCards(...cards) {
cards.map(card => {
card.classList.add('paired')
})
},
renderScore(score){
document.querySelector('.score').textContent = `Score: ${score}`
},
renderTriedTimes(times){
document.querySelector('.tried').textContent = `You've tried: ${times} times`
},
appendWrongAnimation(...cards){//add something
cards.map(card => {
card.classList.add('wrong')
card.addEventListener('animationend', e => {
card.classList.remove('wrong')
}, {
once: true //加eventListener會增加效能,加once true,意思是只會觸發一次
})
})
},
showGameFinished() {
const div = document.createElement('div')
div.classList.add('completed')
div.innerHTML = `
<p>Complete!</p>
<p>Score: ${model.score}</p>
<p>You've tried: ${model.triedTimes} times</p>
`
const header = document.querySelector('#header')
header.before(div)
},
}
const model = {
revealedCards:[],
isRevealedCardsMatched() {
return this.revealedCards[0].dataset.index % 13 === this.revealedCards[1].dataset.index % 13
},
score: 0,
triedTimes: 0,
}
const controller = {
currentState:GAME_STATE.FirstCardAwaits,//狀態變翻第一張牌之前
generateCards () {
view.displayCards(utility.getRandomNumberArray(52))
},
//依不同的遊戲狀態,做不同的行為
dispatchCardAction(card) {
if(!card.classList.contains('back')) {
return
}
switch (this.currentState) {
case GAME_STATE.FirstCardAwaits :
view.flipCards(card)
model.revealedCards.push(card)
this.currentState = GAME_STATE.SecondCardAwaits
break //break會繼續執行程式碼,return是到那裡結束
case GAME_STATE.SecondCardAwaits:
view.flipCards(card)
model.revealedCards.push(card)
view.renderTriedTimes((++model.triedTimes))
if (model.isRevealedCardsMatched()){
//配對正確
view.renderScore((model.score += 10))
this.currentState = GAME_STATE.CardsMatched
view.pairCards(...model.revealedCards)
model.revealedCards = []
if (model.score === 260) {
console.log('showGameFinished')
this.currentState = GAME_STATE.GameFinished
view.showGameFinished() // 加在這裡
return
}
this.currentState = GAME_STATE.FirstCardAwaits
} else {
//配對失敗
view.appendWrongAnimation(...model.revealedCards)
this.currentState = GAME_STATE.CardsMatchFailed
setTimeout(this.resetCards,1000)//指過了1秒
//不能寫this.resetCard(),1000,這樣會失敗
}
break //break會繼續執行程式碼,return是到那裡結束
}
console.log(this.currentState)
console.log(model.revealedCards)
},
resetCards(){
view.flipCards(...model.revealedCards)
model.revealedCards = []
controller.currentState = GAME_STATE.FirstCardAwaits
}
}
const utility = {
getRandomNumberArray(count) {
const number = Array.from(Array(count).keys())
for (let index = number.length - 1; index > 0; index--) {
let randomIndex = Math.floor(Math.random() * (index + 1))
;[number[index], number[randomIndex]] = [number[randomIndex], number[index]]
}
return number
}
}
controller.generateCards()
// node list (array-like)
document.querySelectorAll('.cards').forEach(card => {
card.addEventListener('click', event => {
controller.dispatchCardAction(card)
})
})
// const arr2 = Array(10) //這樣宣告陣列會產生空陣列,但裡面有10個空格
// XXX.map()指的是把XXX裡的東西一個個拿出來
// XXX.join()是指把XXX裡的東西,假如是陣列,就把內容直接全部轉成文字