Created
July 16, 2016 23:14
-
-
Save arayi/fcc94eef6ebb1546258b20a09f37bafc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var playerScore = 0; | |
var correctGuesses = []; | |
// A function to get the set of possible letters | |
function getLetters() { | |
// Select the element with the id 'letters' | |
var lettersContainer = document.querySelector('#letters'); | |
// Get the text content of the element | |
var letters = lettersContainer.innerText; | |
// Return the letters | |
return letters; | |
} | |
// A function to get the user's guess | |
function getGuess() { | |
// Select the input element where the user enters their guess | |
var wordGuess = document.querySelector('input#word-guess'); | |
// Get the text content of the element | |
var guess = wordGuess.value; | |
console.log(guess); | |
// Return the guess | |
return guess; | |
} | |
function shuffleLetters() { | |
var currentLetters = getLetters().split(""); | |
var newLetters = new Array(7); | |
var randomIndex = (Math.floor(Math.random() * 7)); | |
for (i=0; i<7; i++) { | |
while (newLetters[randomIndex]) { | |
randomIndex = (Math.floor(Math.random() * 7)); | |
} | |
newLetters[randomIndex] = currentLetters.pop(); | |
} | |
return newLetters.join(''); | |
} | |
// A function to display a message on the screen | |
function showMessage(messageText) { | |
// Select the element to display a message | |
var messageElem = document.querySelector('#game-message'); | |
// Set the text value of the element to the provided text | |
messageElem.innerText = messageText; | |
} | |
// A function to check whether the guessed word is correct or not | |
function checkGuess() { | |
// Collect the text from the letters and the guess | |
var letters = getLetters(); | |
var guess = getGuess(); | |
if (guess.length === 0 || guess.length > 7) { | |
showMessage("Guess is the wrong length! Try again!"); | |
return false; | |
} | |
// Convert both to uppercase so we can compare equals | |
guess = guess.toUpperCase().split(''); | |
letters = letters.toUpperCase().split(''); | |
// Determine if all the characters in the guess are in the letters | |
for (var i = 0; i < guess.length; i++) { | |
var currentChar = guess[i]; | |
// If the current character can't be found in letters, the guess is incorrect | |
if (letters.indexOf(currentChar) === -1) { | |
// Show a message saying guess is incorrect | |
showMessage("Wrong guess, try again."); | |
// Return false to exit the function | |
return false; | |
} | |
letters[letters.indexOf(currentChar)] = "-"; | |
console.log(letters); | |
} | |
// If we've made it this far, then the guess must be correct! | |
// Show a message saying guess is correct | |
showMessage("Good guess, that is correct!"); | |
correctGuesses.push(guess.join('')); | |
document.getElementById("guesses").innerHTML = "<div>" + correctGuesses.join("</div><div>") + "</div><br>"; | |
// Return true to exit the function | |
return true; | |
} | |
function getNewLetters() | |
{ | |
var text = ""; | |
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
var vowels = "AEIOUY"; | |
var vowelNumber = 0; | |
for( var i=0; i < 7; i++ ) { | |
text += possible.charAt(getWeightedCharacterIndex()); | |
} | |
for (var x=0; x<7; x++) { | |
if (vowels.indexOf(text[x]) != -1) { | |
vowelNumber += 1; | |
} | |
} | |
if (vowelNumber < 2) { | |
return getNewLetters(); | |
} else { | |
return text; | |
} | |
} | |
function updateLetters(fun, message) { | |
document.getElementById("letters").innerHTML = fun(); | |
showMessage(message); | |
} | |
function startNewGame() { | |
updateLetters(getNewLetters, "New game - good luck!"); | |
document.getElementById("guesses").innerHTML = ""; | |
} | |
function shuffle() { | |
updateLetters(shuffleLetters, "Letters shuffled!"); | |
} | |
var letterThresholds = [8167, 9659, 12441, 16694, 29396, 31624, 33639, 39733, 46699, 46852, 47624, 51649, 54055, 60804, 68311, 70240, 70335, 76322, 82649, 91705, 94463, 95441, 97801, 97951, 99925, 99999]; | |
function getWeightedCharacterIndex() { | |
var num = Math.floor(Math.random() * 99999); | |
for (var x=0; x<letterThresholds.length; x++) { | |
if (num < letterThresholds[x]) { | |
return x; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment