diff options
| author | Li Zhineng <[email protected]> | 2013-06-07 17:44:47 +0800 |
|---|---|---|
| committer | Li Zhineng <[email protected]> | 2013-06-07 17:44:47 +0800 |
| commit | c080e07ee1a0004d732508f9ed011678a38eda5e (patch) | |
| tree | 056707266c62dca43ba90de7148c8722628c3d16 | |
| parent | cf75ddf153d4fbddd9ae8a517a9c6121e995972b (diff) | |
| download | word-search-game-c080e07ee1a0004d732508f9ed011678a38eda5e.tar.gz word-search-game-c080e07ee1a0004d732508f9ed011678a38eda5e.zip | |
First commit
| -rw-r--r-- | LICENSE | 7 | ||||
| -rw-r--r-- | README.md | 14 | ||||
| -rw-r--r-- | css/style.css | 35 | ||||
| -rw-r--r-- | index.html | 23 | ||||
| -rw-r--r-- | js/utility.js | 3 | ||||
| -rw-r--r-- | js/wordsearch.js | 203 |
6 files changed, 283 insertions, 2 deletions
@@ -0,0 +1,7 @@ +Copyright (c) 2013 Zhineng Li <[email protected]> and other `word-search-game` contributors + +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 @@ -1,4 +1,14 @@ -word-search-game -================ +# Word search game A word game which programming with HTML5 and Javascript. + +## License +The MIT License (MIT) + +Copyright (c) 2013 Zhineng Li <[email protected]> and other `word-search-game` contributors + +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/css/style.css b/css/style.css new file mode 100644 index 0000000..c07319c --- /dev/null +++ b/css/style.css @@ -0,0 +1,35 @@ +/* ========================================== +/* +/* CSS RESET +/* +/* ========================================== */ + +body, h1 { + margin: 0; + padding: 0; +} + +/* ========================================== +/* +/* HOME +/* +/* ========================================== */ + +.wrap { + width: 960px; + margin: 0 auto; + padding-top: 40px; +} + +/* ========================================== +/* +/* GAMEAREA +/* +/* ========================================== */ + +#gamearea { + margin: 10px 20px; + background: #efefef; + border-radius: 10px; + position: relative; +}
\ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..aefb2d6 --- /dev/null +++ b/index.html @@ -0,0 +1,23 @@ +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="utf-8"> + <title>Word search game</title> + <link rel="stylesheet" href="css/style.css" /> + </head> + <body> + <div class="wrap"> + <h1>Word search game</h1> + <section id="gameArea"></section> + </div> + <script src="js/utility.js"></script> + <script src="js/wordsearch.js"></script> + <script type="text/javascript"> + var gameAreaEl = document.getElementById('gameArea'); + gameAreaEl.wordSeach({ + 'gridSize': 10, + 'words': ['apple'] + }); + </script> + </body> +</html>
\ No newline at end of file diff --git a/js/utility.js b/js/utility.js new file mode 100644 index 0000000..ab67cd2 --- /dev/null +++ b/js/utility.js @@ -0,0 +1,3 @@ +function range(min, max) { + return Math.floor(Math.random() * (max - min + 1)) + min; +}
\ No newline at end of file diff --git a/js/wordsearch.js b/js/wordsearch.js new file mode 100644 index 0000000..7cf0375 --- /dev/null +++ b/js/wordsearch.js @@ -0,0 +1,203 @@ +(function(){ + + Element.prototype.wordSeach = WordSeachHelper; + + function WordSeachHelper(settings) { + return new WordSeach(this, settings); + } + + /** + * Word seach + */ + function WordSeach(wrapEl, settings) { + this.wrapEl = wrapEl; + this.settings = settings; + + // Check the words' length if it is overflow the grid + if (this.parseWords(this.settings.gridSize)) { + // Add words into the martrix data + var isWorded = false; + + while (isWorded == false) { + // initialize the application + this.initialize(); + + isWorded = this.addWords(); + } + + + // Draw the martrix into wrap element + this.drawMartrix(); + + console.log(this); + } + } + + /** + * Add words into the martrix + */ + WordSeach.prototype.addWords = function() { + var keepGoing = true, + counter = 0, + isWorded = true; + + while (keepGoing) { + //var dir = Math.floor(Math.random() * 3), + var dir = 3, + result = this.addWord(this.settings.words[counter], dir), + isWorded = true; + + if (result == false) { + keepGoing = false; + isWorded = false; + } + + counter++; + if (counter >= this.settings.words.length) { + keepGoing = false; + } + } + + return isWorded; + } + + /** + * Add word into the martrix + */ + WordSeach.prototype.addWord = function(word, direction) { + var itWorked = true; + + switch (direction) { + case 0: // Horizontal + // New row(y) and col(x) + var row = Math.floor(Math.random() * (this.settings.gridSize - 1)), + col = Math.floor(Math.random() * (this.settings.gridSize - word.length + 1)); + + for (var i = 0; i < word.length; i++) { + var origin = this.martrix[row][col + i].letter; + if (origin == '.' || origin == word[i]) { + this.martrix[row][col + i].letter = word[i]; + } else { + itWorked = false; + } + } + break; + + case 1: // vertical + var col = Math.floor(Math.random() * (this.settings.gridSize - 1)), + row = Math.floor(Math.random() * (this.settings.gridSize - word.length + 1)); + + for (var i = 0; i < word.length; i++) { + var origin = this.martrix[row + i][col].letter; + if (origin == '.' || origin == word[i]) { + this.martrix[row + i][col].letter = word[i]; + } else { + itWorked = false; + } + } + break; + + case 2: // From top left to bottom right + var col = Math.floor(Math.random() * (this.settings.gridSize - word.length + 1)), + row = Math.floor(Math.random() * (this.settings.gridSize - word.length + 1)); + + for (var i = 0; i < word.length; i++) { + var origin = this.martrix[row + i][col + i].letter; + if (origin == '.' || origin == word[i]) { + this.martrix[row + i][col + i].letter = word[i]; + } else { + itWorked = false; + } + } + break; + + case 3: // From top right to bottom left + var col = range(this.settings.gridSize - word.length + 1, this.settings.gridSize - 1), + row = range(0, this.settings.gridSize - word.length); + + console.log(word, row, col); + + for (var i = 0; i < word.length; i++) { + var origin = this.martrix[row + i][col - i].letter; + if (origin == '.' || origin == word[i]) { + this.martrix[row + i][col - i].letter = word[i]; + } else { + itWorked = false; + } + } + break; + } + + return itWorked; + } + + /** + * Initialize the application + */ + WordSeach.prototype.initialize = function() { + /** + * Store the words + * + * param {Array} + */ + this.martrix = []; + + this.initMartrix(this.settings.gridSize); + } + + WordSeach.prototype.parseWords = function(maxSize) { + var itWord = true; + + this.settings.words.forEach(function(word){ + if (word.length > maxSize) { + alert('The length of word `' + word + '` is overflow the gridSize.'); + console.error('The length of word `' + word + '` is overflow the gridSize.'); + itWord = false; + } + }); + + return itWord; + } + + /** + * Fill default items into the martrix + */ + WordSeach.prototype.initMartrix = function(size) { + for (var row = 0; row < size; row++) { + for (var col = 0; col < size; col++) { + var item = { + 'letter': '.' // Default value + } + + if (!this.martrix[row]) { + this.martrix[row] = []; + } + + this.martrix[row][col] = item; + } + } + } + + /** + * Draw the martrix + */ + WordSeach.prototype.drawMartrix = function() { + for (var row = 0; row < this.settings.gridSize; row++) { + // New row + var divEl = document.createElement('div'); + this.wrapEl.appendChild(divEl); + + for (var col = 0; col < this.settings.gridSize; col++) { + var cvEl = document.createElement('canvas'); + cvEl.setAttribute('width', 40); + cvEl.setAttribute('height', 40); + + var ctx = cvEl.getContext('2d'); + ctx.fillText(this.martrix[row][col].letter, 0, 20); + + divEl.appendChild(cvEl); + } + } + } + +})();
\ No newline at end of file |
