0% found this document useful (0 votes)
40 views7 pages

Word Le Code

This document contains code for a Wordle game. It declares variables to track the current word, row, and other game states. Functions are defined to check if a submitted word is valid by comparing it to word lists, and to update the color of squares based on if letters match the answer word. An event handler checks for key presses and processes submitted words, updating the game state and colors accordingly. When a game is won or lost, buttons are displayed to start a new game.

Uploaded by

synoviari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views7 pages

Word Le Code

This document contains code for a Wordle game. It declares variables to track the current word, row, and other game states. Functions are defined to check if a submitted word is valid by comparing it to word lists, and to update the color of squares based on if letters match the answer word. An event handler checks for key presses and processes submitted words, updating the game state and colors accordingly. When a game is won or lost, buttons are displayed to start a new game.

Uploaded by

synoviari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

//**********************************************

//************ Wordle Practice ***************


//**********************************************

// Design and word lists from https://www.nytimes.com/games/wordle/index.html


// Declares variable for the current wordle solution
// Sets value to a random item in the answer list
var currentAnswer = "";
readRecords("AnswerList", {}, function(records) {
currentAnswer = records[randomNumber(0, records.length - 1)].words;
});

// Declares all global variables for the program


var processing = false;
var gameWon = false;
var gameLost = false;
var settingsOpen = false;
var statsOpen = false;
var isAllowed;
var currentWord = "";
var currentSquare = 1;
var currentRow = 0;
var charCode = 0;
var gamesPlayed = 0;
var gamesWon = 0;
var gamesLost = 0;
var wonOne = 0;
var wonTwo = 0;
var wonThree = 0;
var wonFour = 0;
var wonFive = 0;
var wonSix = 0;
var curStreak = 0;
var maxStreak = 0;

// Checks if submitted word matches an item in the answer list


// Uses keycode of the first character to set index values in order to shorten list search
// If word is found in answer list isAllowed will be true
function checkAnswerList(word, keyCode) {
var startIndex;
var endIndex;
if (keyCode < 77) {
startIndex = 0;
endIndex = 1500;
} else {
startIndex = 1000;
endIndex = 2314;
}
readRecords("AnswerList", {}, function(records) {
for (var i = startIndex; i <= endIndex; i++) {
if (records[i].words == word) {
isAllowed = true;
break;

file:///C/Users/Beybl/Downloads/WordleCode.txt[2/25/2022 2:24:48 AM]


} else {
isAllowed = false;
}
}
});
}

// Checks if submitted word matches an item in the word list


// Uses keycode of the first character to set index values in order to shorten list search
// If word is found in word list isAllowed will be true
function checkWordList(word, keyCode) {
var startIndex;
var endIndex;
if (keyCode < 67) {
startIndex = 0;
endIndex = 1500;
} else if (keyCode < 70) {
startIndex = 1300;
endIndex = 3000;
} else if (keyCode < 73) {
startIndex = 2800;
endIndex = 4500;
} else if (keyCode < 77) {
startIndex = 4200;
endIndex = 5500;
} else if (keyCode < 79) {
startIndex = 5400;
endIndex = 6500;
} else if (keyCode < 83) {
startIndex = 6200;
endIndex = 8000;
} else if (keyCode < 84) {
startIndex = 7800;
endIndex = 9100;
} else {
startIndex = 9000;
endIndex = 10656;
}
readRecords("WordList", {}, function(records) {
for (var i = startIndex; i <= endIndex; i++) {
if (records[i].words == word) {
isAllowed = true;
break;
} else {
isAllowed = false;
}
}
});
}

// Changes square colors if characters in word match characters in current answer


// Green means the square has the right character and it is in the right place
// Yellow means the square has a character that is in the word but it is in the wrong place
// Gray means the character is not in the word

file:///C/Users/Beybl/Downloads/WordleCode.txt[2/25/2022 2:24:48 AM]


