Repository navigation
Expand file tree
/
Copy pathmatchgame.js
More file actions
188 lines (147 loc) · 5.24 KB
/
Copy pathmatchgame.js
File metadata and controls
188 lines (147 loc) · 5.24 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
$(function(){
var answers = [{
"text": "Apple",
"order": "1",
"img":"apple1.png"
}, {
"text": "Banana",
"order": "2",
"img":"banana1.png"
}, {
"text": "Orange",
"order": "3",
"img":"orange1.png"
}, {
"text": "potato",
"order": "4",
"img":"potato1.jpg"
}, {
"text": "tomato",
"order": "5",
"img":"tomato1.jpg"
}, {
"text": "onion",
"order": "6",
"img":"onion1.jpg"
}, {
"text": "lemon",
"order": "7",
"img":"lemon1.png"
}, {
"text": "carrot",
"order": "8",
"img":"carrot.jpeg"
}, {
"text": "cabage",
"order": "9",
"img":"cabage1.jpg"
}, {
"text": "cucumber",
"order": "10",
"img":"cucumber.jpeg"
}];
var subcontainers = [{
"text": "fruits",
"id": "fruits"
},
{
"text": "vegetables",
"id": "vegetables"
},
{
"text": "salads",
"id": "salads"
},
];
var car_correct = new Array('answer1','answer2','answer3');
var vegetables_correct = new Array('answer4','answer5','answer6');
var train_correct = new Array('answer7','answer8','answer9','answer10');
reset_game(); // this initializes the game
$('#game_container #button_container #reset_button').click(function(){
reset_game();
});
$('#game_container #button_container #check_button').click(function(){
$("#game_container .qanswer").promise().done(function() { // promise().done() waits for any animations to complete before firing the function
score_game(); // this is necessary because any divs that have not yet finished the "drop" animation will not be scored
});
});
$('#game_container #ok_button').click(function(){
$('#game_container #message').animate( {
width: '0',
height: '0',
padding: '0',
opacity: 0
}, 1000).hide(1000);
});
function reset_game() {
$('#game_container #draggable_container').html('').removeClass();
$('#game_container #droppable_container').html('');
$('#game_container #check_button').removeAttr('disabled');
$('#game_container #message').hide();
$('#game_container #score_container').hide();
for (var j=0; j<3; j++) {
$('<div><strong>' + subcontainers[j].text + '</strong></div>').attr('class', 'subcontainer').attr('id', subcontainers[j].id).appendTo('#game_container #droppable_container').sortable({
containment: "#game_container",
cursor: "move",
items: "div",
revert: 250,
connectWith: "#game_container .subcontainer",
receive: function(event, ui) {
if (ui.item.parents('#game_container .subcontainer')) {
ui.item.removeClass('dragthis').addClass('dropped');
} else {
ui.item.removeClass('dropped').addClass('dragthis');
}
}
}).disableSelection();
}
answers.sort(function(){ return (Math.round(Math.random())-0.5); });
for (var i=0; i<answers.length; i++) {
$('<div style="overflow:hidden;">' + answers[i].text + '<br><img src ="imgs/'+answers[i].img+'" width=200px></img></div>').attr('id', 'answer' + answers[i].order).attr('class', 'dragthis qanswer').appendTo('#game_container #draggable_container').disableSelection();
}
$("#game_container #draggable_container").sortable({
connectWith: '#game_container .subcontainer',
containment: '#game_container',
cursor: 'move',
items: 'div',
revert: 250,
}).disableSelection();
}
function score_game() {
if (!$("#game_container #draggable_container").is(":empty")) {
$('#game_container #message #text').html('The game is not complete! Please drag all answers to a category first.');
$('#game_container #message').show().css({
top: $("#game_container #droppable_container").position().top-50,
left: $("#game_container #droppable_container").position().left+100
}).animate( {
width: '450px',
height: '80px',
padding: '20px',
opacity: 1
}, 500);
return;
}
$("#game_container .subcontainer").each(function(index){
$(this).sortable("option","disabled",true);
});
$('#game_container #button_container #check_button').attr("disabled", "disabled");
$correctcounter = 0; // keep track of how many are right
$("#game_container .dropped").each(function(index){
$thisid = $(this).attr('id'); // shortcuts
$parentid = $(this).parent().attr('id');
$(this).css('cursor','default'); // UI helper to help the user know the elements are no longer draggable
if ( // big long if statement to see if the element is in the right place
($.inArray($thisid, car_correct) > -1 && $parentid == 'fruits') ||
($.inArray($thisid, vegetables_correct) > -1 && $parentid == 'vegetables') ||
($.inArray($thisid, train_correct) > -1 && $parentid == 'salads')
) {
$(this).addClass('correct', 800).removeClass('dropped', 800); // it's in the right place - make it all green and happy
$correctcounter++; // +1 to the counter of correct answers
} else {
$(this).addClass('incorrect', 800).removeClass('dropped', 800); // it's in the wrong place - make it all red and sad
}
});
$('#game_container #score_container #score_text').html('You got <span class="score">' + $correctcounter + '</span> out of 10 correct!');
$('#game_container #score_container').slideDown(500);
}
})