@jonathanbuchner/minesweeper
Version:
Minesweeper typescript project
163 lines • 6.01 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const square_1 = require("./models/square");
class Minesweeper {
constructor(width, height, mines) {
this.board = [];
this.errors = [];
this.width = width;
this.height = height;
this.squares = width * height;
this.mineInitialAmount = mines;
this.build();
}
build() {
this.addEmptyBoard();
this.addMinesToBoard();
this.addNumbersToBoard();
}
addEmptyBoard() {
for (let i = 1; i <= this.squares; i++) {
this.board.push(new square_1.Square(i));
}
}
addMinesToBoard() {
let pool = [];
for (let i = 1; i <= this.squares; i++) {
pool.push(i);
}
for (let i = 0; i < this.mineInitialAmount; i++) {
let spot = Math.floor(Math.random() * pool.length);
let number = pool[spot];
pool.splice(spot, 1);
this.board.forEach(square => {
if (square.id === number) {
square.isMine = true;
}
});
}
}
addNumbersToBoard() {
this.board.forEach(square => {
if (square.isMine === false) {
let surroundmines = 0;
//Above Square
if (square.id <= this.width) {
//Do nothing
}
else if (square.id % this.width === 1) {
if (this.board[square.id - 1 - this.width].isMine)
surroundmines++;
if (this.board[square.id - this.width].isMine)
surroundmines++;
}
else if (square.id % this.width === 0) {
if (this.board[square.id - 2 - this.width].isMine)
surroundmines++;
if (this.board[square.id - 1 - this.width].isMine)
surroundmines++;
}
else {
if (this.board[square.id - 2 - this.width].isMine)
surroundmines++;
if (this.board[square.id - 1 - this.width].isMine)
surroundmines++;
if (this.board[square.id - this.width].isMine)
surroundmines++;
}
//Left and right squares
if (square.id % this.width === 1) {
if (this.board[square.id].isMine)
surroundmines++;
}
else if (square.id % this.width === 0) {
if (this.board[square.id - 2].isMine)
surroundmines++;
}
else {
if (this.board[square.id - 2].isMine)
surroundmines++;
if (this.board[square.id].isMine)
surroundmines++;
}
//Bottom Mines
if (square.id > this.width * this.height - this.width) {
//Do nothing
}
else if (square.id % this.width === 1) {
if (this.board[square.id - 1 + this.width].isMine)
surroundmines++;
if (this.board[square.id + this.width].isMine)
surroundmines++;
}
else if (square.id % this.width === 0) {
if (this.board[square.id - 2 + this.width].isMine)
surroundmines++;
if (this.board[square.id - 1 + this.width].isMine)
surroundmines++;
}
else {
if (this.board[square.id - 2 + this.width].isMine)
surroundmines++;
if (this.board[square.id - 1 + this.width].isMine)
surroundmines++;
if (this.board[square.id + this.width].isMine)
surroundmines++;
}
square.number = surroundmines;
}
});
}
logError(err) {
this.errors.push(err);
}
getBoard() {
return this.board;
}
getSquare(id) {
let square = this.board.find(o => o.id === id);
if (typeof square == "undefined")
throw this.logError("No square found by the Id");
return square;
}
checkIsMine(id) {
let square = this.board.find(o => o.id === id);
if (typeof square == "undefined")
throw this.logError("No square found by the Id");
return square.isMine;
}
getNumber(id) {
let square = this.board.find(o => o.id === id);
if (typeof square == "undefined")
throw this.logError("No square found by the Id");
return square.number;
}
getIsVisible(id) {
let square = this.board.find(o => o.id === id);
if (typeof square == "undefined")
throw this.logError("No square found by the Id");
return square.isVisible;
}
markVisible(id) {
let square = this.board.find(o => o.id === id);
if (typeof square == "undefined")
throw this.logError("No square found by the Id");
square.isVisible = true;
}
HasWon() {
this.board.forEach(s => {
if (s.isMine === false && s.isVisible === false)
return false;
});
return true;
}
HasLost() {
this.board.forEach(s => {
if (s.isMine === true && s.isVisible === true)
return true;
});
return false;
}
}
exports.Minesweeper = Minesweeper;
//# sourceMappingURL=minesweeper.js.map