Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/examples/lessons/6_Maths_romanNumerals/js/lesson.js
blob: 0baa99a51bd8e930b1437471064a8ed3334fa06f (plain)
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
$(document).ready(function() {
	var i,j,flag;
	var clickedObject;    //store the clicked image id
	var correctCounter = 0;
	var totalCounter = 0;
	var randQues = [];
	var currentQuestion;
	var TOTAL_QUES = 10;
	var flag_correct; 
	var sectionNum = 0; //store the current section of game 
	var firstClickObj;
	var flag_questionClick; //if set to 0, question is clicked so disable click in image
	var imgCurrent;
	var play = 0;
	var restart = 0;//pause the timer
	var s,h,m;
	var t;

	var questions1 = new Array(3,16,62,59,127,355,400,757,935,1205);
	var questions2 = new Array(10,2,150,50,500,1500,12,120,250,40);
	
	var startTimer = function(){
		s=checkTime(s);					
		m=checkTime(m);
		h=checkTime(h);
		$('#timerBox1').html(s);
		$('#timerBox2').html(m);
		$('#timerBox3').html(h);
	};
	
	var increaseTime = function(){
		if(play === 1){
			if(restart === 1){
				s = 0;
				m = 0;
				h = 0;
				restart = 0;
			}
			clearTimeout(t);
		s++;
		if(s>60){
		    m++;
		    m=checkTime(m);
		    $('#timerBox2').html(m);
		    s = 0;
		}
		if(m>60){
		    h++; 
		    h=checkTime(h);
		    $('#timerBox3').html(h);
		    m=0;
		    
		}				
		s=checkTime(s);	
		$('#timerBox1').html(s);
		var t=setTimeout(function(){increaseTime();},1000);
	
		}
	};
	
	function checkTime(timePara){
		if (timePara<10 ){
		timePara="0" + timePara;
		}
		return timePara;
	};

	var hide_textboxAnswers = function(){
		for(var i=0; i< TOTAL_QUES;i++){
			$('#checkFirst'+i).html('');
			$('#checkSecond'+i).html('');
			$('#ansBoxCorrect'+i).hide();
		}
	};
	
	function delay(){		
		document.delayForm.delayval.value = 1;
		hide_textboxAnswers();
	}
	

	var randNumber = function(limit){   //generate random number between any two ranges
			var rand_no = Math.floor(limit*Math.random());
			return rand_no;
		};

		var generate_random_questions = function(){
			randQues[0] = randNumber(TOTAL_QUES);
			for(i=1; i<TOTAL_QUES; i++){
				do{
					flag = 0;
					randQues[i] = randNumber(TOTAL_QUES);
					for(j=0; j<i; j++){
						if(randQues[i]===randQues[j]){
							flag++;
						}
					}
				}while(flag != 0 );  //end of do while loop	
			}
		};
		
	
		// Convert from Roman Numerals
		function deromanize( roman ) {
			  var roman = roman.toUpperCase(),
			      lookup = {I:1,V:5,X:10,L:50,C:100,D:500,M:1000},
			      arabic = 0,
			      i = roman.length;
			  while (i--) {
			    if ( lookup[roman[i]] < lookup[roman[i+1]] )
			      arabic -= lookup[roman[i]];
			    else
			      arabic += lookup[roman[i]];
			  }
			  return arabic;
			}
		
		//Convert to roman numerals
		function romanize(num) {
			  var lookup = {M:1000,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1},
			      roman = '',
			      i;
			  for ( i in lookup ) {
			    while ( num >= lookup[i] ) {
			      roman += i;
			      num -= lookup[i];
			    }
			  }
			  return roman;
			}
		
		                  
	
	var display_game_over = function(){
		$('#section').show();
		$('#section').addClass('gameOverShow');
		$('#gameOver').show();
		$('#gameOver').html('<img src="assets/image/gameOver.png');
		$('#gameOver').append('<div class="specialText">Click Play Again to play the game again or next/back to different game.</div>');
	};


	var check_answers = function(){
		var counter = 0;
		var ques;
		var flag_correct = 0;
		for(var i = 0; i< TOTAL_QUES;i++){
			if(sectionNum === 1){
				ques = questions1[i];
			}
			else{
				ques = questions2[i];
			}
			var x = $('#ansBox'+i).val();
			$('#checkFirst'+i).html('');
			$('#checkSecond'+i).html('');
			$('#ansBoxCorrect'+i).hide();
			if(ques === deromanize(x) && x!= ""){
				$('#checkFirst'+i).append('<img src = "assets/image/correct.png">');
				counter++;
				flag_correct = 1;
			}
			else{
				flag_correct = 0;
				$('#checkFirst'+i).append('<img src = "assets/image/incorrect.png">');
			}
			if(flag_correct === 0){
				$('#ansBoxCorrect'+i).show();
				$('#checkSecond'+i).html('<img src = "assets/image/correct.png">');
				$('input#ansBoxCorrect'+i).val(romanize(ques));
			}
		}
		if(counter != TOTAL_QUES){
			t=setTimeout(function(){delay();},3000);  //give chance to see for 3 sec if incorrect
		}
		else{
			play = 0;
			display_game_over();
		}
			
	};

	var assignQuestions = function (square){	 
		//var question = randQues[square];
		var ques;
		
		if(sectionNum === 1){
			ques = questions1[square];
		}
		else{
			ques = questions2[square];
		}
		
		$('#gameArea').append('<div id="imageArea'+square+'" class="imgArea"></div>');
		$('#imageArea'+square).append('<div id="ques'+square+'" class="quesBox"></div>');
		$('#ques'+square).html(ques);
		$('#imageArea'+square).append('<input id="ansBox'+square+'" type="text" class="textBox" maxlength="10" size="10">');
		$('#imageArea'+square).append('<div id="checkFirst'+square+'" class="check"></div>');
		$('#imageArea'+square).append('<input id="ansBoxCorrect'+square+'" type="text" style="display:none" class="textBox" maxlength="10" size="10">');		
		$('#imageArea'+square).append('<div id="checkSecond'+square+'" class="check"></div>');
	};

	function game(){

		$('#frontDisplay').show();
		$('#imgStory').hide();
		$('#topText').hide();
		$('#gameOver').hide();
		$('#section').hide();
		$('#linkBack').hide();
		$('#confirmBtn').hide();
		$('#linkNext').show();
		$('#timerBar').hide();
		sectionNum = 0;
	}
	function game_start(){
		clearTimeout(t);
		if(sectionNum === 1){
			$('#linkNext').show();
			$('#linkBack').show();
		}
		else{
			$('#linkNext').hide();
			$('#linkBack').show();
		}
		//alert(sectionNum);
		play = 1;
		s = 0; h = 0; m = 0;
		startTimer();
		increaseTime();
		$('#imgStory').show();
		$('#frontDisplay').hide();
		$('#gameOver').hide();
		$('#confirmBtn').show();
		$('#timerBar').show();
		$('#currentTitle').html('').append('तिम्रो सम्झने शक्ति को जाँच');
		$('#section').show();
		$('#section').removeClass('gameOverShow');
		$('#gameArea').html('').append('<div id="topText"></div>');
	
		for(var i = 0; i < TOTAL_QUES; i++){			
			assignQuestions(i);
		}	
		focus_blur();
	}
	$('#confirmBtn').click(function(){
		check_answers();
	});
	$('#linkStart').click(function(){
		sectionNum = 1;
		game_start();		
		
	});

	$('#linkPlayAgain').click(function(){
		restart = 0;
		game_start();	
		
	});
	$('#linkBack').click(function(){
		if(sectionNum === 1){
			game();		
		}
		else if(sectionNum === 2){
			sectionNum = 1;
			game_start();
		}
		
	});
	$('#linkNext').click(function(){
		sectionNum += 1;
		game_start();
		
	});
	function focus_blur(){
		$('input[type="text"]').focus(function() {
		    $(this).addClass("focus");
		});
		$('input[type="text"]').blur(function() {
		    $(this).removeClass("focus");    
		});
	}

	game();

	

});//end of DOM