Changed around line 1
+ const wordList = new Set(['above', 'active', 'afraid', /* ... full word list ... */]);
+ let gameActive = false;
+ let lastWord = '';
+
+ const playerInput = document.getElementById('playerInput');
+ const submitButton = document.getElementById('submitWord');
+ const wordChain = document.getElementById('wordChain');
+ const gameMessage = document.getElementById('gameMessage');
+ const playerScore = document.getElementById('playerScore');
+ const computerScore = document.getElementById('computerScore');
+ const newGameButton = document.getElementById('newGame');
+ const rulesButton = document.getElementById('rules');
+ const rulesDialog = document.getElementById('rulesDialog');
+
+ function initGame() {
+ gameActive = true;
+ lastWord = '';
+ wordChain.innerHTML = '';
+ gameMessage.textContent = 'Start by entering any valid word!';
+ playerInput.value = '';
+ playerScore.textContent = '0';
+ computerScore.textContent = '0';
+ }
+
+ function isValidWord(word) {
+ return wordList.has(word.toLowerCase());
+ }
+
+ function addWordToChain(word, isPlayer) {
+ const wordSpan = document.createElement('span');
+ wordSpan.textContent = word;
+ wordSpan.classList.add(isPlayer ? 'player-word' : 'computer-word');
+ wordChain.appendChild(wordSpan);
+ lastWord = word;
+ }
+
+ function computerTurn() {
+ const lastLetter = lastWord.slice(-1).toLowerCase();
+ const possibleWords = Array.from(wordList).filter(word =>
+ word.toLowerCase().startsWith(lastLetter) && word.toLowerCase() !== lastWord.toLowerCase()
+ );
+
+ if (possibleWords.length === 0) {
+ gameMessage.textContent = 'Computer cannot find a word. You win!';
+ gameActive = false;
+ playerScore.textContent = (parseInt(playerScore.textContent) + 1).toString();
+ return;
+ }
+
+ const computerWord = possibleWords[Math.floor(Math.random() * possibleWords.length)];
+ addWordToChain(computerWord, false);
+ computerScore.textContent = (parseInt(computerScore.textContent) + 1).toString();
+ }
+
+ submitButton.addEventListener('click', () => {
+ if (!gameActive) return;
+
+ const word = playerInput.value.trim();
+
+ if (!isValidWord(word)) {
+ gameMessage.textContent = 'Invalid word! Game Over!';
+ gameActive = false;
+ computerScore.textContent = (parseInt(computerScore.textContent) + 1).toString();
+ return;
+ }
+
+ if (lastWord && !word.toLowerCase().startsWith(lastWord.slice(-1).toLowerCase())) {
+ gameMessage.textContent = 'Word must start with the last letter of previous word! Game Over!';
+ gameActive = false;
+ computerScore.textContent = (parseInt(computerScore.textContent) + 1).toString();
+ return;
+ }
+
+ addWordToChain(word, true);
+ playerInput.value = '';
+ playerScore.textContent = (parseInt(playerScore.textContent) + 1).toString();
+
+ setTimeout(computerTurn, 1000);
+ });
+
+ playerInput.addEventListener('keypress', (e) => {
+ if (e.key === 'Enter') submitButton.click();
+ });
+
+ newGameButton.addEventListener('click', initGame);
+
+ rulesButton.addEventListener('click', () => {
+ rulesDialog.showModal();
+ });
+
+ document.querySelector('.close-dialog').addEventListener('click', () => {
+ rulesDialog.close();
+ });
+
+ initGame();