Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

need help running the code through visual code studio making sure it runs right then through a github link so that others can play it

need help running the code through visual code studio making sure it runs right then through a github link so that others can play it and needs a logo
const easyWords =["star", "moon", "mars"];
const mediumWords =["rocket", "astronaut", "gravity"];
const hardWords =["constellation", "nebula", "blackhole"];
const difficultyLevels ={
easy: easyWords,
medium: mediumWords,
hard: hardWords,
};
let guessedLetters =[];
const maxWrongGuesses =5;
let wordToGuess;
let wrongGuesses;
let displayWord;
console.log("Welcome to Spaceman! Guess the space-themed word.");
function chooseDifficulty(){
let difficulty;
while (!difficulty){
difficulty = prompt("Choose difficulty (easy, medium, hard):").toLowerCase();
if (!difficultyLevels.hasOwnProperty(difficulty)){
console.error("Invalid difficulty. Please choose easy, medium, or hard.");
difficulty = null;
}
}
return difficulty;
}
function newGame(){
try {
const difficulty = chooseDifficulty();
const wordList = difficultyLevels[difficulty];
wordToGuess = wordList[Math.floor(Math.random()* wordList.length)];
guessedLetters =[];
wrongGuesses =0;
displayWord ="_".repeat(wordToGuess.length);
} catch (error){
console.error("Error starting a new game:", error);
} finally {
gameLoop();
}
}
function displayGameState(){
console.log("Word: "+ displayWord.split('').join(''));
console.log("Guessed Letters: "+ guessedLetters.join(','));
console.log("Wrong guesses left: "+(maxWrongGuesses - wrongGuesses));
}
function gameLoop(){
while (wrongGuesses maxWrongGuesses && displayWord.includes("_")){
displayGameState();
let guess;
try {
guess = prompt("Enter a letter to guess:").toLowerCase();
if (guess.length !==1||!guess.match(/[a-z]/i)){
throw new Error("Invalid guess. Please enter a single letter.");
}
} catch (error){
console.error(error.message);
continue;
}
if (guessedLetters.includes(guess)){
console.log("You already guessed that letter.");
continue;
}
guessedLetters.push(guess);
if (wordToGuess.includes(guess)){
for (let i =0; i wordToGuess.length; i++){
if (wordToGuess[i]=== guess){
displayWord = displayWord.substr(0, i)+ guess + displayWord.substr(i +1);
}
}
} else {
wrongGuesses++;
}
}
if (!displayWord.includes("_")){
console.log("Congratulations! You've guessed the word: "+ wordToGuess);
} else {
console.log("Game Over! The word was: "+ wordToGuess);
}
}
newGame();
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Watson Watson

5th Edition

0471715360, 978-0471715368

More Books

Students also viewed these Databases questions

Question

=+a. Write two different, but related, headlines.

Answered: 1 week ago