Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/examples/Math_Largest_Number_Identification/js/lesson.js
blob: 769b7b473d698a27f6b9055db04f3cd4910cb62a (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
$(document).ready(function() {
	var k = Karma({
		audio: [{'name':'correct','file':'correct.ogg'},
			{'name':'incorrect','file':'incorrect.ogg'},
			{'name':'trigger', 'file':'trigger.ogg'}
		]});
		  
	k.ready(function(){


	//initialize the variables used and display initial value
	var selected_box = "not selected";
	var score_value = 0;
	var greatest;
	var imgrand=[];
	var flag, i ,j;
	var volValue = 1;     //value of volume 1  means volume on 0 means volume off
	var score_sign = 1;    //0 means the sign is negative and 1 means it is positive
	
	/*
	document.display.selectedBox.value = selected_box;
	document.display.droppedBox.value = "not dropped";
	document.display.score.value = score_value;
	*/
	document.getElementById("scoreDisplay").src = "assets/image/drag_images/"+score_value+".png";
	game();
	
	$('a#anchorInfo').click(function(){              //Show the info of game
		$('#gameInfo').toggle(5000);
	});
	
	$('a#exitNow').click(function(){                 //Restart The Game
		var confirmVal = confirm("Do you really want to restart the game.");
		if(confirmVal == true)
			location.reload(true);
	});
	$('a#volControl').click(function(){              //Show the info of game
		$('.imgVolume').toggle();
		if(volValue == 1)
			volValue = 0;
		else
			volValue = 1;
		if(volValue == 1){
			k.audio.trigger.play();
			alert("Volume On");
			
		}
		else{
			k.audio.trigger.play();
			alert("Volume is Off");
		}
	});
	
	

	function generate_random_no()	{                //generate random number
		var rand_no = Math.ceil(99*Math.random());
		return rand_no;
	}

	function sortNumber(a,b){                       //find the greatest number
		return a - b;
	}
	
	
	function getRadioCheckedValue(radio_name) {
		var oRadio = document.diffLevel.elements[radio_name];
		for(var i = 0; i < oRadio.length; i++) {
			if(oRadio[i].checked) {
				return oRadio[i].value;
			}

		}

		return '';
	}
	
	function displayNumbers(){
		for(i=0; i<4; i++){
			document.getElementById("imgdrag"+i+"").src = "assets/image/drag_images/"+imgrand[i]+".png";
	
		}
	}

		
	function game(){               //draws the necessary random numbers for the game 
	    
		//var selected_radio = getRadioCheckedValue("levelBtn");
		//alert(selected_radio);
		
		//generate random numbers w/o repitition
		imgrand[0]=generate_random_no();   //1 number generated, 3 different numbers to be generated
		for(i=1; i<4; i++){
			do{
				flag = 0;
				imgrand[i] = generate_random_no();
				for(j=0; j<i; j++){
					if(imgrand[i]===imgrand[j]){
						flag++;
					}
				}
			}while(flag != 0 );  //end of do while loop	
		}
		displayNumbers();   //display the random numbers in the respective boxes.
	}    //end of game()
		
		// Set up the dragable element.
		$('#feedback_images .drag_delegates').bind('dragstart', function(ev) {
	         if (!$(ev.target).hasClass('dragme'))
	        	 return true;
	         switch (ev.target.id) {
	                case 'imgdrag0':
	                	selected_box = imgrand[0];                //the box is selected
	                	break;
	                case 'imgdrag1':
	                	selected_box = imgrand[1];                //the box is selected
	                	break;
	                case 'imgdrag2':
	                	selected_box = imgrand[2];                //the box is selected
	                	break;
	                case 'imgdrag3':
	                	selected_box = imgrand[3];                //the box is selected
	                	break;
	         }
	         //document.display.selectedBox.value = selected_box;
	         
	         return true;
	        });
		
	    // Set up the drop zone.
	    $('#drop_area .drophere').bind('dragenter', function(ev) {  // Update the drop zone class on drag enter/leave
	        if (!$(ev.target).hasClass('drophere')) return true;
	            $(ev.target).addClass('dragover');  return false;
	        })
		
	        .bind('dragleave', function(ev) {
	            if (!$(ev.target).hasClass('drophere')) return true;
	            $(ev.target).removeClass('dragover');   return false;
	        })
	
	        // Allow drops of any kind into the zone.
	        .bind('dragover', function(ev) {
	            if (!$(ev.target).hasClass('drophere')) return true;
	            return false;
	        })
	
	        // Handle the final drop...
	        .bind('drop', function(ev) {
	            if (!$(ev.target).hasClass('drophere')) return true;
	            
	            /** the box is dropped and now the calculation begins **/
	           // document.display.droppedBox.value = selected_box;
	            
	            //finding the greatest among the 4 random numbers
	            imgrand.sort(sortNumber);
	            greatest = imgrand[3];
		    console.log(greatest);
	            if(selected_box < greatest){
	            	score_value -=1;
	            	if(volValue==1)
	            	    k.audio.incorrect.play();
	            }
	            else{
	            	score_value +=1;
	            	if(volValue==1)
			    k.audio.correct.play();
	            }
	            
	            //NEgative number display technique
	            if(score_value<0)
	            	document.getElementById("minussign").style.display = 'block';
	            else
	            	document.getElementById("minussign").style.display = 'none';
	            
	            resultval = Math.abs(score_value);   //change the negative value to positive 
	            document.getElementById("scoreDisplay").src = "assets/image/drag_images/"+resultval+".png";
	            if(score_value == 99){
	               	alert("Congratulations!!! You have won the game. Press Ok to Continue...");
	            	var gameVal = confirm("Press Ok To restart or Cancel to stay in page.");
	            	if(gameVal == true){
	            		location.reload(true);
	            	score_value =0;
	            	}
	            }
	           // document.display.score.value = score_value;
	            game();	                      
	           
	            ev.stopPropagation();
	            return false;
	           });
	    
  
  });    	
});    //end of document.ready