undo and undo for undo
This commit is contained in:
parent
026e254aca
commit
dde13cbdca
|
|
@ -34,132 +34,186 @@ export default {
|
||||||
minesweeperGrid: null,
|
minesweeperGrid: null,
|
||||||
numberOfMines: 0,
|
numberOfMines: 0,
|
||||||
item: null,
|
item: null,
|
||||||
|
undoStack: [],
|
||||||
|
redoStack: [],
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
name: "Board",
|
name: "Board",
|
||||||
// props: { numberOfRows: Number, numberOfCols: Number },
|
// props: { numberOfRows: Number, numberOfCols: Number },
|
||||||
methods: {
|
methods: {
|
||||||
isCellRevealed(row, col) {
|
isCellRevealed(row, col) {
|
||||||
let cell;
|
let cell;
|
||||||
if (
|
if (
|
||||||
this.minesweeperGrid &&
|
this.minesweeperGrid &&
|
||||||
this.minesweeperGrid.board &&
|
this.minesweeperGrid.board &&
|
||||||
this.minesweeperGrid.board.board &&
|
this.minesweeperGrid.board.board &&
|
||||||
this.minesweeperGrid.board.board[row] &&
|
this.minesweeperGrid.board.board[row] &&
|
||||||
row < this.localNumberOfRows &&
|
row < this.localNumberOfRows &&
|
||||||
col < this.localNumberOfCols
|
col < this.localNumberOfCols
|
||||||
) {
|
) {
|
||||||
cell = this.minesweeperGrid.board.board[row][col];
|
cell = this.minesweeperGrid.board.board[row][col];
|
||||||
if (cell) {
|
if (cell) {
|
||||||
return cell.isRevealed;
|
return cell.isRevealed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false; // return false when minesweeperGrid is null or the cell doesn't exist
|
return false; // return false when minesweeperGrid is null or the cell doesn't exist
|
||||||
},
|
},
|
||||||
isCellFlagged(row, col) {
|
isCellFlagged(row, col) {
|
||||||
this.play = new Play();
|
this.play = new Play();
|
||||||
this.play.doFlagged(row, col);
|
this.play.doFlagged(row, col);
|
||||||
this.minesweeperGrid = JSON.parse(
|
this.minesweeperGrid = JSON.parse(
|
||||||
localStorage.getItem("minesweeperGrid")
|
localStorage.getItem("minesweeperGrid")
|
||||||
);
|
);
|
||||||
this.getCellSymbol(row, col);
|
this.getCellSymbol(row, col);
|
||||||
},
|
},
|
||||||
|
|
||||||
cellOpener(row, col) {
|
cellOpener(row, col) {
|
||||||
if (
|
if (
|
||||||
this.minesweeperGrid &&
|
this.minesweeperGrid &&
|
||||||
this.minesweeperGrid.board &&
|
this.minesweeperGrid.board &&
|
||||||
this.minesweeperGrid.board.board &&
|
this.minesweeperGrid.board.board &&
|
||||||
this.minesweeperGrid.board.board[row] &&
|
this.minesweeperGrid.board.board[row] &&
|
||||||
this.minesweeperGrid.board.board[row][col] &&
|
this.minesweeperGrid.board.board[row][col] &&
|
||||||
!this.minesweeperGrid.board.board[row][col].isFlagged
|
!this.minesweeperGrid.board.board[row][col].isFlagged
|
||||||
) {
|
) {
|
||||||
this.play = new Play();
|
|
||||||
if (!this.minesweeperGrid.board.board[row][col].isMine) {
|
this.play = new Play();
|
||||||
this.minesweeperGrid.board.numberToWin = JSON.parse(
|
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")
|
localStorage.getItem("numberToWin")
|
||||||
);
|
);
|
||||||
if (this.minesweeperGrid.board.numberToWin > 1) {
|
// this.minesweeperGrid.board.numberToWin =this.$store.state.numberToWin;
|
||||||
this.play.onClick(row, col);
|
if (this.minesweeperGrid.board.numberToWin > 1) {
|
||||||
} else {
|
this.play.onClick(row, col);
|
||||||
Swal.fire({
|
}else {
|
||||||
title: "you win",
|
Swal.fire({
|
||||||
text: "Do you want to continue",
|
title: "you win",
|
||||||
icon: "success",
|
text: "Do you want to continue",
|
||||||
showCancelButton: true,
|
icon: "success",
|
||||||
confirmButtonText: "replay",
|
showCancelButton: true,
|
||||||
cancelButtonText: "Do nothing",
|
confirmButtonText: "replay",
|
||||||
}).then((result) => {
|
cancelButtonText: "Do nothing",
|
||||||
if (result.isConfirmed) {
|
}).then((result) => {
|
||||||
this.$router.push("/")
|
if (result.isConfirmed) {
|
||||||
.then(() => window.location.reload())}
|
this.$router.push("/")
|
||||||
});
|
.then(() => window.location.reload())}
|
||||||
}
|
});
|
||||||
} else {
|
}
|
||||||
Swal.fire({
|
} else {
|
||||||
title: "you lose",
|
Swal.fire({
|
||||||
text: "Do you want to continue",
|
title: "you lose",
|
||||||
icon: "error",
|
text: "Do you want to continue",
|
||||||
showCancelButton: true,
|
icon: "error",
|
||||||
confirmButtonText: "replay",
|
showCancelButton: true,
|
||||||
cancelButtonText: "Do nothing",
|
confirmButtonText: "replay",
|
||||||
}).then((result) => {
|
cancelButtonText: "Do nothing",
|
||||||
if (result.isConfirmed) {
|
}).then((result) => {
|
||||||
this.$router.push("/")
|
if (result.isConfirmed) {
|
||||||
.then(() => window.location.reload())}
|
this.$router.push("/")
|
||||||
});
|
.then(() => window.location.reload())}
|
||||||
}
|
});
|
||||||
this.minesweeperGrid = JSON.parse(
|
}
|
||||||
localStorage.getItem("minesweeperGrid")
|
this.minesweeperGrid = JSON.parse(
|
||||||
);
|
localStorage.getItem("minesweeperGrid")
|
||||||
this.getCellSymbol(row, col);
|
);
|
||||||
|
this.getCellSymbol(row, col);
|
||||||
|
this.redoStack = [];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
saver() {
|
||||||
|
this.$store.dispatch("writeOnFile", {
|
||||||
|
name: "yazan",
|
||||||
|
board: this.minesweeperGrid,
|
||||||
|
rows: this.localNumberOfRows,
|
||||||
|
cols: this.localNumberOfCols,
|
||||||
|
mines: this.numberOfMines,
|
||||||
|
numberToWin: this.minesweeperGrid.board.numberToWin,
|
||||||
|
});
|
||||||
|
// console.log(this.minesweeperGrid.board.numberToWin);
|
||||||
|
// console.log(this.localNumberOfRows);
|
||||||
|
// console.log(this.localNumberOfCols);
|
||||||
|
// console.log(this.numberOfMines);
|
||||||
|
},
|
||||||
|
getCellSymbol(row, col) {
|
||||||
|
if (
|
||||||
|
this.minesweeperGrid &&
|
||||||
|
this.minesweeperGrid.board &&
|
||||||
|
this.minesweeperGrid.board.board &&
|
||||||
|
this.minesweeperGrid.board.board[row] &&
|
||||||
|
row < this.localNumberOfRows &&
|
||||||
|
col < this.localNumberOfCols
|
||||||
|
) {
|
||||||
|
let cell = this.minesweeperGrid.board.board[row][col];
|
||||||
|
if (cell) {
|
||||||
|
if (cell.isFlagged) {
|
||||||
|
return "F";
|
||||||
|
} else if (cell.isRevealed === true) {
|
||||||
|
return String(cell.nearbyMines); // Convert to string before returning
|
||||||
|
} else if (!cell.isRevealed) {
|
||||||
|
return "#";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
// );
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
saver() {
|
keydownHandler(event) {
|
||||||
this.$store.dispatch("writeOnFile", {
|
if (event.ctrlKey && event.key === 'z') {
|
||||||
name: "yazan",
|
this.undo();
|
||||||
board: this.minesweeperGrid,
|
}
|
||||||
rows: this.localNumberOfRows,
|
if (event.ctrlKey && event.key === 'y') {
|
||||||
cols: this.localNumberOfCols,
|
this.redo();
|
||||||
mines: this.numberOfMines,
|
|
||||||
numberToWin: this.minesweeperGrid.board.numberToWin,
|
|
||||||
});
|
|
||||||
// console.log(this.minesweeperGrid.board.numberToWin);
|
|
||||||
// console.log(this.localNumberOfRows);
|
|
||||||
// console.log(this.localNumberOfCols);
|
|
||||||
// console.log(this.numberOfMines);
|
|
||||||
},
|
|
||||||
getCellSymbol(row, col) {
|
|
||||||
if (
|
|
||||||
this.minesweeperGrid &&
|
|
||||||
this.minesweeperGrid.board &&
|
|
||||||
this.minesweeperGrid.board.board &&
|
|
||||||
this.minesweeperGrid.board.board[row] &&
|
|
||||||
row < this.localNumberOfRows &&
|
|
||||||
col < this.localNumberOfCols
|
|
||||||
) {
|
|
||||||
let cell = this.minesweeperGrid.board.board[row][col];
|
|
||||||
if (cell) {
|
|
||||||
if (cell.isFlagged) {
|
|
||||||
return "F";
|
|
||||||
} else if (cell.isRevealed === true) {
|
|
||||||
return String(cell.nearbyMines); // Convert to string before returning
|
|
||||||
} else if (!cell.isRevealed) {
|
|
||||||
return "#";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return "#"; // return "#" when minesweeperGrid is null or the cell doesn't exist
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
this.$nextTick(() => {
|
this.$nextTick(() => {
|
||||||
this.localNumberOfRows = this.$store.state.numberOfRows;
|
|
||||||
this.localNumberOfCols = this.$store.state.numberOfCols;
|
window.addEventListener('keydown', this.keydownHandler);
|
||||||
this.numberOfMines = this.$store.state.numberOfMines;
|
this.localNumberOfRows = this.$store.state.numberOfRows;
|
||||||
this.localNumberOfRows = parseInt(this.localNumberOfRows);
|
this.localNumberOfCols = this.$store.state.numberOfCols;
|
||||||
this.localNumberOfCols = parseInt(this.localNumberOfCols);
|
this.numberOfMines = this.$store.state.numberOfMines;
|
||||||
this.numberOfMines = parseInt(this.numberOfMines);
|
this.localNumberOfRows = parseInt(this.localNumberOfRows);
|
||||||
|
this.localNumberOfCols = parseInt(this.localNumberOfCols);
|
||||||
|
this.numberOfMines = parseInt(this.numberOfMines);
|
||||||
|
|
||||||
// console.log(this.localNumberOfRows," row");
|
// console.log(this.localNumberOfRows," row");
|
||||||
// console.log(this.localNumberOfCols," col");
|
// console.log(this.localNumberOfCols," col");
|
||||||
|
|
@ -182,12 +236,15 @@ export default {
|
||||||
JSON.stringify(this.minesweeperGrid)
|
JSON.stringify(this.minesweeperGrid)
|
||||||
);
|
);
|
||||||
localStorage.setItem(
|
localStorage.setItem(
|
||||||
"numberToWin",
|
"numberToWin",
|
||||||
JSON.stringify(this.minesweeperGrid.board.numberToWin)
|
JSON.stringify(this.minesweeperGrid.board.numberToWin)
|
||||||
);
|
);
|
||||||
return this.minesweeperGrid;
|
return this.minesweeperGrid;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
beforeDestroy() {
|
||||||
|
window.removeEventListener('keydown', this.keydownHandler);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
<!-- eslint-disable -->
|
<!-- eslint-disable -->
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
/*eslint-disable*/
|
/*eslint-disable*/
|
||||||
|
import { state } from "vuex";
|
||||||
|
|
||||||
// const readline = require("readline");
|
// const readline = require("readline");
|
||||||
// var fs = require("fs");
|
// var fs = require("fs");
|
||||||
// module.exports = {
|
// module.exports = {
|
||||||
|
|
|
||||||
|
|
@ -86,14 +86,14 @@ export default {
|
||||||
this.play = new Play();
|
this.play = new Play();
|
||||||
if (!this.minesweeperGrid.board.board[row][col].isMine) {
|
if (!this.minesweeperGrid.board.board[row][col].isMine) {
|
||||||
this.minesweeperGrid.board.numberToWin = JSON.parse(
|
this.minesweeperGrid.board.numberToWin = JSON.parse(
|
||||||
localStorage.getItem("numberToWin")
|
localStorage.getItem("numberToWin")
|
||||||
);
|
);
|
||||||
if (this.minesweeperGrid.board.numberToWin > 1) {
|
if (this.minesweeperGrid.board.numberToWin > 1) {
|
||||||
this.play.onClick(row, col);
|
this.play.onClick(row, col);
|
||||||
this.minesweeperGrid.board.numberToWin--;
|
this.minesweeperGrid.board.numberToWin--;
|
||||||
localStorage.setItem(
|
localStorage.setItem(
|
||||||
"numberToWin",
|
"numberToWin",
|
||||||
this.minesweeperGrid.board.numberToWin
|
this.minesweeperGrid.board.numberToWin
|
||||||
);
|
);
|
||||||
this.minesweeperGrid = JSON.parse(
|
this.minesweeperGrid = JSON.parse(
|
||||||
localStorage.getItem("minesweeperGrid")
|
localStorage.getItem("minesweeperGrid")
|
||||||
|
|
@ -185,6 +185,14 @@ export default {
|
||||||
this.numberToWin = parseInt(localStorage.getItem("numberToWin"));
|
this.numberToWin = parseInt(localStorage.getItem("numberToWin"));
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// created() {
|
||||||
|
// window.addEventListener('keydown', this.keydownHandler);
|
||||||
|
// },
|
||||||
|
// beforeDestroy() {
|
||||||
|
// window.removeEventListener('keydown', this.keydownHandler);
|
||||||
|
// },
|
||||||
|
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
<!-- eslint-disable -->
|
<!-- eslint-disable -->
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user