**/ var observations = ''; var events = [ { label: "Start", text: "Your cell phone is ringing away. You are groggy and wondering who it could be.", choices: [ { choice_label: "Try to sleep", action: "handleChoice(0, 1)" }, { choice_label: "Pick up your phone or", action: "handleChoice(0, 0)" }, ] }, { text: "Your cell phone stops ringing. You hold your phone in your hand and see it's 9pm.", choices: [ { choice_label: "Try to sleep", action: "handleChoice(0, 1)" }, { choice_label: "Check your missed calls", action: "handleChoice(0, 0)" }, ] }, { text: "You wake up 5 hours later at 2pm. ", choices: [ { choice_label: "Pick up your phone and check your missed calls or", action: "handleChoice(0, 0)" }, { choice_label: "Go to the washroom", action: "handleChoice(0, 1)" } ] }, ]; var currentEvent = 0; // Function to handle a choice function handleChoice(eventIndex, choiceIndex) { // Handle the choice here... console.log('User chose option ' + choiceIndex + ' for event ' + eventIndex); } // Function to speak a text function speak(text) { observations += text + ' '; document.getElementById('heading').textContent = text; let msg = new SpeechSynthesisUtterance(text); window.speechSynthesis.speak(msg); } // Function to speak a choice function speak_choice(text, choices) { window.speechSynthesis.cancel(); let speak_msg = observations + ' ' + text; console.log(text); const outputDiv = document.getElementById('output'); outputDiv.textContent = observations + ' ' + text; const choicesDiv = document.getElementById('choices'); choicesDiv.innerHTML = ''; speak_msg += ' ' ; console.log(choices); choices.forEach(function(obj) { const actionLink = document.createElement('a'); actionLink.href = '#'; actionLink.textContent = obj.choice_label; actionLink.setAttribute('onclick', obj.action); choicesDiv.appendChild(actionLink); speak_msg += obj.choice_label + ' '; }); let msg = new SpeechSynthesisUtterance(speak_msg); window.speechSynthesis.speak(msg); observations = ''; } // Function to start the game function startGame() { speak_choice(events[currentEvent].text, events[currentEvent].choices); currentEvent++; setInterval(function() { if (currentEvent < events.length) { speak_choice(events[currentEvent].text, events[currentEvent].choices); currentEvent++; } }, 20000); // Every 15 seconds } // Start the game startGame();