diff options
author | pommicket <leonardomtenenbaum@gmail.com> | 2016-07-28 18:28:00 -0400 |
---|---|---|
committer | pommicket <leonardomtenenbaum@gmail.com> | 2016-07-28 18:28:00 -0400 |
commit | a5ce3a61c22e6730bc2d161a4a512e9d1e7d51be (patch) | |
tree | d5b8dc5ec67afde89331de90159df855de794a3a | |
parent | 979ac58c3d79fa2f2170e55746f700660be52913 (diff) |
Added The Anagram Game
-rw-r--r-- | all.html | 9 | ||||
-rw-r--r-- | anagramgame.html | 27 | ||||
-rw-r--r-- | js/anagramgame.js | 133 | ||||
-rw-r--r-- | screenshots/anagramgame.png | bin | 0 -> 5078 bytes |
4 files changed, 169 insertions, 0 deletions
@@ -182,6 +182,13 @@ approximates π!<br> </p> <hr> +<img src="screenshots/anagramgame.png" width=250 height=250><br> +<a href="anagramgame.html">The Anagram Game</a><br> +<p> +Find an anagram of a random 7-letter word. +</p> +<hr> + <img src="screenshots/autoartapp.png" width=200><br> <a href="https://play.google.com/store/apps/details?id=org.neocities.autoart.autoart">AutoArt</a><br> The Android app for <a href="AutoImages.html">AutoImages</a>.<br> @@ -201,6 +208,8 @@ The Android app for <a href="ballbounce.html">Ball Bounce</a>.<br> </p> <hr> + + <p> More coming soon! </p> diff --git a/anagramgame.html b/anagramgame.html new file mode 100644 index 0000000..b272eec --- /dev/null +++ b/anagramgame.html @@ -0,0 +1,27 @@ +<html> + <head> + <title>The Anagram Game</title> + <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> + <link rel="stylesheet" href="css/style.css"> + </head> + + <body> + <h2>The Anagram Game</h2> + <div id="header_links_div"></div> + <script src="js/header_links.js"></script> + <hr> + <p>The word list can be found <a href="https://raw.githubusercontent.com/sindresorhus/word-list/master/words.txt">here</a>.</p> + <div id="loading">Loading word list...</div> + <div id="word"></div> + <div id="hidden" style="visibility: hidden;"> + <input type="text" id="guess" onKeyDown="if(event.keyCode==13) submit();"> + <button onclick="submit();">Submit guess</button> + <button onclick="giveUp();">Give up?</button> + + </div> + <div id="answer"></div> + <button id="again" style="visibility: hidden;" onclick="play();">Play again</button> + <script src="js/anagramgame.js"></script> + </body> + +</html> diff --git a/js/anagramgame.js b/js/anagramgame.js new file mode 100644 index 0000000..82c403a --- /dev/null +++ b/js/anagramgame.js @@ -0,0 +1,133 @@ +var words = []; +var word; +var anagram; +var alphabet = "qwertyuiopasdfghjklzxcvbnm"; +var letters = alphabet + alphabet.toUpperCase(); + + +Array.prototype.equals = function (array) +{ + if (this.length != array.length) + return false; + for (var i = 0; i < array.length; i++) + { + if (this[i] != array[i]) + return false; + } + return true; +} + +function containsSymbols(w) +{ + for (var i = 0; i < w.length; i++) + { + if (!letters.includes(w[i])) + return true; + } + return false; +} + +function letterCounts(w) +{ + var lc = []; + for (var i = 0; i < 26; i++) + { + lc.push(0); + } + var w = w.toLowerCase(); + for (var i = 0; i < w.length; i++) + { + lc[w.charCodeAt(i)-97]++; + } + return lc; +} + +function isAnagram(w1, w2) +{ + return letterCounts(w1).equals(letterCounts(w2)); +} + +function findWord() +{ + var numWords = words.length; + word = words[Math.floor(Math.random()*numWords)]; + if (containsSymbols(word)) + findWord(); + var lc = letterCounts(word); + for (var i = 0; i < numWords; i++) + { + if (words[i] != word && letterCounts(words[i]).equals(lc)) + { + anagram = words[i]; + return; + } + } + findWord(); +} + +function play() +{ + document.getElementById("hidden").style = "visibility: hidden;"; + document.getElementById("again").style = "visibility: hidden;"; + document.getElementById("answer").style = "visibility: hidden;"; + document.getElementById("guess").value = ""; + document.getElementById("loading").innerHTML = "Finding a word with an anagram..."; + findWord(); + document.getElementById("loading").innerHTML = ""; + document.getElementById("word").innerHTML = "Find an anagram of " + word + "."; + document.getElementById("hidden").style = ""; + +} + +function isWord(x) +{ + return words.indexOf(x) > -1; +} + +function loadWords() +{ + var xhttp = new XMLHttpRequest(); + xhttp.onreadystatechange = function() + { + if (xhttp.readyState == 4 && xhttp.status == 200) + { + var allwords = xhttp.responseText.split('\n'); + words = []; + for (var i = 0; i < allwords.length; i++) + { + if (allwords[i].length == 7) + words.push(allwords[i].toLowerCase()); + } + play(); + } + }; + xhttp.open("GET", "https://raw.githubusercontent.com/sindresorhus/word-list/master/words.txt", true); + xhttp.send(); +} + +function submit() +{ + var guess = document.getElementById("guess").value.toLowerCase(); + if (letterCounts(guess).equals(letterCounts(word)) && isWord(guess)) + { + document.getElementById("answer").style = ""; + document.getElementById("answer").innerHTML = "<span style='color: green'>Correct!</span>"; + + document.getElementById("again").style = ""; + } + else + { + document.getElementById("answer").style = ""; + document.getElementById("answer").innerHTML = "<span style='color: red'>Incorrect!</span>"; + } +} + +function giveUp() +{ + document.getElementById("answer").style = ""; + document.getElementById("answer").innerHTML = "<span style='color: red'>The answer was " + anagram + ".</span>"; + + document.getElementById("again").style = ""; +} + +loadWords(); diff --git a/screenshots/anagramgame.png b/screenshots/anagramgame.png Binary files differnew file mode 100644 index 0000000..1de2e51 --- /dev/null +++ b/screenshots/anagramgame.png |