function giveHints(word) {
// Sets wordManip as a list with each character in currentAnswer
var wordManip = [];
var answerManip = [];
for (var i = 0; i < 5; i++) {
wordManip[i] = word.charAt(i);
}
for (i = 0; i < 5; i++) {
answerManip[i] = currentAnswer.charAt(i);
}
// Sets hints as a list with the default rgb values of the squares
var hints = ["rgb(58, 58, 60)", "rgb(58, 58, 60)", "rgb(58, 58, 60)", "rgb(58, 58, 60)", "rgb(58, 58, 60)"];
// Checks for characters in word matching the exact position of characters in wordManip
// If characters match, the rgb value of that square is set to green
// If characters match, the character in the wordManip list is set to null (prevents value from changing again)
for (i = 0; i < 5; i++) {
if (wordManip[i] == answerManip[i]) {
hints[i] = "rgb(84,142,78)";
wordManip[i] = " ";
answerManip[i] = "";
}
}
// Checks for characters in word matching characters of any position in wordManip
// If characters match, the rgb value of that square is set to yellow
for (i = 0; i < 5; i++) {
for (var n = 0; n < 5; n++) {
if (wordManip[n] == answerManip[i]) {
hints[n] = "rgb(181,159,58)";
wordManip[i] = " ";
answerManip[i] = "";
break;
}
}
}
// Sets the colors of each square in the row to the rgb value (every 250 milliseconds)
i = 0;
var interval = setInterval(function() {
setProperty("char" + (i + 1 + currentRow * 5), "background-color", hints[i]);
setProperty("char" + (i + 1 + currentRow * 5), "border-width", 0);
if (i > 3) {
currentRow += 1;
currentSquare = 1;
clearInterval(interval);
}
i += 1;
}, 250);
}

// Event handler for any time a key is pressed on the screen


onEvent("Wordle", "keydown", function(event) {
// Blocks input if the game has been won or lost
if (gameWon == true || gameLost == true) {
} else {
// Sets charCode (used in search functions) to the charcode of key in first square in row

file:///C/Users/Beybl/Downloads/WordleCode.txt[2/25/2022 2:24:48 AM]


if (currentSquare == 1) {
charCode = event.keyCode;
}
// Deletes character in the recent square when backspace is pressed
// If key is not backspace, checks if key is a character a-z and sets text of next square to that character
// If key is not any of the above, and it is enter, the rest of the code is executed
if (event.key == "Backspace" && currentSquare != 1) {
currentSquare -= 1;
setText("char" + (currentSquare + currentRow * 5), "");
} else if (event.keyCode > 64 && event.keyCode < 91 && currentSquare < 6) {
setText("char" + (currentSquare + currentRow * 5), event.key.toUpperCase());
currentSquare += 1;
} else if (event.key == "Enter") {
// Code does not execute if enter has been pressed within the last 4 seconds
if (processing == true) {
} else if (currentSquare == 6) {
processing = true;
currentWord = "";
// Combines the characters from each square in the current row to a string
for (var i = 1; i < 6; i++) {
currentWord += getText("char" + (i + (5 * currentRow))).toLowerCase();
}
// Calls function to check if the word is in the answer list
checkAnswerList(currentWord, charCode);
// Timeout gives function time to read records
setTimeout(function() {
// If word is the current answer, squares are changed to green, stats are added, and game is ended
if (currentAnswer == currentWord) {
giveHints(currentWord);
gameWon = true;
setTimeout(function() {
showElement("PlayAgain");
}, 2500);
gamesPlayed += 1;
gamesWon += 1;
curStreak += 1;
if (maxStreak < curStreak) {
maxStreak = curStreak;
}
if (currentRow + 1 == 1) {
wonOne += 1;
}
if (currentRow + 1 == 2) {
wonTwo += 1;
}
if (currentRow + 1 == 3) {
wonThree += 1;
}
if (currentRow + 1 == 4) {
wonFour += 1;
}
if (currentRow + 1 == 5) {
wonFive += 1;
}

file:///C/Users/Beybl/Downloads/WordleCode.txt[2/25/2022 2:24:48 AM]


if (currentRow + 1 == 6) {
wonSix += 1;
}
// If word is in answer list but not the current answer, hints are given and row is checked to see if player lost
} else if (isAllowed) {
giveHints(currentWord);
// If row is the final row, game is ended and current answer is displayed
if (currentRow == 5) {
gameLost = true;
setTimeout(function() {
showElement("PlayAgain");
setText("MissedWord", "WORD: " + currentAnswer.toUpperCase());
gamesPlayed += 1;
gamesLost += 1;
curStreak = 0;
}, 2500);
}
} else {
// If word is in word list, hints are given and row is checked to see if player lost
checkWordList(currentWord, charCode);
setTimeout(function() {
if (isAllowed) {
giveHints(currentWord);
// If row is the final row, game is ended and current answer is displayed
if (currentRow == 5) {
gameLost = true;
setTimeout(function() {
showElement("PlayAgain");
setText("MissedWord", "WORD: " + currentAnswer.toUpperCase());
gamesPlayed += 1;
gamesLost += 1;
curStreak = 0;
}, 2500);
}
} else {
// If word is not in either list, a notifier is displayed and entry is invalid
setPosition("NotInWordList", 95, 105 + (currentRow * 55));
showElement("NotInWordList");
setTimeout(function() {
hideElement("NotInWordList");
}, 1000);
}
}, 1000);
}
}, 1000);
}
setTimeout(function() {
processing = false;
}, 4000);
}
}
});

