undo and undo for undo

This commit is contained in:
unknown 2024-05-14 15:48:42 +03:00
parent 026e254aca
commit dde13cbdca
3 changed files with 187 additions and 120 deletions

View File

@ -34,68 +34,73 @@ export default {
minesweeperGrid: null,
numberOfMines: 0,
item: null,
undoStack: [],
redoStack: [],
};
},
name: "Board",
// props: { numberOfRows: Number, numberOfCols: Number },
methods: {
isCellRevealed(row, col) {
let cell;
if (
this.minesweeperGrid &&
this.minesweeperGrid.board &&
this.minesweeperGrid.board.board &&
this.minesweeperGrid.board.board[row] &&
row < this.localNumberOfRows &&
col < this.localNumberOfCols
) {
cell = this.minesweeperGrid.board.board[row][col];
if (cell) {
methods: {
isCellRevealed(row, col) {
let cell;
if (
this.minesweeperGrid &&
this.minesweeperGrid.board &&
this.minesweeperGrid.board.board &&
this.minesweeperGrid.board.board[row] &&
row < this.localNumberOfRows &&
col < this.localNumberOfCols
) {
cell = this.minesweeperGrid.board.board[row][col];
if (cell) {
return cell.isRevealed;
}
}
return false; // return false when minesweeperGrid is null or the cell doesn't exist
},
isCellFlagged(row, col) {
}
}
return false; // return false when minesweeperGrid is null or the cell doesn't exist
},
isCellFlagged(row, col) {
this.play = new Play();
this.play.doFlagged(row, col);
this.minesweeperGrid = JSON.parse(
localStorage.getItem("minesweeperGrid")
);
this.getCellSymbol(row, col);
},
},
cellOpener(row, col) {
if (
this.minesweeperGrid &&
this.minesweeperGrid.board &&
this.minesweeperGrid.board.board &&
this.minesweeperGrid.board.board[row] &&
this.minesweeperGrid.board.board[row][col] &&
!this.minesweeperGrid.board.board[row][col].isFlagged
) {
this.play = new Play();
if (!this.minesweeperGrid.board.board[row][col].isMine) {
cellOpener(row, col) {
if (
this.minesweeperGrid &&
this.minesweeperGrid.board &&
this.minesweeperGrid.board.board &&
this.minesweeperGrid.board.board[row] &&
this.minesweeperGrid.board.board[row][col] &&
!this.minesweeperGrid.board.board[row][col].isFlagged
) {
this.play = new Play();
if (!this.minesweeperGrid.board.board[row][col].isMine) {
this.undoStack.push(JSON.parse(JSON.stringify(this.minesweeperGrid)));
this.minesweeperGrid.board.numberToWin = JSON.parse(
localStorage.getItem("numberToWin")
);
if (this.minesweeperGrid.board.numberToWin > 1) {
// this.minesweeperGrid.board.numberToWin =this.$store.state.numberToWin;
if (this.minesweeperGrid.board.numberToWin > 1) {
this.play.onClick(row, col);
} else {
Swal.fire({
}else {
Swal.fire({
title: "you win",
text: "Do you want to continue",
icon: "success",
showCancelButton: true,
confirmButtonText: "replay",
cancelButtonText: "Do nothing",
}).then((result) => {
}).then((result) => {
if (result.isConfirmed) {
this.$router.push("/")
.then(() => window.location.reload())}
});
}
} else {
});
}
} else {
Swal.fire({
title: "you lose",
text: "Do you want to continue",
@ -108,14 +113,15 @@ export default {
this.$router.push("/")
.then(() => window.location.reload())}
});
}
this.minesweeperGrid = JSON.parse(
}
this.minesweeperGrid = JSON.parse(
localStorage.getItem("minesweeperGrid")
);
this.getCellSymbol(row, col);
}
},
saver() {
);
this.getCellSymbol(row, col);
this.redoStack = [];
}
},
saver() {
this.$store.dispatch("writeOnFile", {
name: "yazan",
board: this.minesweeperGrid,
@ -128,8 +134,8 @@ export default {
// console.log(this.localNumberOfRows);
// console.log(this.localNumberOfCols);
// console.log(this.numberOfMines);
},
getCellSymbol(row, col) {
},
getCellSymbol(row, col) {
if (
this.minesweeperGrid &&
this.minesweeperGrid.board &&
@ -150,10 +156,58 @@ export default {
}
}
return "#"; // return "#" when minesweeperGrid is null or the cell doesn't exist
},
undo() {
if (this.undoStack.length > 0) {
// Save the current state to the redo stack before undoing the move
this.redoStack.push(JSON.parse(JSON.stringify(this.minesweeperGrid)));
// Undo the move
this.minesweeperGrid = this.undoStack.pop();
localStorage.setItem(
"minesweeperGrid",
JSON.stringify(this.minesweeperGrid)
);
this.$store.state.numberToWin =this.minesweeperGrid.board.numberToWin
// localStorage.setItem(
// "numberToWin",
// JSON.stringify(this.minesweeperGrid.board.numberToWin)
// );
}
},
redo() {
if (this.redoStack.length > 0) {
// save current state to undo stack
this.undoStack.push(this.minesweeperGrid);
// redo the move
this.minesweeperGrid = this.redoStack.pop();
localStorage.setItem(
"minesweeperGrid",
JSON.stringify(this.minesweeperGrid)
);
this.$store.state.numberToWin =this.minesweeperGrid.board.numberToWin
// localStorage.setItem(
// "numberToWin",
// JSON.stringify(this.minesweeperGrid.board.numberToWin)
// );
}
},
keydownHandler(event) {
if (event.ctrlKey && event.key === 'z') {
this.undo();
}
if (event.ctrlKey && event.key === 'y') {
this.redo();
}
},
mounted() {
},
mounted() {
this.$nextTick(() => {
window.addEventListener('keydown', this.keydownHandler);
this.localNumberOfRows = this.$store.state.numberOfRows;
this.localNumberOfCols = this.$store.state.numberOfCols;
this.numberOfMines = this.$store.state.numberOfMines;
@ -188,6 +242,9 @@ export default {
return this.minesweeperGrid;
});
},
beforeDestroy() {
window.removeEventListener('keydown', this.keydownHandler);
},
};
</script>
<!-- eslint-disable -->

View File

@ -1,4 +1,6 @@
/*eslint-disable*/
import { state } from "vuex";
// const readline = require("readline");
// var fs = require("fs");
// module.exports = {

View File

@ -185,6 +185,14 @@ export default {
this.numberToWin = parseInt(localStorage.getItem("numberToWin"));
});
},
// created() {
// window.addEventListener('keydown', this.keydownHandler);
// },
// beforeDestroy() {
// window.removeEventListener('keydown', this.keydownHandler);
// },
};
</script>
<!-- eslint-disable -->