Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/examples/6_English_matchingProductsAndWords/js/lesson.js
blob: 6d8de62224cf9742f0eaeac3fce39c1f913c8e16 (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
$(document).ready(function() {
	var i,j,flag;
	var s=0;	var m=0;	var h=0;   //varoiables for timer
	var clickedObjects = [];   //array storing the clicks of the two succesive clicks
	var clickedObject = 0;    //store the clicked image id
	var matchedObjects = [];//store the matched images
	var objrand = [];
	var actualObjects = [];
	var numClicked = 0;       // If click on image it is incremnted by 1
	var numMatched = 0;      //how many matched objects
	var currentObj; //store the current object clicked
	var play =0;    //not played yet pause
	var restart = 0;   //not restarted
	var clickCounter = 0;
	var NUM_OBJECTS = 24;  //total number of objects in the game
	var products_words = new Array('Cow','Hen','Wood','Bee','Tap','House','Sheep','Cloth','Lamp','Leather','Wheat','Sugar','Milk','Egg','Chair','Honey','Water','Brick','Wool','Shirt','Light','Shoes','Flour','Sweet');
	var section = $('#section');	


	var checkTime = function(timePara){
	    if (timePara<10 )
	    {
		timePara="0" + timePara;
	    }
	    return timePara;
	};

	
	var startTimer = function(){
				s=checkTime(s);					
				m=checkTime(m);
				h=checkTime(h);
				clickCounter = checkTime(clickCounter);
				document.getElementById('clickBox').innerHTML=clickCounter;
				document.getElementById('timerBox1').innerHTML=s;
				document.getElementById('timerBox2').innerHTML=m;
				document.getElementById('timerBox3').innerHTML=h;
				
	};
	
	var increaseTime = function(){
	    if(play == 1){
			if(restart == 1){
			s = 0;
			m = 0;
			h = 0;
			}
		s++;
		if(s>60){
		    m++;
		    m=checkTime(m);
		    document.getElementById('timerBox2').innerHTML=m;
		    s = 0;
		}
		if(m>60){
		    h++; 
		    h=checkTime(h);
		    document.getElementById('timerBox3').innerHTML=h;
		    
		    m=0;
		    
		}				
		s=checkTime(s);					
		
		document.getElementById('timerBox1').innerHTML=s;
		
		var t=setTimeout(function(){increaseTime();},1000);
	    }
	};

	

	

	var generate_random_no = function()	{                //generate random number
		var rand_no = Math.floor(NUM_OBJECTS*Math.random());
		return rand_no;
	};
	
	var generate_random_objects_no = function(){
		
		objrand[0]=generate_random_no();   
		for(i=1; i<NUM_OBJECTS; i++){
			do{
				flag = 0;
				objrand[i] = generate_random_no();
				for(j=0; j<i; j++){
					if(objrand[i]===objrand[j]){
						flag++;
					}
				}
			}while(flag != 0 );  //end of do while loop	
		}
		
	};
	//alert(objrand);
	

	var check_game_over = function(){
		if(numMatched === NUM_OBJECTS){   //show all
			play = 0;
			$('#section').html('');
			$('#section').append('<div id="gameOver">GAME OVER<br/>Congratulations!!!</div>');
			$('#section').append('<div id="gameOverInfo">You have completed the game in <span class="specialText">'+clickCounter+
					'</span> clicks within  <span class="specialText">'+h+'</span> hour  <span class="specialText">'+m+
					'</span> minutes and  <span class="specialText">'+s+'</span> seconds .</div>');
		}
	};
	var store_clicked_object = function(objectClicked){
		
		if(play === 1){			
		    clickedObject = objectClicked;
		    var randClick = objrand[clickedObject];
		    clickedObjects[numClicked] = randClick;
		    actualObjects[numClicked]=clickedObject;
		    numClicked++;
		    clickCounter++;
		    clickCounter = checkTime(clickCounter);  //for showing format as 1 for 01
		    $("#clickBox").html(clickCounter);
		    show_processed_image();
		    
		    return true;
		}
		else{
		    return false;
			    
		}
			
	};

	var process_object = function(){
		var matchedCondition = 0;  //not matched
		/*
		 alert('Clicked Objects:'+clickedObjects[0]+' & '+clickedObjects[1]+'\n'+
			 'Actual Positions:'+actualObjects[0]+' & '+actualObjects[1]+'\n'+
			 'Rand Objects: '+objrand);
		 */
		
		if(Math.abs(clickedObjects[0]-clickedObjects[1]) === 12 && (clickedObjects[0] != clickedObjects[1])){
			matchedCondition = 1;
		}		
		if(matchedCondition === 1){   //matches		
			//check for the * sign and if it was any one having that then show off all for 2 secs
			// future
			
			document.getElementById('object'+actualObjects[0]).innerHTML='';
			document.getElementById('object'+actualObjects[1]).innerHTML='';
			$("#object"+actualObjects[0]).addClass('matched');
			$("#object"+actualObjects[1]).addClass('matched');
			matchedObjects[numMatched] = actualObjects[0];
			numMatched++;
			matchedObjects[numMatched] = actualObjects[1];
			numMatched++;
			check_game_over();	
			numClicked = 0;			
		}
		else{
			$('#object'+actualObjects[0]).html('');
			$('#object'+actualObjects[1]).html('');
			$("#object"+actualObjects[0]).addClass('default');
			$("#object"+actualObjects[1]).addClass('default');
			numClicked = 0;			
		}
		
		
		};
	
	var delay = function(){	
		document.delayForm.delayval.value = 1;
		process_object();
		
	};
	
	var show_processed_image = function(){    //Show the click Image
	    var t;
	    currentObj = objrand[clickedObject];
	    if (numMatched !=0){   //some pairs has matched so be sure not to show them again
			var flag = 0;  //if matched already it is set to 1
			for(i = 0; i<numMatched; i++){
			    if(clickedObject === matchedObjects[i] ){
			    	flag = 1;
			    }
			}
		
			if(flag === 0){    //no matches found
				$('#object'+clickedObject).html('');
				$("#object"+clickedObject).addClass('default');
				$('#object'+clickedObject).html(products_words[currentObj]);
			    if(numClicked === 2){
			    	t=setTimeout(function(){delay();},1000);			
			    }
			}
			else{          // If click on image it is incremnted by 1
			      //how many matched      //matched already so don't show
				$('#object'+clickedObject).html('object'+actualObjects[0]);
			    numClicked = 0;
			}
	    }
		
	    else if(numClicked == 2){    //process the image after 2 successive clicks
	
	    	  $('#object'+clickedObject).html('');
		       $('#object'+clickedObject).html(products_words[currentObj]);
	    	t=setTimeout(function(){delay();},1000);
		
	    }
	    else{	
	       $('#object'+clickedObject).html('');
	       $('#object'+clickedObject).html(products_words[currentObj]);
	    }
	};

	var assignSquares = function (square){	    
	    section.append('<a href="#"></a>');
	    $('#section a:last-of-type').append('<div class="default" id="object'+square+'"></div>'); 	
	    $('#section a:last-of-type').click(function(){		    
		    store_clicked_object(square);
		});
	};
		
	
	function game(){
		numClicked = 0;
		numMatched = 0;
		clickCounter = 0;
		matchedObjects = [];
		s=0;	m=0;	h=0;   
		$('#section').html('');
		$('#timerBox1').html('');$('#timerBox2').html('');$('#timerBox3').html('');
		$('#clickBox').html('00');
		generate_random_objects_no();
		startTimer();
		var square;
		for(i=0; i<NUM_OBJECTS; i++){
		    assignSquares(i);
		}
		play = 1;
		increaseTime();		
	}
	$('#linkStart').click(function(){
		game();
	});

	$('#linkPlayAgain').click(function(){
		game();
		
	});
	$('#section').html('');
	$('#clickBox').html('00');
	//game();

});//end of DOM