-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame (3).html
107 lines (80 loc) · 2.6 KB
/
game (3).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
<!DOCTYPE html>
<html>
<head>
<title>Memory games</title>
<link href="styles.css" rel="stylesheet">
<script type="text/javascript">
var memory_values = [];
var memory_card_ids = [];
function shuffle(array) {
var counter = array.length, temp, index;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
index = Math.floor(Math.random() * counter);
// Decrease counter by 1
counter--;
// And swap the last element with it
temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
function flip(card, letter) {
if (card.innerHTML == '' && memory_values.length < 2) {
console.log(letter);
card.innerHTML = letter;
card.style.background = 'white';
if (memory_values.length == 0) {
// Remember letter
memory_values.push(letter);
memory_card_ids.push(card.id);
console.log(memory_values);
} else if (memory_values.length == 1) {
// Remember second letter
memory_values.push(letter);
memory_card_ids.push(card.id);
console.log(memory_values);
if (memory_values[0] == memory_values[1]) {
// Match found
console.log('match found');
// Reset
memory_values = [];
memory_card_ids = [];
} else {
// No match
console.log('no match, flip back');
function flipBack() {
var card_1 = document.getElementById(memory_card_ids[0]);
var card_2 = document.getElementById(memory_card_ids[1]);
card_1.style.background = '#ccc';
card_1.innerHTML = "";
card_2.style.background = '#ccc';
card_2.innerHTML = "";
// Reset
memory_values = [];
memory_card_ids = [];
}
setTimeout(flipBack, 1000);
}
}
}
}
function loadBoard() {
var output = '';
shuffle(cards);
for (var i = 0; i < cards.length; i++) {
output = output + '<div id="card_' + i + '" onclick="flip(this, \'' + cards[i] + '\')"></div>';
}
document.getElementById('board').innerHTML = output;
}
var cards = ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D'];
</script>
</head>
<body onload="loadBoard()">
<div id="board">
This game needs JavaScript enabled to work. Please enable it.
</div>
</body>
</html>