forked from udacity/fend-project-memory-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
187 lines (165 loc) · 6.61 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Matching Game</title>
<meta name="description" content="">
<link rel="stylesheet prefetch" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">
<link rel="stylesheet prefetch" href="https://fonts.googleapis.com/css?family=Coda">
<link rel="stylesheet" href="css/app.css">
<link rel="stylesheet" href="css/modal.css">
</head>
<body>
<div class="container">
<header>
<h1>Matching Game</h1>
</header>
<section class="score-panel">
<ul class="stars">
</ul>
<span class="moves">3</span> Moves
<div class="assist">
<i class="fa fa-clock-o"></i>
<span class="timer">0</span>
<i class="fa fa-repeat restart"></i>
</div>
</section>
<ul class="deck">
</ul>
</div>
<div class="modal-shade hide">
<ul class="modal-container">
<object class="modal-icon" data="img/success.svg" type="image/svg+xml">
</object>
<h2 class="modal-title">Congratulations! You Won!</h2>
<section class="modal-msg">
<p>With <span class="final-moves">17</span> Moves and <span class="final-stars">1</span> Starts.</p>
<p>Woooooo!</p>
</section>
<h2 class="modal-title">Ranking list</h2>
<ul class="modal-rank-list">
</ul>
<button class="modal-ok">Play again!</button>
</div>
</div>
<script src="lib/jquery-3.2.1.min.js"></script>
<script src="js/Card.js"></script>
<script src="js/binder.js"></script>
<script src="js/store.js"></script>
<script src="js/app.js"></script>
<script>
$(function(){
const deck=$('.deck');
// create a App
const game=new app.App();
//set up deck's click event handler
deck.click(function(e){
const index=$(e.target).data('index');
if (typeof index==='number') {
game.cardsClickEventHandler(index);
}
});
//restart button's click event handler
$('.restart').click(function(){
start();
});
// ok button's click event handler
$('.modal-ok').click(function(){
start();
$('.modal-shade').addClass('hide');
});
start();
/**
* start function
*/
function start(){
bind();
// clear cards element
deck.empty();
// start app
game.restart();
bindAllCardToDom();
}
/**
* bind game's state
*/
function bind(){
// bind move counter
binder.simpleBind(game,'moveCounter',value=>{
$('.moves').text(value);
});
// bind grade
binder.simpleBind(game,'grade',value=>{
$('.stars').empty();
if (value<0) return;
for (let index = 0; index < value; index++) {
$('.stars').append('<li><i class="fa fa-star"></i></li>');
}
for (let index = 0; index < game.getMaxGrade()-value; index++) {
$('.stars').append('<li><i class="fa fa-star-o"></i></li>');
}
});
//bind success state
binder.simpleBind(game,'win',value=>{
if (value) {
setTimeout(() => {
$('.modal-shade').removeClass('hide');
$('.final-moves').text(game.moveCounter);
$('.final-stars').text(game.grade);
const list=store.getRank();
list.push({moveCounter:game.moveCounter,date:new Date().toLocaleString()});
list.sort((x,y)=> x.moveCounter>y.moveCounter );
store.setRank(list);
showRankList(list);
}, 1000);
}
});
//bind timer
binder.simpleBind(game,'timer',value=>{
$('.timer').text(value);
});
function showRankList(list){
for (const item of list) {
$('.modal-rank-list').append(`<li>${item.date} - ${item.moveCounter} moves </li>`);
}
}
}
/**
* bind all cards to dom
*/
function bindAllCardToDom(){
for (let index = 0; index < game.getCards().length; index++) {
(function(index){
const element = game.getCards()[index];
deck.append(`<li class="card" data-index="${index}" id="card${index}">
<i class="fa fa-${element.symbol}" data-index="${index}"></i>
</li>`);
//show state binding
binder.simpleBind(element,'show',value=>{
if (value){
$(`#card${index}`).addClass('open show');
} else {
$(`#card${index}`).removeClass('open show not-match');
}
});
//matched state binding
binder.simpleBind(element,'matched',value=>{
if (value){
$(`#card${index}`).removeClass('open show').addClass('match animated rubberBand');
setTimeout(() => {
$(`#card${index}`).removeClass('animated rubberBand');
}, 1500);
} else {
$(`#card${index}`).addClass('not-match animated jello');
setTimeout(() => {
$(`#card${index}`).removeClass('animated jello');
}, 1500);
}
});
})(index);
}
}
});
</script>
</body>
</html>