xxxxxxxxxx
let wordList; // List: stores all of the guessable words.
let validityCheckList; // List: stores a huge amount of five letter words.
let mysteryWord; // Stores the current mystery word.
let board = []; // Stores all of the tiles.
let buttons = []; // Stores all of the letter buttons.
let backspaceButton; // Stores the "DEL" button.
let enterButton; // Stores the "ENTER" button.
// Object: stores the current row and column value of the selected tile.
let selected = {
row: 0,
col: 0
}
let alpha = "abcdefghijklmnopqrstuvwxyz"; // String that stores all of the letters in the alphabet. For creating the letter buttons and to check a valid letter when a key is pressed.
let selectedColor = 'blue'; // Stores the color the selected tile will be when it is selected.
let revealText = ''; // Stores the text that the label between the tiles and the on-screen keyboard displays.
let endGame = false; // Stores whether the game has ended or not; if the user has won/lost or not.
function preload() {
// Loads in the two word lists and trims them.
wordList = loadTable("officialwordle.csv");
validityCheckList = loadTable("words2 - words2.csv");
wordList.trim();
validityCheckList.trim();
}
function setup() {
// Chooses a random word from the possible-answers word list and sets it as the mystery word.
mysteryWord = wordList.getString(floor(random(wordList.getRowCount())), 0).toLowerCase();
// Creates Canvas, background, & sets align.
createCanvas(windowWidth, windowHeight);
background(0);
rectMode(CENTER);
textAlign(CENTER, CENTER);
// Creates the tiles & the keyboard.
createBoard();
}
function createBoard() {
for (let rowNum = 0; rowNum < 6; rowNum++) {
board.push([]); // Creates 6 rows of tiles.
for (let i = 0; i < 5; i++) {
// Creates 5 tiles for each row.
let size = width / 20;
let pos = createVector(width / 2 - (size * 2) + (i * size), height / 2 - (size * 3) + (rowNum * size));
board[rowNum].push(new Tile(pos, size, '', 'white'));
}
}
board[selected.row][selected.col].f = selectedColor; // Sets the top-left tile as the selected tile.
let buttonSize = width / 45;
for (let j = 0; j < 2; j++) {
for (let i = 0; i < alpha.length / 2; i++) {
// Creates two rows of buttons to be the on-screen keyboard.
buttons.push(new Button(((width / 2) - (alpha.length / 4.34 * buttonSize)) + (i * buttonSize), (height * (18 / 20)) + (j * buttonSize), buttonSize, 'black', alpha[i + (alpha.length / 2 * j)]));
}
}
// Creates the backspace button.
backspaceButton = new ControlKey(13.5 * (width / 20), 18 * (height / 20), buttonSize * 2, buttonSize, "DEL");
// Creates the enter button.
enterButton = new ControlKey(13.5 * (width / 20), 18 * (height / 20) + buttonSize, buttonSize * 2, buttonSize, "ENTER");
}
function mousePressed() {
for (let button of buttons) {
if (button.contains(mouseX, mouseY)) {
// If the user clicks a letter button, the code in keyPressed() that runs when the corresponding key on the keyboard is pressed will run.
key = null;
key = button.value;
keyPressed();
}
}
if (enterButton.contains(mouseX, mouseY)) {
// If the user clicks on the enter button, the code in keyPressed() that runs when the Enter Key is pressed will run.
key = null;
keyCode = 13;
keyPressed();
}
else if (backspaceButton.contains(mouseX, mouseY)) {
// If the user clicks on the "DEL" button, the code in keyPressed() that runs when the user presses the Backspace key will run.
key = null;
keyCode = 8;
keyPressed();
}
}
function keyPressed() {
if (!endGame) { // If the user hasn't won or lost the game yet:
if (alpha.includes(key)) { // If the user presses any letter key on the keyboard or clicks its corresponding button:
board[selected.row][selected.col].value = key.toUpperCase(); // Draws the letter inside the tile.
board[selected.row][selected.col].size = width / 100; // Sets the width of the tile to be very small. Makes the expanding animation work.
if (selected.col < board[selected.row].length - 1) { // If the user hasn't reached the end of the row yet:
// Moves to the next tile.
board[selected.row][selected.col].f = 'white';
selected.col += 1;
board[selected.row][selected.col].f = selectedColor;
}
} else if (keyCode == LEFT_ARROW || keyCode == RIGHT_ARROW) { // If the user presses the Left or Right Arrow Keys.
// Moves to the next or previous tile if the user hasn't reached either end of the row yet.
board[selected.row][selected.col].f = 'white';
if (keyCode == LEFT_ARROW && selected.col > 0) selected.col -= 1;
else if (keyCode == RIGHT_ARROW && selected.col < board[selected.row].length - 1) selected.col += 1;
board[selected.row][selected.col].f = selectedColor;
} else if (keyCode == 8) { // If the user presses the Backspace key.
board[selected.row][selected.col].value = ''; // Deletes the letter in the selected tile.
if (selected.col > 0) {
// Moves to the previous tile if the user hasn't reached the front yet.
board[selected.row][selected.col].f = 'white';
selected.col -= 1;
board[selected.row][selected.col].f = selectedColor;
}
} else if (keyCode == 13 && checkInputLength() && checkValidWord()) { // If the user inputs a valid, five-letter word, and presses the Enter Key:
board[selected.row][selected.col].f = 'white'; // Deselects the current tile.
checkWord(); // Color codes each tile according to wordle rules.
if (selected.row < board.length - 1) {
// Moves on to the next row and selects the first tile of that row when the user hasn't reached the end yet.
selected.row += 1;
selected.col = 0;
board[selected.row][selected.col].f = selectedColor;
} else {
// Reveals the word and ends the game if the user moves on from the last row.
revealText = 'The word was: ' + mysteryWord + '.';
endGame = true;
}
}
}
}
function checkInputLength() {
// Returns false if any tile in the current row is blank. Returns true if not.
for (let tile of board[selected.row]) {
if (tile.value == '') return false;
}
return true;
}
function checkValidWord() {
// Constructs the current guess in the row to string form.
let activeGuess = '';
for (let i = 0; i < board[selected.row].length; i++) {
activeGuess += board[selected.row][i].value.toLowerCase();
}
// Iterates through every word in the mega-word list, and checks to see of the current guess is equal to any of those words. Returns true if so. Returns false if not.
for (let i = 0; i < validityCheckList.getRowCount(); i++) {
if (activeGuess == validityCheckList.getString(i, 0).toLowerCase()) {
return true
}
}
return false
}
function checkWord() {
// Constructs the active guess into stirng form.
let activeGuess = '';
for (let i = 0; i < board[selected.row].length; i++) {
activeGuess += board[selected.row][i].value.toLowerCase();
}
// Iterates through every letter in the active guess; every tile in the current row.
for (let checkIndex = 0; checkIndex < activeGuess.length; checkIndex++) {
// Sets the current tile to green if the letter in the tile is in the word and in the correct position.
if (activeGuess[checkIndex] == mysteryWord[checkIndex]) board[selected.row][checkIndex].f = 'lawnGreen';
// Sets the current tile to red if the letter in the tile is in the word, but not in the right position.
else if (mysteryWord.includes(activeGuess[checkIndex])) board[selected.row][checkIndex].f = 'fireBrick';
// Sets the current tile to gray if the letter is not in the word.
else board[selected.row][checkIndex].f = 100;
for (let button of buttons) {
// Sets the corresponding button in the on-screen keyboard to the same color as the current tile.
if (board[selected.row][checkIndex].value == button.value.toUpperCase() && button.f != 'lawnGreen') button.f = board[selected.row][checkIndex].f;
}
}
if (mysteryWord == activeGuess) {
// Ends the game and gives a congratulatins message if the user guesses the correct word.
endGame = true;
revealText = "Good job! You guessed the word!"
}
}
function draw() {
background(0); // Refreshes the screen every frame.
for (let row of board) {
for (let tile of row) {
// Draws every tile and makes the expansion animation work.
tile.draw();
tile.expand();
}
}
for (let button of buttons) {
// Draws every button.
button.draw();
}
// Draws the backspace and enter buttons.
backspaceButton.draw();
enterButton.draw();
// Draws the label between the tiles and the buttons.
fill('white');
textSize(40);
textStyle('bold');
text(revealText, width / 2, height * (83 / 100));
}