commit 360d31dac79247f2c093dcbddf275bc6467adfa1 Author: Russell Beattie Date: Thu Mar 7 22:25:52 2013 -0800 Initial commit diff --git a/.htacess b/.htacess new file mode 100644 index 0000000..7fb41c1 --- /dev/null +++ b/.htacess @@ -0,0 +1,8 @@ +AddType text/cache-manifest .manifest + +AddType audio/mpeg mp3 +AddType audio/mp4 m4a +AddType audio/ogg ogg +AddType audio/ogg oga +AddType audio/webm webma +AddType audio/wav wav diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1803a47 --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +The MIT License (MIT) +Copyright (c) 2013 Russell Beattie + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..b757b9c --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# [Flip.io Puzzle](http://flip.io) + +An HTML5 puzzle app, and puzzle file generator. MIT licensed. Code is running at http://flip.io + +Here's a quick rundown of the files: + +* app.js - Main logic for the HTML5 GUI +* batch.js - Node.js batch program which generates a new puzzle.js file containing 1000 puzzles. +* flip.manifest_ - unused, but ready to be used for offline Web App +* index.html - HTML5 page with templates using Underscore templating (ejs). +* main.css - style for GUI +* LICENSE - MIT license for project +* puzzles.js - generated JSON list of puzzles using batch +* fonts/ - offline cache of web fonts used in various formats (included for convenience) +* lib/ - directory containing third party libraries (included for convenience): backbone, lodash, jquery, normalize.css +* media/ - folder for sound files and images diff --git a/app.js b/app.js new file mode 100644 index 0000000..d9419b8 --- /dev/null +++ b/app.js @@ -0,0 +1,566 @@ +'use strict'; + +var App = {}; + +App.init = function(){ + + App.name = 'Flip.io'; + $('.title').text(App.name); + + App.touch = ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch; + + if(window['Audio']){ + var audio = new Audio(); + var ext = '.wav'; + if(audio.canPlayType('audio/mp3')){ + ext = '.mp3'; + } + App.sounds = {}; + + var ding = new Audio(); + ding.src = './media/ding' + ext; + ding.volume = 0.5; + + var sweep = new Audio(); + sweep.src = './media/sweep' + ext; + sweep.volume = 0.3; + + App.sounds['sweep'] = sweep; + App.sounds['ding'] = ding; + } + + $('body').on('touchmove', function(e){ + + if(e.target.style = 'clues'){ + + } else { + e.preventDefault(); + } + }); + + this.homeView = new App.HomeView(); + this.puzzleView = new App.PuzzleView(); + this.router = new App.Router(); + + Backbone.history.start(); + +} + +App.play = function(sound){ + if(App.sounds){ + var snd = App.sounds[sound]; + try{ + snd.pause(); + snd.currentTime = 0; + } catch (err){} + snd.play(); + + } +} + +App.addWin = function(puzNum){ + if(window['localStorage']){ + var wins = localStorage.setItem['wins']; + if(!wins){ + wins = []; + } + wins.push(puzNum); + localStorage.setItem['wins'] = wins; + } +} + +App.getWins = function(){ + if(window['localStorage']){ + var wins = localStorage.setItem['wins']; + if(!wins){ + wins = []; + } + return wins; + } else { + return []; + } +} + + +App.Router = Backbone.Router.extend({ + + currentView: null, + + routes: { + '': 'showHome', + ':number': 'showPuzzle', + }, + + clearCurrent: function(){ + if(this.currentView){ + this.currentView.$el.html(''); + this.currentView.remove(); + } + }, + + showHome: function() { + + $('.content').fadeOut(function(){ + + App.router.clearCurrent(); + + document.title = App.name; + $('.titlenum').text(''); + + App.homeView.render(); + + $('.content').html(App.homeView.$el); + + App.router.currentView = App.homeView; + + $('.puznum').focus(); + + $('.content').fadeIn(); + + }); + + + }, + + showPuzzle: function(puzNum) { + + $('.content').fadeOut(function(){ + + App.router.clearCurrent(); + + document.title = App.name + ' / Puzzle ' + puzNum; + $('.titlenum').text('#' + puzNum); + + App.puzzleView.render(puzNum); + + $('.content').html(App.puzzleView.$el); + + App.router.currentView = App.puzzleView; + + $('.content').fadeIn(); + + }); + + } + +}); + + +App.HomeView = Backbone.View.extend({ + + tagName: 'div', + className: 'home', + + events: function() { + + var events; + + if(App.touch){ + events = { + 'keypress .puznum': 'checkField', + 'update .puznum': 'checkField', + 'submit .puz-form': 'loadPuzzle', + 'touchend .go-btn': 'loadPuzzle', + 'touchend .random-btn': 'randomPuzzle' + } + } else { + events = { + 'keypress .puznum': 'checkField', + 'submit .puz-form': 'loadPuzzle', + 'click .go-btn': 'loadPuzzle', + 'click .random-btn': 'randomPuzzle' + + } + } + return events; + }, + + render: function() { + + this.delegateEvents(); + + var tpl = $('.homeTemplate').text(); + var html = _.template(tpl, {}); + + this.$el.html(html); + + $('.puznum').focus(); + + }, + + checkField: function(e){ + + var keyCode = e.keyCode || e.charCode; + + //console.log(keyCode); + + if(keyCode == 13 || keyCode == 8 || keyCode == 46 || keyCode == 92 || keyCode == 39 || keyCode == 37){ + return true; + } + + + if(keyCode < 48 || keyCode > 57){ + return false; + } + + var val = $('.puznum').val(); + + if(val.length >= 3){ + return false; + } + + + }, + + loadPuzzle: function(e){ + if(e){ + e.preventDefault(); + } + + var puzNum = parseInt($('.puznum').val()); + + if(isNaN(puzNum) || puzNum < 1 || puzNum > 1000){ + + $('.puznum').val(''); + + } else { + + App.router.navigate(puzNum+'', {trigger: true}); + } + + }, + + randomPuzzle: function(){ + + var puzNum = Math.floor( Math.random() * ( 1001) ); + + $('.puznum').val(puzNum); + + // App.router.navigate(puzNum + "", {trigger: true}); + + } + + +}); + + +App.PuzzleView = Backbone.View.extend({ + + tagName: 'div', + className: 'puzzle', + + secs: 0, + stop: false, + intervalID: null, + guessing: false, + count: 0, + total: 7, + puzNum: 0, + data: null, + + events: function(){ + + var events; + + if(App.touch){ + events = { + 'touchend .shuffle' : 'doShuffle', + 'touchend .hint' : 'doHint', + 'touchend .square' : 'doSquare', + 'touchend .reset': 'doReset', + 'touchend .restart': 'doRestart', + 'touchend .gohome': 'doHome', + 'touchend .next': 'doNext' + } + } else { + events = { + 'click .shuffle' : 'doShuffle', + 'click .hint' : 'doHint', + 'click .square' : 'doSquare', + 'click .reset': 'doReset', + 'click .restart': 'doRestart', + 'click .gohome': 'doHome', + 'click .next': 'doNext' + } + } + return events; + + }, + + render: function(puzNum) { + + this.delegateEvents(); + + this.guessing = false; + this.count = 0; + + this.puzNum = parseInt(puzNum); + + if(isNaN(puzNum) || puzNum < 1 || puzNum > 1000){ + App.router.navigate('', {trigger: true}); + } + + this.start = new Date(); + this.secs = 0; + + this.data = puzzles[puzNum]; + + var tpl = $('.puzzleTemplate').text(); + var html = _.template(tpl, this.data); + + this.$el.html(html); + + this.startTimer(); + + + }, + + doHome: function(e){ + + this.stopTimer(); + + App.router.navigate('', {trigger: true}); + + }, + + doNext: function(e){ + App.router.navigate((App.puzzleView.puzNum +1)+'', {trigger: true}); + }, + + doShuffle: function(e){ + shuffleItems('tr'); + shuffleItems('tbody'); + }, + + doHint: function(e){ + + var active = []; + + $('.clues li').each(function(index){ + + if($(this).data('answer') !== ''){ + active.push($(this)); + } + + }); + + var rand = Math.floor(Math.random() * active.length); + var $clue = active[rand]; + + var b = rot13($clue.data('answer')); + + b = b.substr(0,3); + + $('.square').each(function(index){ + + if($(this).data('guessed') !== 'true'){ + + var s = $(this).text(); + + if(b.indexOf(s) == 0){ + $clue.fadeOut('slow', function(){ + $clue.fadeIn(); + }); + $(this).fadeOut('slow', function(){ + $(this).fadeIn(); + }); + + } + + } + + }); + + }, + + doSquare: function(e){ + + if(App.puzzleView.guessing == false){ + var sq = e.target; + + $(sq).data('guessed','true'); + + $(sq).hide(); + + var a = $('.answertext').text(); + + a = a + $(sq).text(); + + $('.answertext').html(a); + + if(a.length > 9){ + App.puzzleView.doReset(e); + } else { + App.puzzleView.doGuess(a); + } + } + + }, + + doGuess: function(a){ + + if(a == ''){ + return; + } + + var $clues = $('.clues li'); + + App.puzzleView.guessing = true; + + for(var i = 0; i < $clues.length; i++){ + + var $clue = $clues.eq(i); + + var b = rot13($clue.data('answer')); + + if( a.toLowerCase() == b.toLowerCase()){ + + App.play('ding'); + + $clue.data('answer',''); + + $('.answertext').fadeOut('fast',function(){ + $('.answertext').html(''); + $('.answertext').show(); + }); + + $clue.find('.word').fadeOut(function(){ + $(this).html(b).addClass('correct'); + $(this).fadeIn(); + }); + + $clue.find('.def').fadeOut(function(){ + $(this).css({'text-decoration':'line-through','font-style':'italic'}); + $(this).fadeIn(); + }); + + $('.square').each(function(index){ + $(this).data('guessed',''); + }); + + App.puzzleView.count++; + + if(App.puzzleView.count == App.puzzleView.total){ + App.puzzleView.doWin(); + return; + } + + break; + + } + + } + + App.puzzleView.guessing = false; + + + }, + + doWin: function(){ + + App.puzzleView.stopTimer(); + + App.addWin(App.puzzleView.puzNum); + + App.play('sweep'); + + setTimeout(function() { + + $('.squares table').fadeOut(); + $('.hint').hide(); + $('.shuffle').hide(); + + var tpl = $('.winTemplate').text(); + var html = _.template(tpl, {}); + + $('.squares').html(html); + + }, 1000); + + }, + + doRestart: function(e){ + + App.puzzleView.secs = 0; + + App.puzzleView.render(App.puzzleView.puzNum); + + + }, + + doReset: function(e){ + + $('.answertext').html(''); + //$('.answertext').css('color',''); + + $('.square').each(function(index){ + + if($(this).data('guessed') == 'true'){ + $(this).data('guessed',''); + $(this).fadeIn(); + } + + }); + }, + + stopTimer: function(){ + clearInterval(App.puzzleView.intervalID); + App.puzzleView.intervalID = null; + }, + + startTimer: function(){ + if(App.puzzleView.intervalID == null){ + App.puzzleView.intervalID = setInterval('App.puzzleView.timer()', 1000); + } + }, + + timer: function(){ + App.puzzleView.secs = Math.floor(((new Date()) - App.puzzleView.start)/1000) ; + + $('.timer').text(formatTime(App.puzzleView.secs)); + } + +}); + + + +// ----- misc functions + +function formatTime(sec){ + + var hr = Math.floor(sec / 3600); + var min = Math.floor((sec - (hr * 3600))/60); + sec -= ((hr * 3600) + (min * 60)); + sec += ''; min += ''; + while (min.length < 2) {min = '0' + min;} + while (sec.length < 2) {sec = '0' + sec;} + hr = (hr)?':'+hr:''; + + return hr + min + ':' + sec; + +} + +function rot13(str) { + return str.replace(/[a-zA-Z]/g, function(c) { + return String.fromCharCode((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26); + }); +} + + +function shuffleItems(sel) { + var $el = $(sel); + $el.each(function(){ + var items = $(this).children().clone(true); + var ret = (items.length) ? $(this).html(shuffle(items)) : this; + $el.replaceWith(ret); + }); +} + + +function shuffle(arr) { + for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x); + return arr; +} + + + diff --git a/apple-touch-icon.png b/apple-touch-icon.png new file mode 100644 index 0000000..7205bca Binary files /dev/null and b/apple-touch-icon.png differ diff --git a/batch.js b/batch.js new file mode 100644 index 0000000..a92e839 --- /dev/null +++ b/batch.js @@ -0,0 +1,126 @@ +'use strict'; +var fs = require('fs'); + +// List out the possible ways to divide a word into squares with 2 or 3 letters. +var squaresList = {}; +squaresList[2] = [ + {wordlen: 4, squares: [2,2] }, + {wordlen: 5, squares: [2,3] }, + {wordlen: 5, squares: [3,2] }, + {wordlen: 6, squares: [3,3] } +]; +squaresList[3] = [ + {wordlen: 6, squares: [2,2,2] }, + {wordlen: 7, squares: [2,2,3] }, + {wordlen: 7, squares: [2,3,2] }, + {wordlen: 7, squares: [3,2,2] }, + {wordlen: 8, squares: [3,3,2] }, + {wordlen: 8, squares: [3,2,3] }, + {wordlen: 8, squares: [2,3,3] }, + {wordlen: 9, squares: [3,3,3] }, +]; +squaresList[4] = [ + {wordlen: 8, squares: [2,2,2,2] }, + {wordlen: 9, squares: [2,2,2,3] }, + {wordlen: 9, squares: [2,2,3,2] }, + {wordlen: 9, squares: [2,3,2,2] }, + {wordlen: 9, squares: [3,2,2,2] } +]; + +// To fill 20 squares with letters 7 words +var squaresNeeded = [4, 3, 3, 3, 3, 2, 2]; + + +// pull in the word list, divide into lists by word length +var lines = fs.readFileSync('./words.txt').toString().split('\n'); + +var words = {}; + +for (var i = 0; i < lines.length; i++) { + var line = lines[i].trim(); + if(line !== ''){ + var ln = line.split(' - '); + var s = ln[0].length; + if(!words[s]){ + words[s] = []; + } + words[s].push({ + word: ln[0], + def : ln[1].charAt(0).toUpperCase() + ln[1].substr(1) + '.' + }); + + } +} + + +// generate puzzles +var puzzles = []; + +for(var p = 1; p <= 1000; p++){ + + + var puzzle = {}; + var cwords = []; + var csquares = []; + + for (var i = 0; i < squaresNeeded.length; i++) { + + var squareTypes = squaresList[squaresNeeded[i]]; + + var squareType = squareTypes[Math.floor((Math.random() * squareTypes.length))]; + + var wordlist = words[squareType.wordlen]; + + var entry = wordlist[Math.floor((Math.random() * wordlist.length))]; + + cwords.push({ + word: rot13(entry.word), + def: entry.def + }); + + var start = 0; + + var squares = squareType.squares; + + for (var x = 0; x < squares.length; x++) { + var letnum = squares[x]; + + csquares.push(entry.word.substr(start, letnum)); + + start += letnum; + + } + + } + + cwords = shuffle(cwords); + csquares = shuffle(csquares); + + puzzle['clues'] = cwords; + puzzle['squares'] = csquares; + + puzzles.push(puzzle); + +} + +var json = JSON.stringify(puzzles); + +var js = 'var puzzles = ' + json; + +fs.writeFileSync('./puzzles_test.js', js); + + + + +function rot13(str) { + return str.replace(/[a-zA-Z]/g, function(c) { + return String.fromCharCode((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26); + }); +} + +function shuffle(arr) { + for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x); + return arr; +} + + diff --git a/favicon.ico b/favicon.ico new file mode 100644 index 0000000..c4126b4 Binary files /dev/null and b/favicon.ico differ diff --git a/flip.manifest_ b/flip.manifest_ new file mode 100644 index 0000000..5d42abe --- /dev/null +++ b/flip.manifest_ @@ -0,0 +1,18 @@ +CACHE MANIFEST +app.js +apple-touch-icon.png +favicon.ico +lib/backbone.min.js +lib/jquery.min.js +lib/lodash.min.js +lib/normalize.css +main.css +media/ding.mp3 +media/ding.wav +media/flipscreenshot.png +media/flipscreenshot_sm.png +media/star.mp3 +media/star.wav +media/sweep.mp3 +media/sweep.wav +puzzles.js \ No newline at end of file diff --git a/fonts/oxygen400.eot b/fonts/oxygen400.eot new file mode 100644 index 0000000..5f93f00 Binary files /dev/null and b/fonts/oxygen400.eot differ diff --git a/fonts/oxygen400.ttf b/fonts/oxygen400.ttf new file mode 100644 index 0000000..982a032 Binary files /dev/null and b/fonts/oxygen400.ttf differ diff --git a/fonts/oxygen400.woff b/fonts/oxygen400.woff new file mode 100644 index 0000000..00f158e Binary files /dev/null and b/fonts/oxygen400.woff differ diff --git a/fonts/oxygen700.eot b/fonts/oxygen700.eot new file mode 100644 index 0000000..eebd37f Binary files /dev/null and b/fonts/oxygen700.eot differ diff --git a/fonts/oxygen700.ttf b/fonts/oxygen700.ttf new file mode 100644 index 0000000..95c3726 Binary files /dev/null and b/fonts/oxygen700.ttf differ diff --git a/fonts/oxygen700.woff b/fonts/oxygen700.woff new file mode 100644 index 0000000..c0373d1 Binary files /dev/null and b/fonts/oxygen700.woff differ diff --git a/index.html b/index.html new file mode 100644 index 0000000..66ed619 --- /dev/null +++ b/index.html @@ -0,0 +1,169 @@ + + + + + Flip.io + + + + + + + + + + + + + + + + + + + +
+
+

