Question: Can you edit this code so the game runs smoothly? The game for some reason closes after asking to swap items? What is wrong with
Can you edit this code so the game runs smoothly? The game for some reason closes after asking to swap items? What is wrong with the code that is causing the game to do so?
*No HTML* *The Game should run from command prompt*
Existing code is below:
const prompt = require('prompt-sync')();
var adventurersName = ["Captain Thomas King", "George", "Tim", "Sarah", "Mike", "Edward"]; var inventory = ["Food", "Wine", "Horses", "Medicine"];
var adventurersKilled = 3; var survivors; var numberOfAdventurers = adventurersName.length;
survivors = numberOfAdventurers - adventurersKilled;
console.log("Welcome to The God Among Us "); console.log("A group of adventurers began their search for the mystical god said to live among us. In charge of the squad was " + adventurersName[0] + " who was famous for his past exploits. Along the way, the group of comrades were attacked by the god's loyal followers. The adventurers fought with bravado and strength under the tutelage of "+ adventurersName[0] + " the followers were defeated but they still suffered great losses. After a headcount of the remaining squad, "+ adventurersKilled +" were found to be dead which left only " + survivors + " remaining survivors. ");
console.log("Current Statistics : "); console.log("Total Adventurers = " + numberOfAdventurers); console.log("Adventures Names:"); for (var i = 0; i < numberOfAdventurers; i++) console.log("\t"+adventurersName[i]+", "); console.log("Total Killed = " + adventurersKilled); console.log("Total Survived = " + survivors);
var user_life = 3; var correct_answer = ["1","2","3"]; var userIsCorrect; var options = [" Option 1 Enter the village hut?","Option 2 Eat the turkey leg?","Option 3 Sit on the stool?","Option 4 Talk with the shadowy figure? "];
adventurersName[0] = prompt("Enter Leader Name: ");
while (user_life>0) { user_life = game(adventurersName[0],options);
if (user_life === 0) { var play_again = prompt("Do You Want Play Again (y/n)? ");
if (play_again =="y") { user_life = 3; adventurersName[0] = prompt("Enter Leader Name:"); } else { console.log("Game Ended.Bye!"); break; } } else{ var play_continue = prompt("Do You Want continue the Play(y/n)? "); if (play_continue == "n") { console.log("Game Ended.Bye!"); break; } } }
function game(leader,options) {
showOptions(options); userIsCorrect = 0; const input = prompt("Enter Your choice (Number) ");
for(var i=0;i if (userIsCorrect) { console.log("Yay, you picked the right option. After you are finished, you take a look at some of the items around you and compare them to the items you already have, would you like to pick up one of them?"); console.log("Your Inventory Items: "); for(var i=0; i < inventory; i++) { console.log(Inventory[i]+", "); } console.log(" Items To Pick Up: "); console.log("1.Food 2.Clothes 3.Horses 4.Weapons 5.Wine"); var item = prompt("Choose an item to pick up: "); var x = ["Food", "Clothes", "Horses","Weapons","Wine"]; if(item=="Food") item=x[0]; else if(item=="Clothes") item=x[1]; else if (item == "Horses" ) item =x[2]; else if (item == "Weapons") item =x[3]; else if (item == "Wine") item =x[4]; if(inventory.length == 5) { console.log("Your Inventory is full!"); var swap_option = prompt("Do you want to swap(y/n):"); if(swap_option == 'y') { console.log("Your Inventory Items: "); for(var i=0;i function showOptions(option) { for(let i=0;i<=3;i++){ console.log(option[i]); } } function displayStats(user_life,name) { console.log(" Current Statistics: Leader Name: ",name," Lives Remaining: ",user_life); console.log("Adventurers in your party = " + survivors); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
