-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.html
111 lines (91 loc) · 3.83 KB
/
game.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<title>Memory Game</title>
</head>
<body>
<!-- Memory Game -->
<div x-data="game()"
class="px-10 flex items-center justify-center min-h-screen bg-gradient-to-br from-sky-500 to-indigo-500">
<h1 class="fixed top-0 right-0 p-10 font-bold text-3xl">
<span x-text="points"></span>
<span class="text-xs">pts</span>
</h1>
<div class="flex-1 grid grid-cols-4 gap-10">
<template x-for="(card, index) in cards" :key="index">
<div>
<button x-show="! card.cleared" :style="'background: ' + (card.flipped ? card.color : '#999')"
:disabled="flippedCards.length >= 2" class="w-full h-32 rounded" @click="flipCard(card)">
</button>
</div>
</template>
</div>
</div>
<!-- Flash Message -->
<div x-data="{ show: false, message: '' }" x-show.transition.opacity="show" x-text="message" @flash.window="
message = $event.detail.message;
show = true;
setTimeout(() => show = false, 1000)
" class="fixed bottom-0 right-0 bg-green-500 text-white p-2 mb-4 mr-4 rounded">
</div>
<script>
function pause(milliseconds = 1000) {
return new Promise(resolve => setTimeout(resolve, milliseconds));
}
function flash(message) {
window.dispatchEvent(new CustomEvent('flash', {
detail: { message }
}));
}
function game() {
return {
cards: [
{ color: 'green', flipped: false, cleared: false },
{ color: 'red', flipped: false, cleared: false },
{ color: 'blue', flipped: false, cleared: false },
{ color: 'yellow', flipped: false, cleared: false },
{ color: 'green', flipped: false, cleared: false },
{ color: 'red', flipped: false, cleared: false },
{ color: 'blue', flipped: false, cleared: false },
{ color: 'yellow', flipped: false, cleared: false },
].sort(() => Math.random() - .5),
get flippedCards() {
return this.cards.filter(card => card.flipped);
},
get clearedCards() {
return this.cards.filter(card => card.cleared);
},
get remainingCards() {
return this.cards.filter(card => !card.cleared);
},
get points() {
return this.clearedCards.length;
},
async flipCard(card) {
card.flipped = !card.flipped;
if (this.flippedCards.length !== 2) return;
if (this.hasMatch()) {
flash('You found a match!');
await pause();
this.flippedCards.forEach(card => card.cleared = true);
if (!this.remainingCards.length) {
alert('You Won!');
}
} else {
await pause();
}
this.flippedCards.forEach(card => card.flipped = false);
},
hasMatch() {
return this.flippedCards[0]['color'] === this.flippedCards[1]['color'];
}
};
}
</script>
</body>
</html>