Skip to content
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
48 changes: 47 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,52 @@ const reset_board = () => {
mute_sound_btn.parentNode.removeChild(mute_sound_btn); //delete the button when reseting the board
}

// Add this function to the existing JavaScript code
function undoMove() {
// Get the last move's position from the player_moves array
const lastMovePosition = player_moves.pop();

// If there is a move to undo
if (lastMovePosition) {
// Remove the X mark from the board
document.getElementById(`block_${lastMovePosition}`).innerHTML = "";

// Enable the corresponding block for future moves
document.getElementById(`block_${lastMovePosition}`).classList.remove("disabled");

// Switch the turn back to the player
player_turn = true;
}
}

// Inside the existing play() function
function play(clicked_id) {
// Check if the block is empty and the game is not over
if (document.getElementById(clicked_id).innerHTML === "" && !game_over) {
if (player_turn) {
// Update the block with an X
document.getElementById(clicked_id).innerHTML = "X";
// Disable the block to prevent further moves in the same position
document.getElementById(clicked_id).classList.add("disabled");
// Add the player's move to the player_moves array
player_moves.push(parseInt(clicked_id.split("_")[1]));
// Check for a win or draw
checkWinOrDraw("X");
} else {
// Update the block with an O
document.getElementById(clicked_id).innerHTML = "O";
// Disable the block to prevent further moves in the same position
document.getElementById(clicked_id).classList.add("disabled");
// Check for a win or draw
checkWinOrDraw("O");
}

// Switch the turn for the next move
player_turn = !player_turn;
}
}


//document.getElementsByClassName("playerstat1").innerText = playerstat1;
//document.getElementsByClassName("computerstat").innerText = computerstat1;

Expand Down Expand Up @@ -332,4 +378,4 @@ button.addEventListener("click", function() {
document.body.style.backgroundColor = "white";
}
});
*/
*/