-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprivate.js
More file actions
347 lines (252 loc) · 6.65 KB
/
private.js
File metadata and controls
347 lines (252 loc) · 6.65 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
======================================================
NOTE:
The code below serves only demonstration purposes.
It could be improved.
======================================================
*/
"use strict;"
/*
=====================
DEFINITION OF CLASSES
=====================
*/
//Purpose: Contains a list of Riddles and their answers
function Riddles()
{
//Private member variables
var riddle = [];
var answer = [];
//Sets content and its correct answer
this.Fill = function(content, solution)
{
//Answer and riddle should be on the same position when later getting both
riddle.push(content);
answer.push(solution);
}
//Get content from a specific position.
/*
Parameter:
fromPosition -> an integer number for the specific position which will be accessed
riddle_or_answer -> "answer" for the riddles answer or "riddle" for the riddle itself
*/
this.GetInfo = function(fromPosition, riddle_or_answer)
{
if ( (riddle[fromPosition] === undefined) )
{
console.log("Wrong array position!")
}
else if(riddle_or_answer === "riddle")
{
return riddle[fromPosition];
}
else if(riddle_or_answer === "answer")
{
return answer[fromPosition];
}
}
}
//Purpose: Class for left, middle and right cup which are represented in picture
function Cup()
{
//Private member variable
var hiddenObject = false;
//Variable "intoCup" must be a bool
this.putObject = function(intoCup)
{
hiddenObject = intoCup;
}
//Checks the player selected cup
this.hasObject = function()
{
return hiddenObject;
}
}
//Purpose: Contains all the game status information
function GameStats()
{
//Private member variables
var points = 0;
var rounds = 1;
//Increases or decreases points depending on positive or negative argument value
this.changePoints = function(addPoints)
{
points += addPoints;
}
//Needed to show points to player
this.getPoints = function()
{
return points;
}
//Will be increased with every new turn
this.increaseRounds = function()
{
rounds += 1;
}
//Will return the current turn in which the player is
this.currentRound = function()
{
return rounds;
}
}
//Purpose: Needed for shuffling and randomly showing a riddle
function Randomizer(min, max)
{
var lastResult = 0;
//[min,max] interval
this.start = function()
{
//To make sure that the interval between min and max is correct
var tempMax = max + 1;
//Storing the result
lastResult = ( Math.floor(Math.random() * (tempMax - min)) + min );
return lastResult;
}
//For reusability of the result
this.getLastResult = function()
{
return lastResult;
}
}
/*
=========
INSTANCES
=========
*/
var MyRiddles = new Riddles();
//Setting three cups equal to the number in the picture which the user is interacting with
var LeftCup = new Cup();
var MiddleCup = new Cup();
var RightCup = new Cup();
//Puts the "thrill" into the game (pseudo randomness)
var Shuffle = new Randomizer(0,2);
var Dice = new Randomizer(0,9);
//Gamestats are zero when initialized
var MyGameStats = new GameStats();
/*
======================
GAMEPLAY SUB FUNCTIONS
======================
*/
//Shows current round status
function showRounds()
{
var rounds = document.getElementById('round-number');
rounds.innerHTML = MyGameStats.currentRound();
}
//Shows current point status
function showPoints()
{
var points = document.getElementById('points');
points.innerHTML = MyGameStats.getPoints();
}
//Shows from the system a randomly selected riddle
function showRiddle()
{
var riddle = document.getElementById('riddle');
//Between 0 and 9
var diced = Dice.start();
riddle.innerHTML = MyRiddles.GetInfo(diced, "riddle");
}
/*
=======================
GAMEPLAY MAIN FUNCTIONS
=======================
*/
//Activates the first round of the game
function startRound()
{
var TrueCup = Shuffle.start();
if(TrueCup === 0) //Left Cup
{
LeftCup.putObject(true);
MiddleCup.putObject(false);
RightCup.putObject(false);
}
else if(TrueCup === 1) //Middle Cup
{
LeftCup.putObject(false);
MiddleCup.putObject(true);
RightCup.putObject(false);
}
else if(TrueCup === 2) //Right Cup
{
LeftCup.putObject(false);
MiddleCup.putObject(false);
RightCup.putObject(true);
}
//Show all the game information to the user
showRounds();
showPoints();
showRiddle();
}
//Checks if the user found the object
function choice(selected)
{
var userAnswer = document.getElementById('user-answer');
var show = document.getElementById('riddle');
if(Shuffle.getLastResult() === selected)
{
//Checking if the last round (round 5) is already reached
if(MyGameStats.currentRound() === 5)
{
show.innerHTML = "GAME OVER"
}
else
{
//Increasing points and round
MyGameStats.increaseRounds();
MyGameStats.changePoints(50);
//Activating the ability for the user to answer a new riddle
userAnswer.disabled = false;
//Object was found by user start next round
startRound();
}
}
else
{
//Object was not found, decreasing points
MyGameStats.changePoints(-15);
showPoints();
}
}
//Checks the user given answer
function checkAnswer()
{
var show = document.getElementById('riddle');
var deactivate = document.getElementsByName('cupmap')[0];
var userAnswer = document.getElementById('user-answer');
var answer = MyRiddles.GetInfo(Dice.getLastResult(), "answer");
if(answer === userAnswer.value)
{
//Bonus Points
MyGameStats.changePoints(30);
showPoints();
//Deactivating to prevent bonus points spaming
userAnswer.disabled = true;
show.innerHTML = "Congratulation, you answered the riddle and got bonus points!";
}
}
//Refresh Page = restart game
function refresh()
{
location.reload();
}
/*
=========
INIT GAME
=========
*/
//All riddles which will be used in the game
MyRiddles.Fill("A box without hinges, key or lid, Inside, a golden treasure is hid.", "egg");
MyRiddles.Fill("Forward I am heavy, backward I am not. What am I?", "ton");
MyRiddles.Fill("Give me food and I will live, Give me water, and I will die.", "fire");
MyRiddles.Fill("I look at you, you look at me I raise my right,you raise your left What is this object?", "mirror");
MyRiddles.Fill("If you divide thirty by half, and add ten, what do you get?", "70");
MyRiddles.Fill("The more that there is, the less you can see.", "darkness");
MyRiddles.Fill("What can you catch but not throw?", "cold");
MyRiddles.Fill("What does a rich man want, a poor man have, and a dead man eat?", "nothing");
MyRiddles.Fill("What kind of room has no windows or doors?", "mushroom");
MyRiddles.Fill("You must keep it after giving it.", "promise");
//First round
startRound()