A javascript minefield generator
yarn add notillew-minefield-core
or
npm install notillew-minefield-core
This minefield generator exports a minefield array and a object with mines mapping You can use minesMap to validate if a position is mine.
const { minefields } = minefield.generate(12, 12, 12);
// Output Array[number[]]
// [
// [2, -1, 3, 1],
// [2, -1, -1, 1]
// ]
const { minesMap } = minefield.generate(12, 12, 12);
// Output Object { 'index': Array[number] }
// {
// '0', [1],
// '1': [1, 2]
// }
Name | Required | Min | Max | Type |
---|---|---|---|---|
columns | true | 12 | - | int |
rows | true | 12 | - | int |
mines | true | 6 | less than columns * rows | int |
import React from 'react';
import { FaBomb } from 'react-icons/fa';
import minefield from 'notillew-minefield-core';
import './App.css';
function App() {
const { minefields } = minefield.generate(12, 12, 12);
const getClassName = (number) => {
switch (number) {
case -1:
return 'mine';
case 0:
return 'empty'
default:
return 'number';
}
}
return (
<div className="App">
<ul className="minefields">
{minefields.map(rows => (
<li className="row">
{rows.map(col => (
<div
className={`col-${getClassName(col)}`}
>
{col === -1 ? <FaBomb color="black" /> : col}
</div>
))}
</li>
))}
</ul>
</div>
);
}
export default App;