407 lines
12 KiB
JavaScript
407 lines
12 KiB
JavaScript
/*eslint-disable*/
|
|
import { state } from "vuex";
|
|
|
|
// const readline = require("readline");
|
|
// var fs = require("fs");
|
|
// module.exports = {
|
|
// resolve: {
|
|
// fallback: {
|
|
// fs: false,
|
|
// },
|
|
// },
|
|
// };
|
|
// const { start } = require("repl");
|
|
// const { log } = require("console");
|
|
// const rl = readline.createInterface({
|
|
// input: process.stdin,
|
|
// output: process.stdout,
|
|
// });
|
|
import minesweeperGrid from "./Board.vue";
|
|
|
|
class Cell {
|
|
constructor() {
|
|
this.isMine = false;
|
|
this.isRevealed = false;
|
|
this.isFlagged = false;
|
|
this.nearbyMines = 0;
|
|
this.isHint = false;
|
|
}
|
|
}
|
|
class Board {
|
|
constructor(numberOfRows, numberOfCols, numberOfMines) {
|
|
this.numberOfRows = numberOfRows;
|
|
this.numberOfCols = numberOfCols;
|
|
this.numberOfMines = numberOfMines;
|
|
this.numberOfFlags = this.numberOfMines;
|
|
this.board = Array.from({ length: numberOfRows }, () =>
|
|
Array.from({ length: numberOfCols }, () => new Cell())
|
|
);
|
|
this.numberToWin =
|
|
this.numberOfRows * this.numberOfCols - this.numberOfMines;
|
|
console.log(this.numberToWin);
|
|
// console.log(this.numberOfRows, "tt");
|
|
// console.log(this.numberOfCols, "tt");
|
|
// console.log(this.numberOfMines, "tt");
|
|
// console.log(this.numberOfFlags, "tt");
|
|
// console.log(this.numberToWin, "tt");
|
|
this.putMine(this.numberOfMines);
|
|
}
|
|
goAroundTheArray(callBack) {
|
|
// i = row
|
|
// j = col
|
|
for (let j = 0; j < this.numberOfCols; j++) {
|
|
for (let i = 0; i < this.numberOfRows; i++) {
|
|
callBack(i, j);
|
|
}
|
|
}
|
|
console.log(this.board);
|
|
}
|
|
putMine(numberOfMines) {
|
|
for (let i = 0; i < numberOfMines; ) {
|
|
let row = Math.floor(Math.random() * this.numberOfRows);
|
|
let col = Math.floor(Math.random() * this.numberOfCols);
|
|
if (!this.board[row][col].isMine) {
|
|
this.board[row][col].isMine = true;
|
|
i++;
|
|
}
|
|
}
|
|
console.log(this.board, "122");
|
|
// for (let j = 0; j < this.numberOfCols; j++) {
|
|
// for (let i = 0; i < this.numberOfRows; i++) {
|
|
this.goAroundTheArray((row, col) => this.makeNumberInBlock(row, col));
|
|
// }
|
|
// }
|
|
// this.goAroundTheArray(this.numberOfRows, this.numberOfCols);
|
|
}
|
|
|
|
// goAroundTheArray(boardSize_row, boardSize_col) {
|
|
// for (let j = 0; j < boardSize_col; j++) {
|
|
// for (let i = 0; i < boardSize_row; i++) {
|
|
// this.makeNumberInBlock(i, j);
|
|
// }
|
|
// }
|
|
// console.log(this.board);
|
|
// }
|
|
async makeNumberInBlock(row, col) {
|
|
// console.log(this.board);
|
|
let theNumberOfMines = 0;
|
|
// console.log();
|
|
if (this.board[row][col].isMine === false) {
|
|
for (let dy = -1; dy <= 1; dy++) {
|
|
for (let dx = -1; dx <= 1; dx++) {
|
|
const newRow = row + dy;
|
|
const newCol = col + dx;
|
|
if (
|
|
newRow >= 0 &&
|
|
newRow < this.numberOfRows &&
|
|
newCol >= 0 &&
|
|
newCol < this.numberOfCols &&
|
|
this.board[newRow][newCol].isMine
|
|
) {
|
|
theNumberOfMines++;
|
|
}
|
|
}
|
|
}
|
|
this.board[row][col].nearbyMines = theNumberOfMines;
|
|
} else {
|
|
this.board[row][col].nearbyMines = "M";
|
|
}
|
|
}
|
|
}
|
|
|
|
export class EndGame extends Board {
|
|
savingFunction() {
|
|
rl.question("enter your name: \n", (answer) => {
|
|
let name = answer;
|
|
this.writeOnFile(name);
|
|
rl.close();
|
|
});
|
|
}
|
|
|
|
writeOnFile(name, board, rows, cols) {
|
|
console.log("Writing into a file");
|
|
let dataForGame = {
|
|
playerName: JSON.stringify(name),
|
|
item: JSON.stringify(board),
|
|
rows: JSON.stringify(rows),
|
|
cols: JSON.stringify(cols),
|
|
};
|
|
fs.readFile("games.json", "utf8", (err, data) => {
|
|
if (err) {
|
|
console.error(err);
|
|
data = '{"games": []}';
|
|
}
|
|
let allGames = JSON.parse(data);
|
|
allGames.games.push(dataForGame);
|
|
fs.writeFile(
|
|
"newGames.json",
|
|
JSON.stringify(allGames, null, 2),
|
|
(err) => {
|
|
if (err) {
|
|
return console.error(err);
|
|
}
|
|
console.log("Data written to file");
|
|
}
|
|
);
|
|
});
|
|
}
|
|
|
|
finishAndGameOver() {
|
|
for (let i = 0; i < this.numberOfRows; i++) {
|
|
let row = "";
|
|
for (let j = 0; j < this.numberOfCols; j++) {
|
|
if (this.board[i][j].isFlagged === 1) {
|
|
row += "F";
|
|
} else if (this.board[i][j].isMine) {
|
|
row += "M";
|
|
} else if (this.board[i][j].isRevealed) {
|
|
row += this.board[i][j].isMine ? "M" : this.board[i][j].nearbyMines;
|
|
} else {
|
|
row += "#";
|
|
}
|
|
row += " ";
|
|
}
|
|
console.log(row);
|
|
}
|
|
console.log("Game Over");
|
|
end = new Date().getTime();
|
|
time = Math.ceil((end - timeForStart) / 1000) + timeInFile;
|
|
// console.log("end", end);
|
|
// console.log("startTime", startTime);
|
|
// console.log("start", timeForStart);
|
|
// console.log("timeInFile", timeInFile);
|
|
// console.log("time", time);
|
|
console.log("the time you spend in this game is : " + time + " second");
|
|
}
|
|
}
|
|
export class Play extends EndGame {
|
|
replay() {
|
|
rl.question(
|
|
'do you want to play again (0 for no 1 for yes) ex"0": \n',
|
|
(answer) => {
|
|
const [playAgain] = answer.split(" ").map(Number);
|
|
if (playAgain === 0) {
|
|
rl.close();
|
|
} else if (playAgain === 1) {
|
|
startGame.makeSizeOfGame();
|
|
}
|
|
}
|
|
);
|
|
}
|
|
printBoard() {
|
|
for (let i = 0; i < this.numberOfRows; i++) {
|
|
let row = "";
|
|
for (let j = 0; j < this.numberOfCols; j++) {
|
|
if (this.board[i][j].isFlagged === 1) {
|
|
row += "F";
|
|
} else if (this.board[i][j].isRevealed) {
|
|
row += this.board[i][j].nearbyMines;
|
|
} else {
|
|
row += "#";
|
|
}
|
|
row += " ";
|
|
}
|
|
console.log(row);
|
|
}
|
|
// this.game();
|
|
}
|
|
game() {
|
|
rl.question(
|
|
'Enter row and col number and a bool flag(1 for true/0 for false) you want to open or write "save" for save this game ex "1 1 0": \n',
|
|
(answer) => {
|
|
if (answer === "exit") {
|
|
rl.close();
|
|
} else if (answer === "save") {
|
|
this.savingFunction();
|
|
} else {
|
|
const [row, col, isFlagged] = answer.split(" ").map(Number);
|
|
if (isFlagged === 0) {
|
|
if (this.board[row - 1][col - 1].isMine === true) {
|
|
this.finishAndGameOver();
|
|
this.replay();
|
|
} else {
|
|
// console.log(this.numberToWin);
|
|
if (this.numberToWin > 1) {
|
|
this.onClick(row - 1, col - 1);
|
|
this.printBoard();
|
|
this.game();
|
|
} else {
|
|
this.onClick(row - 1, col - 1);
|
|
this.printBoard();
|
|
end = new Date().getTime();
|
|
time = Math.ceil((end - timeForStart) / 1000) + timeInFile;
|
|
console.log(
|
|
"the time you spend in this game is : " + time + " second"
|
|
);
|
|
console.log("you win");
|
|
this.replay();
|
|
// rl.close();
|
|
}
|
|
}
|
|
} else if (isFlagged === 1) {
|
|
this.doFlagged(row - 1, col - 1);
|
|
this.printBoard();
|
|
this.game();
|
|
}
|
|
}
|
|
}
|
|
);
|
|
}
|
|
doFlagged(row, col) {
|
|
this.board = JSON.parse(localStorage.getItem("minesweeperGrid"));
|
|
this.numberToWin = JSON.parse(localStorage.getItem("numberToWin"));
|
|
if (!this.board.board.board[row][col].isRevealed) {
|
|
if (!this.board.board.board[row][col].isFlagged) {
|
|
// this.numberOfFlags -= 1;
|
|
// console.log(1);
|
|
this.board.board.board[row][col].isFlagged = true;
|
|
} else if (this.board.board.board[row][col].isFlagged) {
|
|
// this.numberOfFlags += 1;
|
|
// console.log(2);
|
|
this.board.board.board[row][col].isFlagged = false;
|
|
}
|
|
// console.log("done");
|
|
} else {
|
|
console.log("i cannot put the flag");
|
|
}
|
|
localStorage.setItem("minesweeperGrid", JSON.stringify(this.board));
|
|
}
|
|
onClick(row, col) {
|
|
this.board = JSON.parse(localStorage.getItem("minesweeperGrid"));
|
|
this.numberToWin = JSON.parse(localStorage.getItem("numberToWin"));
|
|
|
|
let queue = [[row, col]];
|
|
console.log("first step done");
|
|
while (queue.length > 0) {
|
|
let [row, col] = queue.shift();
|
|
if (
|
|
row >= 0 &&
|
|
col >= 0 &&
|
|
row < this.board.board.board.length &&
|
|
col < this.board.board.board[0].length &&
|
|
this.board.board.board[row][col].isRevealed === false &&
|
|
this.board.board.board[row][col].isMine === false
|
|
) {
|
|
console.log(this.board.board.board[row][col]);
|
|
console.log("second step done");
|
|
this.board.board.board[row][col].isRevealed = true;
|
|
console.log(this.board.board.board[row][col]);
|
|
this.numberToWin = this.numberToWin - 1;
|
|
// console.log(this.numberToWin, "this is number to win");
|
|
localStorage.setItem("numberToWin", JSON.stringify(this.numberToWin));
|
|
// console.log(this.board.board.board[row][col].nearbyMines, "hello");
|
|
|
|
if (this.board.board.board[row][col].nearbyMines === 0) {
|
|
for (let j = col - 1; j <= col + 1; j++) {
|
|
for (let i = row - 1; i <= row + 1; i++) {
|
|
if (
|
|
i >= 0 &&
|
|
j >= 0 &&
|
|
i < this.board.board.board.length &&
|
|
j < this.board.board.board[0].length &&
|
|
this.board.board.board[i][j].isRevealed === false
|
|
) {
|
|
queue.push([i, j]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
console.log("finish");
|
|
localStorage.setItem("minesweeperGrid", JSON.stringify(this.board));
|
|
}
|
|
}
|
|
export class Minesweeper {
|
|
constructor(numberOfRows, numberOfCols, numberOfMines) {
|
|
this.board = new Board(numberOfRows, numberOfCols, numberOfMines);
|
|
// this.board.putMine();
|
|
// this.board.goAroundTheArray(numberOfRows, numberOfCols);
|
|
this.play = new Play(numberOfRows, numberOfCols);
|
|
this.timeInFile = 0;
|
|
}
|
|
start() {
|
|
startTime = new Date().getTime();
|
|
timeForStart = startTime;
|
|
this.play.printBoard();
|
|
this.play.game();
|
|
}
|
|
}
|
|
// class StartGame {
|
|
// startTabel() {
|
|
// rl.question("1) start game \n2) load game \n", (answer) => {
|
|
// if (answer === "1") {
|
|
// this.makeSizeOfGame();
|
|
// } else if (answer === "2") {
|
|
// this.loadGame();
|
|
// } else {
|
|
// console.log("enter the correct number");
|
|
// startTabel();
|
|
// }
|
|
// });
|
|
// }
|
|
// async loadGame() {
|
|
// try {
|
|
// const answer = await new Promise((resolve) => {
|
|
// rl.question("Enter your name to get your game:\n", (input) => {
|
|
// resolve(input);
|
|
// });
|
|
// });
|
|
// let oldName = answer;
|
|
// this.readFromFile(oldName);
|
|
// } catch (error) {
|
|
// console.error("Error:", error.message);
|
|
// }
|
|
// }
|
|
// async readFromFile(oldName) {
|
|
// try {
|
|
// let data = await fs.promises.readFile("games.json", "utf8");
|
|
// let parsedData = JSON.parse(data);
|
|
// let oldGame = parsedData.games.find(
|
|
// (oldGame) => JSON.parse(oldGame.playerName) === oldName
|
|
// );
|
|
// if (!oldGame) {
|
|
// console.error("No game found for player:", oldName);
|
|
// rl.close();
|
|
// return;
|
|
// }
|
|
// let name = JSON.parse(oldGame.playerName);
|
|
// console.log("hello " + name + " this is your game");
|
|
// this.numberOfRows = JSON.parse(oldGame.rows);
|
|
// this.numberOfCols = JSON.parse(oldGame.cols);
|
|
// const game = new Minesweeper(this.numberOfRows, this.numberOfCols);
|
|
// this.board = JSON.parse(oldGame.item);
|
|
// timeInFile = JSON.parse(oldGame.timeInGame);
|
|
// this.play = new Play(this.numberOfRows, this.numberOfCols);
|
|
// startTime = new Date().getTime();
|
|
// timeForStart = startTime;
|
|
// this.play.printBoard();
|
|
// this.play.game();
|
|
// } catch (err) {
|
|
// console.error(err);
|
|
// }
|
|
// }
|
|
// makeSizeOfGame() {
|
|
// rl.question(
|
|
// 'Enter row and col and number you want to play ex "1 1 ": ',
|
|
// (answer) => {
|
|
// const [row, col] = answer.split(" ").map(Number);
|
|
// if (row > 1 && col > 1 && row < 15 && col < 15) {
|
|
// const game = new Minesweeper(row, col);
|
|
// game.start();
|
|
// } else {
|
|
// console.log("the board is not in the rang from 2 to 15");
|
|
// this.makeSizeOfGame();
|
|
// }
|
|
// }
|
|
// );
|
|
// }
|
|
// }
|
|
let time = 0,
|
|
timeInFile = 0;
|
|
var startTime = 0;
|
|
let timeForStart = 0;
|
|
let end = 0;
|
|
// const startGame = new StartGame();
|
|
// startGame.startTabel();
|