
Código ActionScript :
function randi (a, b) { return (Math.floor(Math.random() * (b - a)) + Math.floor(a)); }; function sign (val) { var _loc1 = val; return (_loc1 == 0 ? (0) : (_loc1 / Math.abs(_loc1))); }; Array.prototype.swap = function (a, b) { var _loc1 = this; var _loc2 = _loc1[a]; _loc1[a] = _loc1[b]; _loc1[b] = _loc2; }; Array.prototype.shuffle = function () { var _loc2 = this; for (var _loc1 = 0; _loc1 < _loc2.length; ++_loc1) { _loc2.swap(_loc1, randi(0, _loc2.length)); } // end of for }; Array.prototype.random_subset = function (n) { var _loc3 = new Array(); var _loc2 = Array.getIndices(0, this.length - 1); _loc2.shuffle(); for (var _loc1 = 0; _loc1 < n; ++_loc1) { _loc3[_loc1] = this[_loc2[_loc1]]; } // end of for return (_loc3); }; Array.getIndices = function (a, b) { var _loc2 = a; var _loc3 = new Array(); for (var _loc1 = _loc2; _loc1 <= b; ++_loc1) { _loc3[_loc1 - _loc2] = _loc1; } // end of for return (_loc3); }; Array.get_letter_grid = function (m, n) { var _loc3 = new Array(); for (var _loc2 = 0; _loc2 < m; ++_loc2) { _loc3[_loc2] = new Array(); for (var _loc1 = 0; _loc1 < n; ++_loc1) { _loc3[_loc2][_loc1] = String.fromCharCode(randi(65, 91)); } // end of for } // end of for return (_loc3); }; Array.prototype.sortOn = function (p) { var _loc1 = p; Array.$sortprop = _loc1; var _loc2 = function (a, b) { var _loc1 = Array.$sortprop; return (b[_loc1] > a[_loc1]); }; this.sort(_loc2); delete Array.$sortprop; }; Array.prototype.return_copy = function () { var _loc2 = this; var _loc3 = new Array(); for (var _loc1 = 0; _loc1 < _loc2.length; ++_loc1) { _loc3[_loc1] = _loc2[_loc1]; } // end of for return (_loc3); }; String.prototype.return_reverse = function () { var _loc2 = this; var _loc3 = ""; for (var _loc1 = _loc2.length - 1; _loc1 >= 0; --_loc1) { _loc3 = _loc3 + _loc2.charAt(_loc1); } // end of for return (_loc3); }; Array.prototype.hasElement = function (e) { var _loc2 = this; var _loc3 = e; for (var _loc1 = 0; _loc1 < _loc2.length; ++_loc1) { if (_loc2[_loc1] == _loc3) { true; } // end if } // end of for return (false); }; Array.prototype.intersection = function (A) { var _loc2 = this; var _loc3 = A; var _array = new Array(); for (var _loc1 = 0; _loc1 < _loc2.length; ++_loc1) { if (_loc3.hasElement(_loc2[_loc3])) { _array.push(_loc2[_loc1]); } // end if } // end of for return (_array); }; String.prototype.searchReplace = function (s, r) { return (this.split(s).join(r)); }; WordSearch = function () { this.load_game_data(); }; WordSearch.prototype.time_limit = Number(time_limit_val) == 0 ? (Number.MAX_VALUE) : (Number(time_limit_val)); WordSearch.prototype.database_file = database_file_val; WordSearch.prototype.grid_rows = Number(grid_rows_val); WordSearch.prototype.grid_columns = Number(grid_columns_val); WordSearch.prototype.words_per_puzzle = Number(words_per_puzzle_val); WordSearch.prototype.word_selection_type = word_selection_type_val; WordSearch.prototype.drag_circle_color = drag_circle_color_rgb; WordSearch.prototype.game_over_circle_color = game_over_circle_color_rgb; WordSearch.prototype.correct_points = Number(correct_points_val); WordSearch.prototype.incorrect_points = Number(incorrect_points_val); WordSearch.prototype.special_sound_probability = Number(special_sound_probability_val); WordSearch.prototype.show_unfound = show_unfound_val; WordSearch.prototype.timeline = this; WordSearch.prototype.load_game_data = function () { var _loc2 = this; _loc2.words = _loc2.timeline._parent.words.split("|"); for (var _loc1 = 0; _loc1 < _loc2.words.length; ++_loc1) { _loc2.words[_loc1] = _loc2.words[_loc1].toUpperCase(); } // end of for _loc2.initialize(); }; WordSearch.prototype.initialize = function () { this.initialize_variables(); this.initialize_graphics(); }; WordSearch.prototype.initialize_variables = function () { var _loc1 = this; _loc1.mouse_down = false; _loc1.depth = 0; _loc1.score = 0; _loc1.start_time = getTimer(); _loc1.answer_key = new Array(); _loc1.letter_grid_width = _loc1.timeline.grid_area._width; _loc1.letter_grid_height = _loc1.timeline.grid_area._height; _loc1.word_bank = _loc1.words.random_subset(_loc1.words_per_puzzle); _loc1.word_bank.sortOn("length"); _loc1.create_letter_grid(); _loc1.embed_word_bank(); }; WordSearch.prototype.create_letter_grid = function () { var _loc3 = this; var initial_grid = Array.get_letter_grid(_loc3.grid_rows, _loc3.grid_columns); _loc3.letter_grid = new Array(); for (var _loc2 = 0; _loc2 < _loc3.grid_rows; ++_loc2) { _loc3.letter_grid[_loc2] = new Array(); for (var _loc1 = 0; _loc1 < _loc3.grid_columns; ++_loc1) { _loc3.letter_grid[_loc2][_loc1] = new Object(); _loc3.letter_grid[_loc2][_loc1].letter = initial_grid[_loc2][_loc1]; _loc3.letter_grid[_loc2][_loc1].word_bank_letter = false; _loc3.letter_grid[_loc2][_loc1].letter_found = false; _loc3.letter_grid[_loc2][_loc1].drag_selected = false; } // end of for } // end of for }; WordSearch.prototype.embed_word_bank = function () { var _loc3 = this; var _loc1 = _loc3.word_bank.return_copy(); for (var _loc2 = 0; _loc2 < _loc1.length; ++_loc2) { _loc1[_loc2] = _loc1[_loc2].searchReplace(" ", ""); } // end of for for (var _loc2 = 0; _loc2 < _loc1.length; ++_loc2) { var index_j = randi(0, _loc3.grid_rows); var index_k = randi(0, _loc3.grid_columns); var possible_directions = _loc3.get_possible_word_directions(_loc1[_loc2].length, index_j, index_k); var bool = _loc3.embed_word(_loc1[_loc2], index_j, index_k, possible_directions); if (!bool) { _loc1.push(_loc1[_loc2]); } // end if } // end of for }; WordSearch.prototype.get_possible_word_directions = function (word_length, index_j, index_k) { var _loc1 = word_length; var _loc3 = index_k; var _loc2 = new Array(); if (index_j + _loc1 < this.grid_rows && _loc3 + _loc1 < this.grid_columns) { _loc2.push("Top Left to Bottom Right"); } // end if if (index_j + _loc1 < this.grid_rows && _loc3 - _loc1 >= 0) { _loc2.push("Top Right to Bottom Left"); } // end if if (index_j - _loc1 >= 0 && _loc3 + _loc1 < this.grid_columns) { _loc2.push("Bottom Left to Top Right"); } // end if if (index_j - _loc1 >= 0 && _loc3 - _loc1 >= 0) { _loc2.push("Bottom Right to Top Left"); } // end if if (index_j + _loc1 < this.grid_rows) { _loc2.push("Top to Bottom"); } // end if if (index_j - _loc1 >= 0) { _loc2.push("Bottom to Top"); } // end if if (_loc3 + _loc1 < this.grid_columns) { _loc2.push("Left to Right"); } // end if if (_loc3 - _loc1 >= 0) { _loc2.push("Right to Left"); } // end if return (_loc2); }; WordSearch.prototype.embed_word = function (word, index_j, index_k, possible_directions) { var _loc3 = possible_directions; _loc3.shuffle(); if (this.answer_key.length < 2.000000E-001 * this.word_bank.length) { _loc3.sortOn("length"); } // end if for (var _loc1 = 0; _loc1 < _loc3.length; ++_loc1) { var _loc2 = this.calculate_slope_increments(_loc3[_loc1]); if (!this.word_overlaps(word, index_j, index_k, _loc2)) { this.put_letters_in(word, index_j, index_k, _loc2, _loc3[_loc1]); return (true); } // end if } // end of for return (false); }; WordSearch.prototype.put_letters_in = function (word, index_j, index_k, increments, direction) { var _loc3 = word; for (var _loc1 = 0; _loc1 < _loc3.length; ++_loc1) { var j = index_j + _loc1 * increments.j; var k = index_k + _loc1 * increments.k; var _loc2 = this.letter_grid[j][k]; _loc2.word_bank_letter = true; _loc2.letter = _loc3.charAt(_loc1); } // end of for var answer = new Object(); answer.start = {j: index_j, k: index_k}; answer.length = _loc3.length; answer.increments = increments; answer.direction = direction; answer.word = _loc3; this.answer_key.push(answer); }; WordSearch.prototype.word_overlaps = function (word, index_j, index_k, increments) { for (var _loc1 = 0; _loc1 < word.length; ++_loc1) { var j = index_j + _loc1 * increments.j; var _loc3 = index_k + _loc1 * increments.k; var _loc2 = this.letter_grid[j][_loc3]; if (_loc2.word_bank_letter && _loc2.letter != word.charAt(_loc1)) { return (true); } // end if } // end of for return (false); }; WordSearch.prototype.calculate_slope_increments = function (direction) { var _loc1 = direction; if (_loc1 == "Top Left to Bottom Right") { return ({j: 1, k: 1}); } else if (_loc1 == "Bottom Left to Top Right") { return ({j: -1, k: 1}); } else if (_loc1 == "Top Right to Bottom Left") { return ({j: 1, k: -1}); } else if (_loc1 == "Bottom Right to Top Left") { return ({j: -1, k: -1}); } else if (_loc1 == "Left to Right") { return ({j: 0, k: 1}); } else if (_loc1 == "Right to Left") { return ({j: 0, k: -1}); } else if (_loc1 == "Top to Bottom") { return ({j: 1, k: 0}); } else if (_loc1 == "Bottom to Top") { return ({j: -1, k: 0}); } // end else if }; WordSearch.prototype.initialize_graphics = function () { var _loc1 = this; _loc1.create_letter_grid_graphics(); _loc1.render_letter_grid(); _loc1.render_word_bank(); }; WordSearch.prototype.create_letter_grid_graphics = function () { var _loc1 = this; _loc1.letter_spacing_x = _loc1.timeline.grid_area._width / (_loc1.grid_columns + 1); _loc1.letter_spacing_y = _loc1.timeline.grid_area._height / (_loc1.grid_rows + 1); var j = 0; while (j < _loc1.grid_rows) { for (var _loc2 = 0; _loc2 < _loc1.grid_columns; ++_loc2) { _loc1.timeline.attachMovie("letter_textfield", "letter_" + j + "_" + _loc2, _loc1.depth++); var _loc3 = _loc1.timeline["letter_" + j + "_" + _loc2]; _loc3._x = _loc1.timeline.grid_area._x + _loc1.letter_spacing_x * (_loc2 + 1); _loc3._y = _loc1.timeline.grid_area._y + _loc1.letter_spacing_y * (j + 1); _loc3.field = _loc1.letter_grid[j][_loc2].letter; _loc3.index_j = j; _loc3.index_k = _loc2; } // end of for ++j; } // end while }; WordSearch.prototype.render_letter_grid = function () { for (var _loc3 = 0; _loc3 < this.grid_rows; ++_loc3) { for (var _loc1 = 0; _loc1 < this.grid_columns; ++_loc1) { var _loc2 = this.timeline["letter_" + _loc3 + "_" + _loc1]; _loc2.field = this.letter_grid[_loc3][_loc1].letter; if (this.letter_grid[_loc3][_loc1].drag_selected) { _loc2.gotoAndStop("Drag Selected"); continue; } // end if if (this.letter_grid[_loc3][_loc1].letter_found) { _loc2.gotoAndStop("Found Selected"); continue; } // end if _loc2.gotoAndStop("Default"); } // end of for } // end of for }; WordSearch.prototype.render_word_bank = function () { var _loc2 = this; _loc2.word_bank.sort(); var word_spacing_y = _loc2.timeline.word_bank_area._height / (_loc2.word_bank.length + 1); for (var _loc1 = 0; _loc1 < _loc2.word_bank.length; ++_loc1) { _loc2.timeline.attachMovie("word_bank_textfield", "word" + _loc1, _loc2.depth++); var _loc3 = _loc2.timeline["word" + _loc1]; _loc3._x = _loc2.timeline.word_bank_area._x; _loc3._y = _loc2.timeline.word_bank_area._y + (_loc1 + 1) * word_spacing_y; _loc3._width= 200; _loc3.field = _loc2.word_bank[_loc1]; } // end of for }; WordSearch.prototype.mouse_press = function () { var _loc1 = this; _loc1.correct_mouse_press = true; if (!_loc1.timeline.grid_area.hitTest(_root._xmouse, _root._ymouse, true)) { _loc1.correct_mouse_press = false; return; } // end if _loc1.mouse_down = true; _loc1.press_mc = _loc1.get_closest_letter(); if (_loc1.word_selection_type == "Circled") { ++_loc1.depth; _loc1.timeline.attachMovie("circle_selection", "circle" + _loc1.depth, _loc1.depth); _loc1.current_circle_selection = _loc1.timeline["circle" + _loc1.depth]; _loc1.current_circle_selection.from_mc = _loc1.press_mc; _loc1.current_circle_selection.to_mc = _loc1.press_mc; var _loc2 = new Color(_loc1.current_circle_selection); _loc2.setRGB(_loc1.drag_circle_color); } // end if }; WordSearch.prototype.get_closest_letter = function () { var _loc1 = this; var delta_x = Math.max(0, _loc1.timeline._xmouse - (_loc1.timeline.grid_area._x + _loc1.letter_spacing_x / 2)); var delta_y = Math.max(0, _loc1.timeline._ymouse - (_loc1.timeline.grid_area._y + _loc1.letter_spacing_y / 2)); var _loc3 = Math.min(_loc1.grid_rows - 1, Math.floor(delta_y / _loc1.letter_spacing_y)); var _loc2 = Math.min(_loc1.grid_columns - 1, Math.floor(delta_x / _loc1.letter_spacing_x)); return (_loc1.timeline["letter_" + _loc3 + "_" + _loc2]); }; WordSearch.prototype.mouse_release = function () { var _loc1 = this; if (!_loc1.correct_mouse_press) { return; } // end if _loc1.mouse_down = false; _loc1.release_mc = _loc1.get_closest_proper_letter(); _loc1.evaluate_guess(); _loc1.set_word_path_property(_loc1.press_mc, _loc1.release_mc, "drag_selected", false); _loc1.render_letter_grid(); _loc1.press_mc = undefined; _loc1.release_mc = undefined; }; WordSearch.prototype.action = function () { var _loc1 = this; if (_loc1.mouse_down) { _loc1.render_dragged_word(); } // end if _loc1.update_game_properties(); }; WordSearch.prototype.render_dragged_word = function () { var _loc1 = this; if (_loc1.word_selection_type == "Circled") { _loc1.render_circled_dragged_word(); return; } // end if _loc1.render_dots_dragged_word(); }; WordSearch.prototype.render_circled_dragged_word = function () { this.current_circle_selection.to_mc = this.get_closest_proper_letter(); }; WordSearch.prototype.render_dots_dragged_word = function () { var _loc1 = this; var _loc2 = _loc1.get_closest_proper_letter(); _loc1.set_word_path_property(_loc1.press_mc, _loc2, "drag_selected", true); _loc1.render_letter_grid(); _loc1.set_word_path_property(_loc1.press_mc, _loc2, "drag_selected", false); }; WordSearch.prototype.get_closest_proper_letter = function () { var closest_mc = this.get_closest_letter(); var j1 = this.press_mc.index_j; var _loc3 = this.press_mc.index_k; var j2 = closest_mc.index_j; var k2 = closest_mc.index_k; var _loc1; var _loc2; if (Math.abs((j1 - j2) / (_loc3 - k2)) == 1 || Math.abs(j1 - j2) == 0 || Math.abs(_loc3 - k2) == 0) { _loc1 = j2; _loc2 = k2; } else { var angle = Math.abs(Math.atan2(j2 - j1, k2 - _loc3) * 180 / 3.141593E+000); if (angle < 2.250000E+001 || angle > 1.575000E+002) { _loc1 = j1; _loc2 = k2; } else if (angle > 6.750000E+001 && angle < 1.125000E+002) { _loc1 = j2; _loc2 = _loc3; } else { _loc1 = Math.max(Math.abs(j2 - j1), Math.abs(k2 - _loc3)) * sign(j2 - j1) + j1; _loc2 = Math.max(Math.abs(j2 - j1), Math.abs(k2 - _loc3)) * sign(k2 - _loc3) + _loc3; } // end else if } // end else if _loc1 = Math.max(0, _loc1); _loc2 = Math.max(0, _loc2); _loc1 = Math.min(this.grid_rows - 1, _loc1); _loc2 = Math.min(this.grid_columns - 1, _loc2); return (this.timeline["letter_" + _loc1 + "_" + _loc2]); }; WordSearch.prototype.set_word_path_property = function (from_mc, to_mc, prop, val) { var inc_x = sign(to_mc.index_k - from_mc.index_k); var inc_y = sign(to_mc.index_j - from_mc.index_j); var dist_x = Math.abs(to_mc.index_k - from_mc.index_k); var dist_y = Math.abs(to_mc.index_j - from_mc.index_j); var dist = Math.max(dist_x, dist_y); for (var _loc1 = 0; _loc1 <= dist; ++_loc1) { var _loc3 = from_mc.index_j + _loc1 * inc_y; var _loc2 = from_mc.index_k + _loc1 * inc_x; this.letter_grid[_loc3][_loc2][prop] = val; } // end of for }; WordSearch.prototype.evaluate_guess = function () { var _loc1 = this; var _loc3 = _loc1.calculate_word_path(_loc1.press_mc, _loc1.release_mc); var _loc2 = _loc1.check_word_bank(_loc3); if (_loc2 != -1) { _loc1.word_found(_loc2); } else { _loc1.wrong_word_found(); } // end else if _loc1.check_for_win(); }; WordSearch.prototype.word_found = function (bank_index) { var _loc1 = this; _loc1.timeline["word" + bank_index].gotoAndStop(2); if (_loc1.word_selection_type == "Dots") { _loc1.set_word_path_property(_loc1.press_mc, _loc1.release_mc, "letter_found", true); _loc1.set_word_path_property(_loc1.press_mc, _loc1.release_mc, "drag_selected", false); } // end if _loc1.score = _loc1.score + _loc1.correct_points; if (Math.random() < _loc1.special_sound_probability) { _loc1.timeline.special_correct_sounds.gotoAndStop(1 + randi(1, _loc1.timeline.special_correct_sounds._totalframes)); _loc1.timeline.special_correct_sounds.gotoAndStop(1); return; } // end if _loc1.timeline.correct_sounds.gotoAndStop(1 + randi(1, _loc1.timeline.correct_sounds._totalframes)); _loc1.timeline.correct_sounds.gotoAndStop(1); }; WordSearch.prototype.wrong_word_found = function () { var _loc1 = this; if (_loc1.word_selection_type == "Dots") { _loc1.set_word_path_property(_loc1.press_mc, _loc1.release_mc, "drag_selected", false); } else { _loc1.current_circle_selection.removeMovieClip(); } // end else if _loc1.score = _loc1.score + _loc1.incorrect_points; if (Math.random() < _loc1.special_sound_probability) { _loc1.timeline.special_incorrect_sounds.gotoAndStop(1 + randi(1, _loc1.timeline.special_incorrect_sounds._totalframes)); _loc1.timeline.special_incorrect_sounds.gotoAndStop(1); return; } // end if _loc1.timeline.incorrect_sounds.gotoAndStop(1 + randi(1, _loc1.timeline.incorrect_sounds._totalframes)); _loc1.timeline.incorrect_sounds.gotoAndStop(1); }; WordSearch.prototype.calculate_word_path = function (from_mc, to_mc) { var inc_x = sign(to_mc.index_k - from_mc.index_k); var inc_y = sign(to_mc.index_j - from_mc.index_j); var dist_x = Math.abs(to_mc.index_k - from_mc.index_k); var dist_y = Math.abs(to_mc.index_j - from_mc.index_j); var dist = Math.max(dist_x, dist_y); var word_str = ""; for (var _loc1 = 0; _loc1 <= dist; ++_loc1) { var _loc3 = from_mc.index_j + _loc1 * inc_y; var _loc2 = from_mc.index_k + _loc1 * inc_x; word_str = word_str + this.letter_grid[_loc3][_loc2].letter; } // end of for return (word_str); }; WordSearch.prototype.check_word_bank = function (word) { var _loc3 = word; for (var _loc2 = 0; _loc2 < this.word_bank.length; ++_loc2) { var _loc1 = this.timeline["word" + _loc2]; if ((_loc1.field == _loc3 || _loc1.field == _loc3.return_reverse()) && _loc1._currentframe == 1) { return (_loc2); } // end if if ((_loc1.field.searchReplace(" ", "") == _loc3 || _loc1.field.searchReplace(" ", "") == _loc3.return_reverse()) && _loc1._currentframe == 1) { return (_loc2); } // end if } // end of for return (-1); }; WordSearch.prototype.check_for_win = function () { var _loc3 = this; for (var _loc1 = 0; _loc1 < _loc3.word_bank.length; ++_loc1) { var _loc2 = _loc3.timeline["word" + _loc1]; if (_loc2._currentframe == 1) { return; } // end if } // end of for _root.gotoAndStop("Win"); }; WordSearch.prototype.check_for_lose = function () { }; WordSearch.prototype.update_game_properties = function () { var _loc1 = this; _loc1.timeline.score = _loc1.score; _root.score = _loc1.score; var _loc3 = Math.floor((_loc1.time_limit - (getTimer() - _loc1.start_time)) / 1000); if (_loc3 < 0) { _loc3 = 0; } // end if var min = Math.floor(_loc3 / 60); var _loc2 = _loc3 % 60; if (_loc2 <= 9) { _loc2 = "0" + _loc2; } // end if _loc1.timeline.timer = min + ":" + _loc2; if (_loc1.timeline.timer == "0:00") { if (_loc1.show_unfound == "False") { _root.gotoAndStop("Lose"); return; } // end if _loc1.show_missed_words(); } // end if }; WordSearch.prototype.show_missed_words = function () { var _loc1 = this; var j = 0; while (j < _loc1.answer_key.length) { var _loc2 = _loc1.answer_key[j]; var found = false; for (var _loc3 = 0; _loc3 < _loc1.word_bank.length; ++_loc3) { var word_mc = _loc1.timeline["word" + _loc3]; if (word_mc._currentframe != 1 && word_mc.field == _loc2.word) { found = true; } // end if } // end of for if (!found) { ++_loc1.depth; _loc1.timeline.attachMovie("circle_selection", "end_selection" + j, _loc1.depth++); var _mc = _loc1.timeline["end_selection" + j]; var to_j = _loc2.start.j + (_loc2.length - 1) * _loc2.increments.j; var to_k = _loc2.start.k + (_loc2.length - 1) * _loc2.increments.k; _mc.from_mc = _loc1.timeline["letter_" + _loc2.start.j + "_" + _loc2.start.k]; _mc.to_mc = _loc1.timeline["letter_" + to_j + "_" + to_k]; var _color = new Color(_mc); _color.setRGB(_loc1.game_over_circle_color); } // end if ++j; } // end while var j = 0; while (j < _loc1.word_bank.length) { _loc1.timeline["word" + j].removeMovieClip(); ++j; } // end while _loc1.timeline.word_bank_area.gotoAndStop(2); for (var n in _loc1) { _loc1[n] = undefined; } // end of for...in }; this._x = 0; this._y = 0; game = new WordSearch();