Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const NOT_OPEN = -1;
- const MINE_FOUND = -2;
- const NUMBERS = [1, 2, 3, 4, 5, 6, 7, 8];
- const dataSets = [
- {
- input: [
- [-1, 1, 1, -1],
- [ 1, 1, 1, 1],
- [ 1, 1, 1, 1],
- [-1, 1, 1, -1],
- ],
- expected: [
- [-2, 1, 1, -2],
- [ 1, 1, 1, 1],
- [ 1, 1, 1, 1],
- [-2, 1, 1, -2],
- ],
- },
- {
- input: [
- [-1, -1, -1, -1, -1, -1],
- [-1, 2, 2, 2, 2, -1],
- [-1, 2, 0, 0, 2, -1],
- [-1, 2, 0, 0, 2, -1],
- [-1, 2, 2, 2, 2, -1],
- [-1, -1, -1, -1, -1, -1],
- ],
- expected: [
- [ 0, 1, -2, -2, 1, 0],
- [ 1, 2, 2, 2, 2, 1],
- [-2, 2, 0, 0, 2, -2],
- [-2, 2, 0, 0, 2, -2],
- [ 1, 2, 2, 2, 2, 1],
- [ 0, 1, -2, -2, 1, 0],
- ],
- },
- ];
- function cloneData(data) {
- return data.map(row => {
- return row.slice();
- });
- }
- function solve(data) {
- const height = data.length;
- const width = data[0].length;
- for (let i = 0; i < height; i++) {
- for (let j = 0; j < width; j++) {
- assertMine(data, i, j);
- markCells(data, i, j);
- }
- }
- }
- function assertMine(data, i, j) {
- const field = data[i][j];
- if (field !== NOT_OPEN) return;
- const newData = cloneData(data);
- newData[i][j] = MINE_FOUND;
- if (!validateData(newData)) {
- data[i][j] = 0;
- const aroundCells = getAdjacent(data, i, j, NUMBERS);
- for (const cell of aroundCells) {
- markCells(data, cell[0], cell[1]);
- }
- }
- }
- function markCells(data, i, j) {
- const minesCount = data[i][j];
- if (minesCount <= 0) return;
- const mineFound = getAdjacent(data, i, j, [MINE_FOUND]);
- const notOpen = getAdjacent(data, i, j, [NOT_OPEN]);
- const allMinesFound = mineFound.length === minesCount;
- const allMinesCanBeFound = (notOpen.length + mineFound.length) === minesCount;
- if (minesCount > mineFound.length + notOpen.length) {
- throw new Error('Invalid');
- }
- if (allMinesFound || allMinesCanBeFound) {
- const marked = [];
- if (allMinesFound) {
- for (const cell of notOpen) {
- data[cell[0]][cell[1]] = 0;
- marked.push(cell);
- }
- }
- if (allMinesCanBeFound) {
- for (const cell of notOpen) {
- data[cell[0]][cell[1]] = MINE_FOUND;
- marked.push(cell);
- }
- }
- if (marked.length) {
- const foundAroundCells = {};
- for (const cell of marked) {
- const aroundCells = getAdjacent(data, cell[0], cell[1], NUMBERS);
- for (const aroundCell of aroundCells) {
- foundAroundCells[cellKey(aroundCell)] = aroundCell;
- }
- }
- for (const aroundCell of Object.values(foundAroundCells)) {
- markCells(data, aroundCell[0], aroundCell[1]);
- }
- }
- }
- }
- function validateData(data) {
- const height = data.length;
- const width = data[0].length;
- for (let i = 0; i < height; i++) {
- for (let j = 0; j < width; j++) {
- try {
- markCells(data, i, j);
- } catch (e) {
- return false;
- }
- }
- }
- return true;
- }
- function cellKey(cell) {
- return cell[0] + '_' + cell[1];
- }
- function getAdjacent(data, i, j, values) {
- let cells = [];
- for (let x = -1; x <= 1; x++) {
- const i0 = i + x;
- if (!data[i0]) continue;
- for (let y = -1; y <= 1; y++) {
- const j0 = j + y;
- if (!data[i0][j0] || (j0 === j && i0 === i)) continue;
- const field = data[i0][j0];
- if (values.indexOf(field) > -1) {
- cells.push([i0, j0]);
- }
- }
- }
- return cells;
- }
- describe("running jasmine in jsfiddle", function(){
- it("should run tests", function() {
- for (const dataset of dataSets) {
- const data = dataset.input;
- solve(data);
- expect(data).toEqual(dataset.expected);
- }
- });
- });
- (function() {
- var env = jasmine.getEnv();
- console.log(jasmine);
- env.addReporter(new jasmine.HtmlReporter());
- env.execute();
- }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement