I’ve this javascript wordle like game code and I would like to change/add three functions
First of all, I would like to remove the randomization of result function, instead show the result from array one by one, ie, in order
I know Math.floor(Math.random() is doing the randomization, but how to change it and instead, pick answers one by one? Removing [Math.floor(Math.random() * wordList.length)].toUpperCase(); is displaying all the results in the array, not one at a time
Secondly, after one guess is over and the answer is shown, I want to show some other text along with the answer, from another array maybe. For example, if the answer is “abcde”, I want it to show abcde 1234
I know <h1 id="answer"></h1> and
if (!gameOver && row == height) {
gameOver = true;
document.getElementById("answer").innerText = word;
setTimeout(function () {
window.location.reload(false);
}, 200000);
}
}
function update() {
let guess = "";
document.getElementById("answer").innerText = "";
are responsible for showing the correct answer but how to add another h1 and get some other text along with the answer? Naturally, I cant add more text to the answer array itself as it’ll break the game
Thirdly, I want to remove the letter limit from the game. ie, right now there are five boxes and I don’t want to limit the answers to an exact five letter word. I want it to accept the answer and move to next game even if the answer given is only a three letter word
Finally, I would like to display different titles each time a new game is loaded. I know <h1 id="title"> is responsible for adding the current title but how to show a different title when a new game is loaded? (A title that matches the answer given)
I’m not expecting anybody to give me solutions for all the questions, even if you can answer only one question, I’ll appreciate it, and if possible please give me the code in answer and tell me what changes should I make and where (or please add the code to the given code)
var height = 6;
var width = 5;
var row = 0;
var col = 0;
var gameOver = false;
var wordList = ['abcde',
'fghij',
'klmno',
'pqrst'
]
var word = wordList[Math.floor(Math.random() * wordList.length)].toUpperCase();
console.log(word);
window.onload = function() {
intialize();
}
function intialize() {
//
for (let r = 0; r < height; r++) {
for (let c = 0; c < width; c++) {
// <span id="0-0" class="tile">P</span>
let tile = document.createElement("span");
tile.id = r.toString() + "-" + c.toString();
tile.classList.add("tile");
tile.innerText = "";
document.getElementById("board").appendChild(tile);
}
}
//
let keyboard = [
["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
["A", "S", "D", "F", "G", "H", "J", "K", "L", "Ç"],
["Enter", "Z", "X", "C", "V", "B", "N", "M", "⌫"]
]
for (let i = 0; i < keyboard.length; i++) {
let currRow = keyboard[i];
let keyboardRow = document.createElement("div");
keyboardRow.classList.add("keyboard-row");
for (let j = 0; j < currRow.length; j++) {
let keyTile = document.createElement("div");
let key = currRow[j];
keyTile.innerText = key;
if (key == "Enter") {
keyTile.id = "Enter";
} else if (key == "⌫") {
keyTile.id = "Backspace";
} else if ("A" <= key && key <= "Z") {
keyTile.id = "Key" + key; // "Key" + "A";
}
keyTile.addEventListener("click", processKey);
if (key == "Enter") {
keyTile.classList.add("enter-key-tile");
} else {
keyTile.classList.add("key-tile");
}
keyboardRow.appendChild(keyTile);
}
document.body.appendChild(keyboardRow);
}
//
document.addEventListener("keyup", (e) => {
processInput(e);
})
}
function processKey() {
e = {
"code": this.id
};
processInput(e);
}
function processInput(e) {
if (gameOver) return;
//
if ("KeyA" <= e.code && e.code <= "KeyZ") {
if (col < width) {
let currTile = document.getElementById(row.toString() + '-' + col.toString());
if (currTile.innerText == "") {
currTile.innerText = e.code[3];
col += 1;
}
}
} else if (e.code == "Backspace") {
if (0 < col && col <= width) {
col -= 1;
}
let currTile = document.getElementById(row.toString() + '-' + col.toString());
currTile.innerText = "";
} else if (e.code == "Enter") {
update();
}
if (!gameOver && row == height) {
gameOver = true;
document.getElementById("answer").innerText = word;
setTimeout(function() {
window.location.reload(false);
}, 200000);
}
}
function update() {
let guess = "";
document.getElementById("answer").innerText = "";
//
for (let c = 0; c < width; c++) {
let currTile = document.getElementById(row.toString() + '-' + c.toString());
let letter = currTile.innerText;
guess += letter;
}
guess = guess.toLowerCase(); //
console.log(guess);
//
let correct = 0;
let letterCount = {}; //
for (let i = 0; i < word.length; i++) {
let letter = word[i];
if (letterCount[letter]) {
letterCount[letter] += 1;
} else {
letterCount[letter] = 1;
}
}
console.log(letterCount);
//
for (let c = 0; c < width; c++) {
let currTile = document.getElementById(row.toString() + '-' + c.toString());
let letter = currTile.innerText;
//
if (word[c] == letter) {
currTile.classList.add("correct");
let keyTile = document.getElementById("Key" + letter);
keyTile.classList.remove("present");
keyTile.classList.add("correct");
correct += 1;
letterCount[letter] -= 1; //
}
if (correct == width) {
gameOver = true;
setTimeout(function() {
window.location.reload(false);
}, 2000000);
}
}
console.log(letterCount);
//
for (let c = 0; c < width; c++) {
let currTile = document.getElementById(row.toString() + '-' + c.toString());
let letter = currTile.innerText;
//
if (!currTile.classList.contains("correct")) {
//
if (word.includes(letter) && letterCount[letter] > 0) {
currTile.classList.add("present");
let keyTile = document.getElementById("Key" + letter);
if (!keyTile.classList.contains("correct")) {
keyTile.classList.add("present");
}
letterCount[letter] -= 1;
} //
else {
currTile.classList.add("absent");
let keyTile = document.getElementById("Key" + letter);
keyTile.classList.add("absent")
}
}
}
row += 1; //
col = 0; //
}
@import url('https://fonts.googleapis.com/css2?family=Bitter&display=swap');
body {
font-family: 'Bitter', serif;
text-align: center;
}
hr {
width: 600px;
}
#title {
font-size: 36px;
font-weight: bold;
letter-spacing: 2px;
}
#board {
width: 350px;
height: 400px;
margin: 0 auto;
display: flex;
flex-wrap: wrap;
}
.tile {
/* Box */
border: 2px solid lightgray;
width: 60px;
height: 60px;
margin: 2.5px;
border-radius: 15px;
/* Text */
color: black;
font-size: 30px;
font-weight: bold;
display: flex;
justify-content: center;
align-items: center;
}
.correct {
background-color: #1ed760;
color: white;
border-color: white;
}
.present {
background-color: #f9ba04;
color: white;
border-color: white;
}
.absent {
background-color: #777e99;
color: white;
border-color: white;
}
.keyboard-row {
width: 400px;
margin: 0 auto;
display: flex;
flex-wrap: wrap;
}
.key-tile {
width: 36px;
height: 40px;
margin: 1px;
border: 1px solid lightgray;
border-radius: 15px;
cursor: pointer;
/* Text */
font-size: 20px;
font-weight: bold;
display: flex;
justify-content: center;
align-items: center;
}
.key-tile:hover {
transform: scale(1.1);
transition: 100ms linear;
}
.enter-key-tile {
width: 76px;
height: 40px;
margin: 1px;
border: 1px solid lightgray;
border-radius: 15px;
cursor: pointer;
/* Text */
font-size: 20px;
font-weight: bold;
display: flex;
justify-content: center;
align-items: center;
}
.enter-key-tile:hover {
transform: scale(1.1);
transition: 100ms linear;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wordle Game</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<main>
<h1 id="title">Wordle Game</h1>
<hr>
<br>
<div id="board">
</div>
<br>
<h1 id="answer"></h1>
</main>
</body>
</html>