summaryrefslogtreecommitdiff
path: root/game.js
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@clearboxsystems.com.au>2013-08-29 11:58:17 +1000
committerCarlo Zancanaro <carlo@clearboxsystems.com.au>2013-08-29 11:58:17 +1000
commit126feff14f65e4e3d0ca4f37055e50f0c8906e5e (patch)
treea13327c8f4ee65d97a105c0e4b41c5fd3ef4f84b /game.js
Initial commit - full "game"
Diffstat (limited to 'game.js')
-rw-r--r--game.js89
1 files changed, 89 insertions, 0 deletions
diff --git a/game.js b/game.js
new file mode 100644
index 0000000..9c7ec3e
--- /dev/null
+++ b/game.js
@@ -0,0 +1,89 @@
+(function($) {
+ $.fn.randomize = function(childElem) {
+ return this.each(function() {
+ var $this = $(this);
+ var elems = $this.children(childElem);
+
+ elems.each(function() {
+ $(this).data("sortIndex", Math.random());
+ });
+ elems.sort(function(a,b) { var as = $(a).data("sortIndex"); var bs = $(b).data("sortIndex"); return as < bs ? -1 : bs < as ? 1 : 0;});
+
+ $this.remove(childElem);
+
+ for(var i=0; i < elems.length; i++)
+ $this.append(elems[i]);
+
+ });
+ };
+
+ $.fn.fullclick = function(callback) {
+ var mousedown = false;
+ this.mousedown(function() {
+ mousedown = true;
+ });
+ this.mouseup(function() {
+ if (mousedown) {
+ callback.apply(this, arguments);
+ }
+ mousedown = false;
+ });
+ };
+})(jQuery);
+
+
+var startGame = (function () {
+
+ return function(source, target, message) {
+ var $message = $(message);
+
+ var $source = $(source);
+ var $target = $(target);
+ var $both = $source.add($target);
+ $source.randomize("li");
+
+ var areAllOver = function() {
+ return $source.find("li").size() == 0;
+ };
+
+ var areInOrder = function() {
+ var success = true;
+ $target.find("li").each(function(i, val) {
+ success = success && (i == $(val).data("index"));
+ });
+ return success;
+ };
+
+ var checkGameResult = function() {
+ if (areAllOver() && areInOrder()) {
+ $message.text("Well done!");
+ $target.addClass("success");
+ } else {
+ $message.text(null);
+ $target.removeClass("success");
+ }
+ };
+
+
+
+ $source.fullclick(function(event) {
+ if (event.target != $(source)[0]) {
+ $target.append(event.target);
+ checkGameResult();
+ }
+ });
+ $target.fullclick(function(event) {
+ if (event.target != $(target)[0]) {
+ $source.append(event.target);
+ checkGameResult();
+ }
+ });
+ $both.sortable({
+ connectWith: ".bible-game",
+ update: function (event) {
+ checkGameResult();
+ }
+ });
+ };
+
+})();