Changed around line 1
+ const wordList = ['above', 'active', 'afraid', 'after', 'ago', 'Alaska', 'all', 'also', 'always', 'amazing', 'American', 'and', 'angry', 'animal', 'ant', 'any', 'anything', 'apple', 'April', "aren't", 'arm', 'art', 'room', 'ask', 'at', 'ate', 'attention', 'August', 'aunt', 'autumn', 'baby', 'bad', 'badminton', 'bag', 'ball', 'bamboo', 'banana', 'basket', 'basketball', 'bathroom', 'be', 'careful', 'beach', 'bear', 'beautiful', 'because', 'become', 'bed', 'bedroom', 'beef', 'before', 'behind', 'beside', 'best', 'better', 'between', 'big', 'bigger', 'bike', 'bird', 'black', 'blackboard', 'blue', 'boat', 'boating', 'body', 'book', 'bookstore', 'both', 'bottle', 'bought', 'bowl', 'box', 'boy', 'bread', 'breakfast', 'breath', 'bridge', 'broken', 'brother', 'brown', 'building', 'bunny', 'bus', 'businessman', 'busy', 'but', 'buy', 'by', 'cake', 'camp', 'Canada', 'Canadian', 'Canberra', 'candy', 'cap', 'car', 'carrot', 'cartoon', 'cat', 'cave', 'chair', 'chase', 'cheap', 'cheetah', 'chicken', 'children', 'China', 'Chinese', 'book', 'chocolate', 'chopsticks', 'Christmas', 'cinema', 'class', 'classroom', 'clean', 'clean my room', 'cleaned', 'clever', 'climbing', 'clock', 'clothes', 'cloudy', 'club', 'coach', 'coat', 'cold', 'come on', 'comic', 'comic book', 'computer', 'computer room', 'contest', 'cook'];
+
+ class WordChainGame {
+ constructor() {
+ this.usedWords = new Set();
+ this.playerScore = 0;
+ this.computerScore = 0;
+ this.lastWord = '';
+ }
+
+ isValidWord(word) {
+ return wordList.includes(word.toLowerCase()) && !this.usedWords.has(word.toLowerCase());
+ }
+
+ checkChain(word) {
+ if (!this.lastWord) return true;
+ return word.charAt(0).toLowerCase() === this.lastWord.charAt(this.lastWord.length - 1).toLowerCase();
+ }
+
+ computerTurn() {
+ const lastLetter = this.lastWord.charAt(this.lastWord.length - 1).toLowerCase();
+ const possibleWords = wordList.filter(word =>
+ word.charAt(0).toLowerCase() === lastLetter &&
+ !this.usedWords.has(word.toLowerCase())
+ );
+
+ if (possibleWords.length === 0) return null;
+ return possibleWords[Math.floor(Math.random() * possibleWords.length)];
+ }
+ }
+
+ let game = new WordChainGame();
+
+ document.addEventListener('DOMContentLoaded', () => {
+ const input = document.getElementById('playerInput');
+ const submitBtn = document.getElementById('submitWord');
+ const message = document.getElementById('message');
+ const wordList = document.getElementById('wordList');
+ const newGameBtn = document.getElementById('newGame');
+ const rulesBtn = document.getElementById('rules');
+ const rulesModal = document.getElementById('rulesModal');
+ const closeRulesBtn = document.getElementById('closeRules');
+
+ function updateScores() {
+ document.getElementById('playerScore').textContent = game.playerScore;
+ document.getElementById('computerScore').textContent = game.computerScore;
+ }
+
+ function addWordToHistory(word, isComputer = false) {
+ const li = document.createElement('li');
+ li.textContent = `${isComputer ? '🤖' : '👤'} ${word}`;
+ wordList.insertBefore(li, wordList.firstChild);
+ }
+
+ function handlePlayerTurn() {
+ const word = input.value.trim().toLowerCase();
+
+ if (!word) {
+ message.textContent = "Please enter a word!";
+ return;
+ }
+
+ if (!game.isValidWord(word)) {
+ message.textContent = "Invalid word or already used!";
+ return;
+ }
+
+ if (game.lastWord && !game.checkChain(word)) {
+ message.textContent = `Word must start with '${game.lastWord.charAt(game.lastWord.length - 1)}'!`;
+ return;
+ }
+
+ game.usedWords.add(word);
+ game.lastWord = word;
+ addWordToHistory(word);
+ game.playerScore++;
+ updateScores();
+ input.value = '';
+
+ // Computer's turn
+ const computerWord = game.computerTurn();
+ if (!computerWord) {
+ message.textContent = "Computer can't find a word. You win!";
+ game.playerScore += 3;
+ updateScores();
+ return;
+ }
+
+ game.usedWords.add(computerWord);
+ game.lastWord = computerWord;
+ addWordToHistory(computerWord, true);
+ game.computerScore++;
+ updateScores();
+ message.textContent = `Computer played: ${computerWord}`;
+ }
+
+ submitBtn.addEventListener('click', handlePlayerTurn);
+ input.addEventListener('keypress', (e) => {
+ if (e.key === 'Enter') handlePlayerTurn();
+ });
+
+ newGameBtn.addEventListener('click', () => {
+ game = new WordChainGame();
+ wordList.innerHTML = '';
+ message.textContent = '';
+ input.value = '';
+ updateScores();
+ });
+
+ rulesBtn.addEventListener('click', () => rulesModal.showModal());
+ closeRulesBtn.addEventListener('click', () => rulesModal.close());
+ });