-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
136 lines (116 loc) · 3.35 KB
/
game.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
var interval;
var started = false;
var time = 0;
var arrayOfClicks = [];
var ready = true;
var numCompleted = 0;
var words = ['hat','sat','rat','that','mat','fat','bat','cat','hat','sat','rat','that','mat','fat','bat','cat'];
function shuffle() {
var currentIndex = words.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = words[currentIndex];
words[currentIndex] = words[randomIndex];
words[randomIndex] = temporaryValue;
}
return words;
}
function wordsSetUp() {
shuffle();
var cells = document.getElementsByTagName("td");
for (var i = 0; i < cells.length; i++) {
var cell = cells[i];
cell.completed = false;
cell.clicked = false;
cell.value = words[i];
cell.addEventListener("mouseenter", function() {
if (this.completed == false && this.clicked == false)
this.style.background = "orange";
});
cell.addEventListener("mouseleave", function() {
if (this.completed == false && this.clicked == false)
this.style.background = "blue";
});
cell.addEventListener('click', function() {
if (this.clicked == false && this.completed == false) {
release(this);
}
});
}
}
function release(cell) {
cell.style.backgroundColor = "lavender";
cell.innerHTML = cell.value;
cell.clicked = true;
// Add the clicked cell to the array
arrayOfClicks.push(cell);
// Call the function to check for a match after two cells are clicked
if (arrayOfClicks.length === 2) {
checkForMatch();
}
}
function startTimer(){
if (started == false){
interval = setInterval(function(){
time++;
var myTime = document.querySelector("#timer");
myTime.innerHTML= "Time Elapsed: " + time;
},1000)
started = true;
}
}
function checkForMatch() {
// Check if the values of the two clicked cells match
if (arrayOfClicks.length == 2){
if (arrayOfClicks[0].value == arrayOfClicks[1].value){ //if the match found
complete(arrayOfClicks[0]);
complete(arrayOfClicks[1]);
arrayOfClicks = [];//reset
if (numCompleted== 16) { //4x4 matrix has 16 elements, game over
alert("You won in " + time + " seconds!");
clearInterval(interval);
}
}
else { //if the match is not found
ready = false;
var table = document.querySelector("#matrix");
table.style.border = "5px dashed red";
setTimeout(function(){
//after a 500ms delay
hide(arrayOfClicks[0]);
hide(arrayOfClicks[1]);
arrayOfClicks = [];
ready = true;
var table = document.querySelector("#matrix");
table.style.border = "5px solid black ";
},500);
}
}
}
function hide(cell){
cell.style.backgroundColor = "purple";
7
cell.innerHTML = "";
cell.clicked = false;
}
function complete(cell){
numCompleted++;
cell.completed = true;
cell.style.backgroundColor = "pink";
}
function start() {
shuffle();
wordsSetUp();
startTimer();
}
function restart() {
location.reload();
}
wordsSetUp();
startTimer(); // Call the startTimer function
shuffle();
var cells = document.getElementsByTagName("td");