Skip to content

Commit

Permalink
Create script.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Roam45 authored Nov 1, 2024
1 parent be6ba51 commit 1557b94
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
let score = 0;
let timeLeft = 60;
let timerInterval;
const wordSet = new Set();
const gridElement = document.getElementById('grid');
const scoreElement = document.getElementById('score');
const timerElement = document.getElementById('timer');
const messageElement = document.getElementById('message');
const wordInput = document.getElementById('wordInput');
const submitButton = document.getElementById('submitWord');

// Create a random letter grid
function generateGrid() {
gridElement.innerHTML = '';
const letters = Array.from({ length: 16 }, () => String.fromCharCode(Math.floor(Math.random() * 26) + 97));

letters.forEach(letter => {
const cell = document.createElement('div');
cell.className = 'cell';
cell.textContent = letter.toUpperCase();
gridElement.appendChild(cell);
});
}

// Start the game
function startGame() {
generateGrid();
score = 0;
timeLeft = 60;
wordSet.clear();
scoreElement.textContent = `Score: ${score}`;
timerElement.textContent = `Time: ${timeLeft}`;
messageElement.textContent = '';

timerInterval = setInterval(() => {
timeLeft--;
timerElement.textContent = `Time: ${timeLeft}`;

if (timeLeft <= 0) {
clearInterval(timerInterval);
messageElement.textContent = 'Time\'s up! Final Score: ' + score;
}
}, 1000);
}

// Check the submitted word
function checkWord() {
const word = wordInput.value.toLowerCase();

if (word.length < 3 || wordSet.has(word)) {
messageElement.textContent = 'Invalid word or already used!';
wordInput.value = '';
return;
}

// Check if the word can be formed from the grid letters (basic implementation)
const gridLetters = Array.from(gridElement.childNodes).map(cell => cell.textContent.toLowerCase());
if (word.split('').every(letter => gridLetters.includes(letter))) {
score += word.length;
wordSet.add(word);
scoreElement.textContent = `Score: ${score}`;
wordInput.value = '';
messageElement.textContent = 'Word accepted!';
} else {
messageElement.textContent = 'Word not found in grid!';
}
}

// Event listeners
submitButton.addEventListener('click', checkWord);
window.addEventListener('load', startGame);

0 comments on commit 1557b94

Please sign in to comment.