96 Word: Simplify use of arrays; add some comments

This commit is contained in:
Steve Bosman
2022-01-29 17:07:45 +00:00
parent 7a417de815
commit 4c49492449

View File

@@ -39,14 +39,11 @@ function tab(space)
return str;
}
const words = ["DINKY", "SMOKE", "WATER", "GLASS", "TRAIN",
// These are the words that the game knows about> If you want a bigger challenge you could add more words to the array
const WORDS = ["DINKY", "SMOKE", "WATER", "GLASS", "TRAIN",
"MIGHT", "FIRST", "CANDY", "CHAMP", "WOULD",
"CLUMP", "DOPEY"];
const secretWordDetails = [];
const knownLettersDetails = [];
const guessDetails = [];
const lettersInCommonDetails = [];
const WORD_COUNT = WORDS.length;
// Main control section
async function main()
@@ -60,91 +57,87 @@ async function main()
print("CLUES TO HELP YOU GET IT. GOOD LUCK!!\n");
print("\n");
print("\n");
while (1) {
outer: while (1) {
print("\n");
print("\n");
print("YOU ARE STARTING A NEW GAME...\n");
const n = words.length;
const secretWord = words[Math.floor(Math.random() * n)];
const secretWord = WORDS[Math.floor(Math.random() * WORD_COUNT)];
let guessCount = 0;
secretWordDetails[0] = secretWord.length;
for (let i = 1; i <= secretWord.length; i++)
secretWordDetails[i] = secretWord.charCodeAt(i - 1);
for (let i = 1; i <= 5; i++)
knownLettersDetails[i] = "-".charCodeAt(0);
for (let j = 1; j <= 5; j++)
lettersInCommonDetails[j] = 0;
// This array holds the letters which have been found in the correct position across all guesses
// For instance if the word is "PLAIN" and the guesses so far are
// "SHALL" ("A" correct) and "CLIMB" ("L" correct) then it will hold "-LA--"
const knownLetters = [];
for (let i = 0; i < 5; i++)
knownLetters[i] = "-";
let guess = undefined;
while (1) {
print("GUESS A FIVE LETTER WORD");
guess = (await input()).toUpperCase();
guessCount++;
if (secretWord === guess)
if (secretWord === guess) {
// The player has guessed correctly
break;
for (let i = 1; i <= 7; i++)
lettersInCommonDetails[i] = 0;
// store detail about the guess
guessDetails[0] = guess.length;
for (let i = 1; i <= guess.length; i++) {
guessDetails[i] = guess.charCodeAt(i - 1);
}
if (guessDetails[1] === 63) {
if (guess.charAt(0) === "?") {
// Player has given up
print("THE SECRET WORD IS " + secretWord + "\n");
print("\n");
break;
// Start a new game by going to the start of the outer while loop
continue outer;
}
if (guessDetails[0] !== 5) {
if (guess.length !== 5) {
print("YOU MUST GUESS A 5 LETTER WORD. START AGAIN.\n");
print("\n");
guessCount--;
continue;
}
// Two things happen in this double loop:
// 1. Letters which are in both the guessed and secret words are put in the lettersInCommon array
// 2. Letters which are in the correct position in the guessed word are added to the knownLetters array
let lettersInCommonCount = 0;
let nextCorrectLetterIndex = 1;
for (let i = 1; i <= 5; i++) {
for (let j = 1; j <= 5; j++) {
if (secretWordDetails[i] === guessDetails[j]) {
lettersInCommonDetails[nextCorrectLetterIndex] = guessDetails[j];
nextCorrectLetterIndex++;
if (i === j)
knownLettersDetails[j] = guessDetails[j];
const lettersInCommon = [];
for (let i = 0; i < 5; i++) {// loop round characters in secret word
let secretWordCharacter = secretWord.charAt(i);
for (let j = 0; j < 5; j++) {// loop round characters in guessed word
let guessedWordCharacter = guess.charAt(j);
if (secretWordCharacter === guessedWordCharacter) {
lettersInCommon[lettersInCommonCount] = guessedWordCharacter;
if (i === j) {
// Letter is in the exact position so add to the known letters array
knownLetters[j] = guessedWordCharacter;
}
lettersInCommonCount++;
}
}
}
knownLettersDetails[0] = 5;
lettersInCommonDetails[0] = lettersInCommonCount;
let lettersInCommonText = "";
for (let i = 1; i <= lettersInCommonDetails[0]; i++)
lettersInCommonText += String.fromCharCode(lettersInCommonDetails[i]);
const lettersInCommonText = lettersInCommon.join("");
print("THERE WERE " + lettersInCommonCount + " MATCHES AND THE COMMON LETTERS WERE... " + lettersInCommonText + "\n");
let knownLettersText = "";
for (let i = 1; i <= knownLettersDetails[0]; i++)
knownLettersText += String.fromCharCode(knownLettersDetails[i]);
const knownLettersText = knownLetters.join("");
print("FROM THE EXACT LETTER MATCHES, YOU KNOW............ " + knownLettersText + "\n");
if (knownLettersText === secretWord) {
guess = knownLettersText;
break;
}
if (lettersInCommonCount <= 1) {
print("\n");
print("IF YOU GIVE UP, TYPE '?' FOR YOUR NEXT GUESS.\n");
print("\n");
}
}
if (secretWord === guess) {
print("YOU HAVE GUESSED THE WORD. IT TOOK " + guessCount + " GUESSES!\n");
print("\n");
} else {
continue;
}
print("WANT TO PLAY AGAIN");
const playAgainResponse = (await input()).toUpperCase();
if (playAgainResponse !== "YES")