Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update script.js #833

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion Chess-Game/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,7 @@ $(document).ready(function () {
main.variables.highlighted.length = 0;

main.variables.selectedpiece = target.id;
main.methods.moveoptions(target.name);
main.methods.moveoptions(target.name);

}

Expand All @@ -1724,6 +1724,62 @@ $(document).ready(function () {
e.preventDefault();
});


});

let whiteTime = 30; // Initial time for white player
let blackTime = 30; // Initial time for black player
let currentPlayer = 'white'; // Track the current player's turn
let timerInterval;

function startTimer() {
clearInterval(timerInterval); // Clear any existing timer
timerInterval = setInterval(() => {
if (currentPlayer === 'white') {
whiteTime--;
document.getElementById('white-time').innerText = whiteTime;
if (whiteTime <= 0) {
clearInterval(timerInterval);
alert('Time is up! Black wins this round!');
switchTurn(); // Switch to black player's turn
}
} else {
blackTime--;
document.getElementById('black-time').innerText = blackTime;
if (blackTime <= 0) {
clearInterval(timerInterval);
alert('Time is up! White wins this round!');
switchTurn(); // Switch to white player's turn
}
}
}, 1000); // Call the function every second
}

function switchTurn() {
clearInterval(timerInterval); // Stop the current player's timer
currentPlayer = currentPlayer === 'white' ? 'black' : 'white'; // Switch player
resetCurrentPlayerTime(); // Reset the current player's time
startTimer(); // Start the timer for the next player
}

function resetCurrentPlayerTime() {
if (currentPlayer === 'white') {
whiteTime = 30; // Reset white time
} else {
blackTime = 30; // Reset black time
}
document.getElementById(currentPlayer + '-time').innerText = 30; // Update displayed time
}

// Start the timer on page load
startTimer();

// Example usage of switching turns when a move is made
document.querySelectorAll('.gamecell').forEach(cell => {
cell.addEventListener('click', () => {
// Logic for making a move can go here

// Switch turns after a move
switchTurn();
});
});