+

+
+
+
+ +
+ + + + + + + + + + + + + + diff --git a/lib/backbone.min.js b/lib/backbone.min.js new file mode 100644 index 0000000..d4b0314 --- /dev/null +++ b/lib/backbone.min.js @@ -0,0 +1,42 @@ +// Backbone.js 0.9.10 + +// (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc. +// Backbone may be freely distributed under the MIT license. +// For all details and documentation: +// http://backbonejs.org +(function(){var n=this,B=n.Backbone,h=[],C=h.push,u=h.slice,D=h.splice,g;g="undefined"!==typeof exports?exports:n.Backbone={};g.VERSION="0.9.10";var f=n._;!f&&"undefined"!==typeof require&&(f=require("underscore"));g.$=n.jQuery||n.Zepto||n.ender;g.noConflict=function(){n.Backbone=B;return this};g.emulateHTTP=!1;g.emulateJSON=!1;var v=/\s+/,q=function(a,b,c,d){if(!c)return!0;if("object"===typeof c)for(var e in c)a[b].apply(a,[e,c[e]].concat(d));else if(v.test(c)){c=c.split(v);e=0;for(var f=c.length;e< +f;e++)a[b].apply(a,[c[e]].concat(d))}else return!0},w=function(a,b){var c,d=-1,e=a.length;switch(b.length){case 0:for(;++d=b);this.root=("/"+this.root+"/").replace(I,"/");b&&this._wantsHashChange&&(this.iframe=g.$('