Changed around line 1
+ const wordList = new Set([/* Your provided word list here */]);
+ const wordInput = document.getElementById('wordInput');
+ const submitButton = document.getElementById('submitWord');
+ const message = document.getElementById('message');
+ const chainContainer = document.getElementById('chainContainer');
+ const playerScoreElement = document.getElementById('playerScore');
+ const computerScoreElement = document.getElementById('computerScore');
+ const newGameButton = document.getElementById('newGame');
+ const rulesButton = document.getElementById('rules');
+ const rulesDialog = document.getElementById('rulesDialog');
+ const closeRulesButton = document.getElementById('closeRules');
+
+ let lastWord = '';
+ let playerScore = 0;
+ let computerScore = 0;
+ let gameActive = true;
+
+ function initializeGame() {
+ lastWord = '';
+ playerScore = 0;
+ computerScore = 0;
+ gameActive = true;
+ chainContainer.innerHTML = '';
+ updateScores();
+ showMessage('Game started! Enter any word to begin.', 'success');
+ }
+
+ function updateScores() {
+ playerScoreElement.textContent = playerScore;
+ computerScoreElement.textContent = computerScore;
+ }
+
+ function showMessage(text, type) {
+ message.textContent = text;
+ message.className = `message ${type}`;
+ }
+
+ function addWordToChain(word, isPlayer) {
+ const wordElement = document.createElement('div');
+ wordElement.textContent = word;
+ wordElement.className = `chain-word ${isPlayer ? 'player' : 'computer'}`;
+ chainContainer.appendChild(wordElement);
+ chainContainer.scrollTop = chainContainer.scrollHeight;
+ }
+
+ function isValidWord(word) {
+ return wordList.has(word.toLowerCase()) &&
+ (!lastWord || word.charAt(0).toLowerCase() === lastWord.charAt(lastWord.length - 1).toLowerCase());
+ }
+
+ function computerTurn() {
+ const lastLetter = lastWord.charAt(lastWord.length - 1).toLowerCase();
+ const possibleWords = Array.from(wordList).filter(word =>
+ word.charAt(0).toLowerCase() === lastLetter
+ );
+
+ if (possibleWords.length === 0) {
+ gameActive = false;
+ showMessage("Computer can't find a word. You win!", 'success');
+ playerScore++;
+ updateScores();
+ return;
+ }
+
+ const computerWord = possibleWords[Math.floor(Math.random() * possibleWords.length)];
+ lastWord = computerWord;
+ addWordToChain(computerWord, false);
+ computerScore++;
+ updateScores();
+ }
+
+ submitButton.addEventListener('click', () => {
+ if (!gameActive) return;
+
+ const word = wordInput.value.trim();
+ if (!word) {
+ showMessage('Please enter a word', 'error');
+ return;
+ }
+
+ if (!isValidWord(word)) {
+ gameActive = false;
+ showMessage('Invalid word! Game Over.', 'error');
+ computerScore++;
+ updateScores();
+ return;
+ }
+
+ lastWord = word;
+ addWordToChain(word, true);
+ playerScore++;
+ updateScores();
+ wordInput.value = '';
+
+ setTimeout(computerTurn, 1000);
+ });
+
+ wordInput.addEventListener('keypress', (e) => {
+ if (e.key === 'Enter') {
+ submitButton.click();
+ }
+ });
+
+ newGameButton.addEventListener('click', initializeGame);
+
+ rulesButton.addEventListener('click', () => {
+ rulesDialog.showModal();
+ });
+
+ closeRulesButton.addEventListener('click', () => {
+ rulesDialog.close();
+ });
+
+ // Initialize game on load
+ initializeGame();