// When play again button is clicked, squares and global variables are reset

file:///C/Users/Beybl/Downloads/WordleCode.txt[2/25/2022 2:24:48 AM]


onEvent("PlayAgain", "click", function() {
hideElement("PlayAgain");
readRecords("AnswerList", {}, function(records) {
currentAnswer = records[randomNumber(1, 2315)].words;
});
for (var i = 1; i < 31; i++) {
setProperty("char" + i, "background-color", "rgb(58, 58, 60, 0)");
setProperty("char" + i, "border-width", 1);
setText("char" + i, "");
}
currentWord = "";
currentSquare = 1;
currentRow = 0;
charCode = 0;
gameWon = false;
gameLost = false;
setText("MissedWord", "");
});

// When stats button is clicked, all recorded statistics for the session are displayed
// Bar graph is drawn based on win distribution
onEvent("Stats" ,"click", function() {
if (statsOpen == true) {
statsOpen = false;
hideElement("StatsList");
hideElement("StatisticsTitle");
hideElement("Played");
hideElement("PlayedText");
hideElement("Win%");
hideElement("Win%Text");
hideElement("CurrentStreak");
hideElement("CurrentStreakText");
hideElement("MaxStreak");
hideElement("MaxStreakText");
hideElement("GuessDistribution");
} else {
statsOpen = true;
var data = [
{ guess: "1", won: (wonOne) },
{ guess: "2", won: (wonTwo) },
{ guess: "3", won: (wonThree) },
{ guess: "4", won: (wonFour) },
{ guess: "5", won: (wonFive) },
{ guess: "6", won: (wonSix) }
];
drawChart("GuessDistribution", "bar", data);
setText("Played", gamesPlayed);
if (gamesPlayed == 0) {
setText("Win%", 0);
} else {
setText("Win%", Math.floor(100 * gamesWon / gamesPlayed));
}
setText("CurrentStreak", curStreak);
setText("MaxStreak", maxStreak);

file:///C/Users/Beybl/Downloads/WordleCode.txt[2/25/2022 2:24:48 AM]


showElement("StatsList");
showElement("StatisticsTitle");
showElement("Played");
showElement("PlayedText");
showElement("Win%");
showElement("Win%Text");
showElement("CurrentStreak");
showElement("CurrentStreakText");
showElement("MaxStreak");
showElement("MaxStreakText");
showElement("GuessDistribution");
}
});

// When settings button is clicked, mobile mode is enabled to allow typing on mobile devices
onEvent("Settings" ,"click", function() {
if (settingsOpen == true) {
settingsOpen = false;
hideElement("MobileInput");
} else {
settingsOpen = true;
showElement("MobileInput");
}
});

// When mobile mode text box receives input, the characters are deleted. All game inputs are recorded through the
screen event handler
onEvent("MobileInput", "input", function() {
setText("MobileInput", "MOBILE MODE");
});

file:///C/Users/Beybl/Downloads/WordleCode.txt[2/25/2022 2:24:48 AM]

You might also like