-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
235 lines (194 loc) · 6.18 KB
/
Copy pathmain.js
File metadata and controls
235 lines (194 loc) · 6.18 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//遊戲狀態
const GAME_START = {
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' // 梅花
]
// 0 - 12:黑桃 1 - 13
// 13 - 25:愛心 1 - 13
// 26 - 38:方塊 1 - 13
// 39 - 51:梅花 1 - 13
// Model-'View'-Controller 資料顯示
const view = {
//第一次進入牌局
getCardElement(index) {
return `
<div class="card back" data-index="${index}" >
</div>
`
},
getCardContent(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>
`
},
//轉換 A,J,Q,K
transformNumber(number) {
switch (number) {
case 1:
return 'A'
case 11:
return 'J'
case 12:
return 'Q'
case 13:
return 'K'
default:
return number
}
},
displayCards(indexs) {
const rootElement = document.querySelector('#cards')
rootElement.innerHTML = indexs.map(index => this.getCardElement(index)).join('')
},
//...會可以讓很多資料帶入變成一個array 如 cards = [1,2,3,4,5]
flipCards(...cards) {
cards.map(card => {
if (card.classList.contains('back')) {
//回傳正面
card.classList.remove('back')
card.innerHTML = this.getCardContent(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}`
},
renderTryTimes(times) {
document.querySelector('.tried').textContent = `You've tried: ${times} times`
},
appendWrongAnimation(...cards) {
cards.map(card => {
card.classList.add('wrong')
card.addEventListener('animationend', function OnanimationendCard(event) {
card.classList.remove('wrong')
},
{
once: true
} //一次性 觸發完就消失(節省瀏覽器效能)
)
})
},
//260分結束遊戲
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)
}
}
// 'Model'-View-Controller 資料集中管理
const model = {
revealedCards: [],//被翻開的卡
isRevealCardsMatched() {
return this.revealedCards[0].dataset.index % 13 === this.revealedCards[1].dataset.index % 13
},
score: 260,
triedTimes: 0,
}
// Model-View-'Controller' 資料控制/動作
const controller = {
currentState: GAME_START.FirstCardAwaits,
generateCards() {
view.displayCards(utility.getRandomNumberArray(52))
//亂數52張牌
},
//依照不同遊戲狀態,做不同行為
dispatchCardAction(card) {
if (!card.classList.contains('back')) {
return
}
switch (this.currentState) {
case GAME_START.FirstCardAwaits:
view.flipCards(card)
model.revealedCards.push(card)
this.currentState = GAME_START.SecondCardAwaits
break
case GAME_START.SecondCardAwaits:
view.renderTryTimes(model.triedTimes++)
view.flipCards(card)
model.revealedCards.push(card)
if (model.isRevealCardsMatched()) {
//配對正確
view.renderScore(model.score += 10)
this.currentState = GAME_START.CardsMatched
view.pairCards(...model.revealedCards)
// view.pairCards(model.revealedCards[0])
// view.pairCards(model.revealedCards[1])
model.revealedCards = []
if (model.score === 260) {
console.log('showGameFinished')
this.currentState = GAME_START.GameFinished
view.showGameFinished() // 加在這裡
return
}
this.currentState = GAME_START.FirstCardAwaits
}
else {
//配對失敗
this.currentState = GAME_START.CardsMatchFailed
view.appendWrongAnimation(...model.revealedCards)
setTimeout(this.resetCards, 1000)
//這裡要放function本身 而不是()他的結果
}
break
}
console.log('this.currentState', this.currentState)
console.log('revealedCards', model.revealedCards.map(card => card.dataset.index))
},
resetCards() {
view.flipCards(...model.revealedCards)
//...把model.revealedCards陣列東西都丟入
// view.flipCards(model.revealedCards[0])
// view.flipCards(model.revealedCards[1])
model.revealedCards = []
controller.currentState = GAME_START.FirstCardAwaits
},
}
//外掛函式庫
const utility = {
//52張牌亂數
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))
//index + 1 因為有52張牌
;[number[index], number[randomIndex]] = [number[randomIndex], number[index]]
}
return number
}
}
controller.generateCards()
//Node List (array-like) 為每一個.card上面都增加監聽器
document.querySelectorAll('.card').forEach(card => {
card.addEventListener('click', function onClickedCard(event) {
controller.dispatchCardAction(card)
})
})