From a5ce3a61c22e6730bc2d161a4a512e9d1e7d51be Mon Sep 17 00:00:00 2001 From: pommicket Date: Thu, 28 Jul 2016 18:28:00 -0400 Subject: Added The Anagram Game --- all.html | 9 +++ anagramgame.html | 27 +++++++++ js/anagramgame.js | 133 ++++++++++++++++++++++++++++++++++++++++++++ screenshots/anagramgame.png | Bin 0 -> 5078 bytes 4 files changed, 169 insertions(+) create mode 100644 anagramgame.html create mode 100644 js/anagramgame.js create mode 100644 screenshots/anagramgame.png diff --git a/all.html b/all.html index 289f220..e80f7f3 100644 --- a/all.html +++ b/all.html @@ -182,6 +182,13 @@ approximates π!


+
+The Anagram Game
+

+Find an anagram of a random 7-letter word. +

+
+
AutoArt
The Android app for AutoImages.
@@ -201,6 +208,8 @@ The Android app for Ball Bounce.


+ +

More coming soon!

diff --git a/anagramgame.html b/anagramgame.html new file mode 100644 index 0000000..b272eec --- /dev/null +++ b/anagramgame.html @@ -0,0 +1,27 @@ + + + The Anagram Game + + + + + +

The Anagram Game

+ + +
+

The word list can be found here.

+
Loading word list...
+
+ +
+ + + + + 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 = "Correct!"; + + document.getElementById("again").style = ""; + } + else + { + document.getElementById("answer").style = ""; + document.getElementById("answer").innerHTML = "Incorrect!"; + } +} + +function giveUp() +{ + document.getElementById("answer").style = ""; + document.getElementById("answer").innerHTML = "The answer was " + anagram + "."; + + document.getElementById("again").style = ""; +} + +loadWords(); diff --git a/screenshots/anagramgame.png b/screenshots/anagramgame.png new file mode 100644 index 0000000..1de2e51 Binary files /dev/null and b/screenshots/anagramgame.png differ -- cgit v1.2.3