-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
203 lines (177 loc) · 6.11 KB
/
app.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
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
//Initiate app
var app = angular.module('memory', ['ngDialog']);
//Colors for Squares
var colors = [
'darkgreen', 'darkgreen', 'blue', 'blue', 'maroon', 'maroon',
'indigo', 'indigo', 'goldenrod', 'goldenrod', 'chartreuse', 'chartreuse',
'deeppink', 'deeppink', 'dimgray', 'dimgray', 'aqua', 'aqua',
'saddlebrown', 'saddlebrown'
];
//Main Controller
app.controller('MainCtrl', function($scope, ngDialog) {
/*
* TOGGLE SQUARE
* Responsible for flipping squares, detecting matches
* and applying score multipliers
*/
$scope.toggle = function(obj) {
if(!obj.flipped && !$scope.timerActive) {
var oldScore = $scope.score;
if($scope.selected === -1) {
obj.flipped = true;
$scope.selected = obj.id;
} else if($scope.squares[$scope.selected].color !== obj.color) {
$scope.squares[$scope.selected].multiplier = false;
$scope.squares[$scope.selected].flipped = false;
$scope.selected = -1;
if($scope.score > 0) {
$scope.score--;
$scope.scoreChanged = -1;
}
} else {
obj.flipped = true;
//Determine multiplier effect
var addedScore = 1;
if(obj.multiplier) {
addedScore += 2;
}
if($scope.squares[$scope.selected].multiplier) {
addedScore += 2;
}
$scope.selected = -1;
$scope.score += addedScore;
$scope.scoreChanged = addedScore;
//Check for end game
$scope.checkEndgame();
}
}
};
//Check for endgame conditions
$scope.checkEndgame = function() {
var endGame = true;
angular.forEach($scope.squares, function(val) {
if(!val.flipped) {
endGame = false;
}
});
if(endGame) {
if($scope.highScore < $scope.score) {
$scope.highScore = $scope.score;
}
ngDialog.open({
template: 'dialogTemplate',
className: 'ngdialog-theme-default',
scope: $scope
});
}
};
//Reset game after Endgame dialog closes
$scope.$on('ngDialog.closed', function () {
$scope.$apply($scope.reset());
});
//Initialize/Reset Game
$scope.reset = function() {
//Attributes
$scope.selected = -1; //Track selected color, default = -1
$scope.timerActive = false; //True if Peek enabled
$scope.scoreChanged = 0;
$scope.score = 0;
$scope.peekLock = false; //Peek button lock
//Fill squares array (random order)
shuffle(colors);
$scope.squares = [];
for(var i = 0; i < colors.length; i++) {
var square = {
'id': i, //Unique ID
'color': colors[i], //Color for memory
'flipped': false, //color shows if flipped==true
'multiplier': true //Score multiplier enabled
};
$scope.squares.push(square);
}
};
//Activate Peek
$scope.peek = function() {
if(!$scope.peekLock) {
$scope.timerActive = true;
var obj = document.getElementById('timerText');
startTimer(5000, obj, $scope.endPeek);
}
};
//End Peek
$scope.endPeek = function() {
$scope.$apply($scope.timerActive = false);
$scope.$apply($scope.peekLock = true);
};
//Start Game
$scope.reset();
$scope.highScore = 0; //Don't want this to reset
});
//Applied to squares, changes CSS on mouseevents
app.directive('hoverDirective', function () {
return {
link: function ($scope, element, attrs) {
var square = element.scope().square;
element.bind('mouseenter', function () {
if($scope.timerActive && !square.flipped) {
element.removeClass('peek');
element.css('background-color', square.color);
}
});
element.bind('mouseleave', function () {
/*
* Small hack added to this if-check, handles case where
* timer hits zero before mouseleave event occurs
*/
if(!square.flipped &&
((element.css('background-color') != '#151515') || $scope.timerActive)) {
//Keep this if-check
if($scope.timerActive)
element.addClass('peek');
if(square.flipped) {
element.css('background-color', square.color);
} else {
element.css('background-color', '#151515');
}
}
});
}
};
});
//Javascript timer
function startTimer(duration, display, callback) {
var start = Date.now(),
diff,
minutes,
seconds;
function timer() {
// get the number of seconds that have elapsed since
// startTimer() was called
diff = duration - ((Date.now() - start) | 0);
var temp = diff/1000;
display.textContent = (temp >= 0 ? temp.toFixed(1) : 0);
if (diff <= 0) {
//Execute callback() when finished
clearInterval(intervalId);
callback();
}
};
// we don't want to wait a full second before the timer starts
timer();
var intervalId = setInterval(timer, 1);
}
//Knuth Shuffle (helper function)
function shuffle(array) {
var currentIndex = array.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 = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}