记得演一下,不演会被封。
骗你的,演了也封。
直接贴到控制台就能用。
记得游戏开始了再用,不然数组会越界。
不用管报错,你就问能跑不能跑。
window.analysisMode = false;
var analysisMode = window.analysisMode;
function getMinesweeperCompact() {
const remaining = [...document.querySelectorAll(`[id^="top_area_mines_"]`)]
.map(e => (e.className.match(/hd_top-area-num(\d)/) || [,'0'])[1])
.join('') | 0;
const flags = document.querySelectorAll('.hd_flag').length;
const totalMines = remaining + flags;
const cells = [...document.querySelectorAll('#AreaBlock .cell')];
const width = Math.max(...cells.map(e => +e.dataset.x)) + 1;
const height = Math.max(...cells.map(e => +e.dataset.y)) + 1;
const board = Array.from({length: height}, (_, y) =>
Array.from({length: width}, (_, x) => {
const cell = document.querySelector(`#cell_${x}_${y}`);
return !cell ? 'X' :
cell.classList.contains('hd_flag') ? 'F' :
cell.classList.contains('hd_closed') ? 'H' :
(cell.className.match(/hd_type(\d)/) || [,'0'])[1];
}).join('')
).join('\n');
return `${width}x${height}x${totalMines}\n${board}`;
}
function showCellR(x, y) {
const cell = document.querySelector(`#cell_${x}_${y}`);
if (cell) {
cell.style.position = 'relative';
cell.innerHTML = '<div style="position:absolute; top:50%; left:50%; transform:translate(-50%, -50%); width:10px; height:10px; border-radius:50%; background:#f00;"></div>';
}
}
function showCellG(x, y) {
const cell = document.querySelector(`#cell_${x}_${y}`);
if (cell) {
cell.style.position = 'relative';
cell.innerHTML = '<div style="position:absolute; top:50%; left:50%; transform:translate(-50%, -50%); width:10px; height:10px; border-radius:50%; background:#0f0;"></div>';
}
}
function showCellB(x, y) {
const cell = document.querySelector(`#cell_${x}_${y}`);
if (cell) {
cell.style.position = 'relative';
cell.innerHTML = '<div style="position:absolute; top:50%; left:50%; transform:translate(-50%, -50%); width:10px; height:10px; border-radius:50%; background:#00f;"></div>';
}
}
function hideCell(x, y) {
const cell = document.querySelector(`#cell_${x}_${y}`);
if (cell) {
cell.style.position = '';
cell.innerHTML = '';
}
}
async function S2B(data, inflate, analyse) {
if (inflate == null) {
inflate = false;
}
const lines = data.split("\n");
const size = lines[0].split("x");
if (size.length != 3) {
return;
}
const width = parseInt(size[0]);
const height = parseInt(size[1]);
const mines = parseInt(size[2]);
if (width < 1 || height < 1 || mines < 0) {
return;
}
if (lines.length < height + 1) {
return;
}
const newBoard = new Board(1, width, height, mines, "", "safe");
for (let y = 0; y < height; y++) {
const line = lines[y + 1];
for (let x = 0; x < width; x++) {
const char = line.charAt(x);
const tile = newBoard.getTileXY(x, y);
if (char == "F" || char == "M") {
tile.toggleFlag();
newBoard.bombs_left--;
tile.inflate = true;
} else if (char == "0") {
tile.setValue(0);
} else if (char == "1") {
tile.setValue(1);
} else if (char == "2") {
tile.setValue(2);
} else if (char == "3") {
tile.setValue(3);
} else if (char == "4") {
tile.setValue(4);
} else if (char == "5") {
tile.setValue(5);
} else if (char == "6") {
tile.setValue(6);
} else if (char == "7") {
tile.setValue(7);
} else if (char == "8") {
tile.setValue(8);
} else if (char == "I") { // hidden but needs inflating, part of the compression of NF games
tile.setCovered(true);
tile.setFoundBomb();
tile.inflate = true;
} else if (char == "O") { // a flag which doesn't need inflating
tile.toggleFlag();
newBoard.bombs_left--;
} else {
tile.setCovered(true);
}
}
}
// if the data needs inflating then for each flag increase the adjacent tiles value by 1
if (inflate) {
for (let tile of newBoard.tiles) {
if (tile.inflate) {
const adjTiles = newBoard.getAdjacent(tile);
for (let i = 0; i < adjTiles.length; i++) {
const adjTile = adjTiles[i];
if (!adjTile.isCovered()) { // inflate tiles which aren't flags
let value = adjTile.getValue() + 1;
adjTile.setValueOnly(value);
}
}
}
}
}
return newBoard;
// switch to the board
board = newBoard;
document.getElementById("canvas").style.cursor = "default";
canvasLocked = false; // just in case it was still locked (after an error for example)
// this redraws the board
await changeTileSize(analyse);
updateMineCount(board.bombs_left);
replayMode = false;
replayData = null;
setPageTitle();
lockMineCount.checked = true;
buildMode.checked = false;
document.getElementById("newGameSmiley").src = 'resources/images/face.svg';
if (!analysisMode && autoPlayCheckBox.checked && acceptGuessesCheckBox.checked) {
await startSolver();
}
return board;
}
function genOption() {
return {
playStyle: 1,
fullProbability: true,
guessPruning: true,
fullBFDA: true,
hardcore: false,
advancedGuessing: true,
noGuessingMode: false,
verbose: true
};
}
function countSolutions(board, notMines) {
// find all the tiles which are revealed and have un-revealed / un-flagged adjacent squares
const allCoveredTiles = [];
const witnesses = [];
const witnessed = [];
let minesLeft = board.num_bombs;
let squaresLeft = 0;
const work = new Set(); // use a map to deduplicate the witnessed tiles
for (let i = 0; i < board.tiles.length; i++) {
const tile = board.getTile(i);
if (tile.isSolverFoundBomb()) {
minesLeft--;
continue; // if the tile is a flag then nothing to consider
} else if (tile.isCovered()) {
squaresLeft++;
allCoveredTiles.push(tile);
continue; // if the tile hasn't been revealed yet then nothing to consider
}
const adjTiles = board.getAdjacent(tile);
let needsWork = false;
let minesFound = 0;
for (let j = 0; j < adjTiles.length; j++) {
const adjTile = adjTiles[j];
if (adjTile.isSolverFoundBomb()) {
minesFound++;
} else if (adjTile.isCovered()) {
needsWork = true;
work.add(adjTile.index);
}
}
// if a witness needs work (still has hidden adjacent tiles) or is broken then add it to the mix
if (needsWork || minesFound > tile.getValue()) {
witnesses.push(tile);
}
}
// generate an array of tiles from the map
for (let index of work) {
const tile = board.getTile(index);
tile.setOnEdge(true);
witnessed.push(tile);
}
var solutionCounter = new SolutionCounter(board, witnesses, witnessed, squaresLeft, minesLeft);
// let the solution counter know which tiles mustn't contain mines
if (notMines != null) {
for (let tile of notMines) {
if (!solutionCounter.setMustBeEmpty(tile)) {
}
}
}
if (solutionCounter.validWeb) {
solutionCounter.process();
} else {
let msg = "Reason not given";
if (solutionCounter.invalidReasons.length > 0) {
msg = solutionCounter.invalidReasons[0];
}
}
return solutionCounter;
}
async function importSolver() {
const baseUrl = 'https://davidnhill.github.io/JSMinesweeper/Minesweeper/';
try {
await loadScript(baseUrl + 'client/solver_main.js');
if (typeof solver === 'undefined' && typeof window.Solver !== 'undefined') {
window.solver = window.Solver;
}
if (solver && !solver.countSolutions) {
solver.countSolutions = countSolutions || function() {
return 1;
};
}
} catch (error) {
return false;
}
try {
await loadScript(baseUrl + 'Utility/Binomial.js');
await loadScript(baseUrl + 'Utility/Compression.js');
await loadScript(baseUrl + 'Utility/PrimeSieve.js');
await loadScript(baseUrl + 'client/Tile.js');
await loadScript(baseUrl + 'client/Board.js');
await loadScript(baseUrl + 'client/SolutionCounter.js');
await loadScript(baseUrl + 'client/Brute_force.js');
await loadScript(baseUrl + 'client/BruteForceAnalysis.js');
await loadScript(baseUrl + 'client/EfficiencyHelper.js');
await loadScript(baseUrl + 'client/FiftyFiftyHelper.js');
await loadScript(baseUrl + 'client/LongTermRiskHelper.js');
await loadScript(baseUrl + 'client/MinesweeperGame.js');
await loadScript(baseUrl + 'client/solver_probability_engine.js');
await loadScript(baseUrl + 'client/solver_main.js');
await loadScript(baseUrl + 'client/main.js');
const BINOMIAL = new Binomial(65000, 500);
binomialCache = new BinomialCache(5000, 500, BINOMIAL);
(() => {
if (typeof Board !== 'undefined') {
Board.prototype.getTileXY = function(x, y) {
if (x < 0 || x >= this.width || y < 0 || y >= this.height) {
return null;
}
const index = this.xy_to_index(x, y);
return this.tiles[index];
};
} else {
}
})();
showMessage = function() {};
} catch (error) {
return false;
}
const originalDivideBigInt = window.divideBigInt;
window.divideBigInt = function(numerator, denominator, dp) {
try {
if (numerator === undefined || denominator === undefined || dp === undefined) {
return 0;
}
const bigNumerator = BigInt(numerator ?? 0);
const bigDenominator = BigInt(denominator ?? 1);
if (bigDenominator === 0n) {
return 0;
}
const safeDp = Math.min(Math.max(Number(dp) || 0, 0), 10);
const multiplier = 10n ** BigInt(safeDp);
const work = bigNumerator * multiplier / bigDenominator;
return Number(work) / (10 ** safeDp);
} catch (e) {
return 0;
}
};
if (!window.power10n) {
window.power10n = [1n, 10n, 100n, 1000n, 10000n, 100000n, 1000000n, 10000000n, 100000000n, 1000000000n, 10000000000n];
}
if (!window.power10) {
window.power10 = [1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000];
}
return true;
}
importSolver();
function loadScript(url) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = url;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
loadScript();
class SolverRunner {
constructor() {
this.lastHighlighted = [];
this.isRunning = false;
this.intervalId = null;
}
clearLastHighlight() {
for (const {x, y} of this.lastHighlighted) {
hideCell(x, y);
}
this.lastHighlighted = [];
}
processActions(result) {
this.clearLastHighlight();
const newHighlighted = [];
if (result.actions && result.actions.length > 0) {
for (const item of result.actions) {
const {x, y, prob, action} = item;
if (action === 2) {
showCellR(x, y);
newHighlighted.push({x, y});
} else if (action === 1) {
if (Math.abs(prob - 1) < 1e-6) {
showCellG(x, y);
newHighlighted.push({x, y});
} else {
showCellB(x, y);
newHighlighted.push({x, y});
}
}
}
}
this.lastHighlighted = newHighlighted;
}
async runOnce() {
if (this.isRunning) return;
this.isRunning = true;
try {
window.board = await S2B(getMinesweeperCompact(), false, true);
const options = genOption();
const result = await solver(board, options);
this.processActions(result);
} catch (error) {
} finally {
this.isRunning = false;
}
}
start(interval = 1000) {
if (this.intervalId) return;
this.intervalId = setInterval(async () => {
if (!this.isRunning) {
await this.runOnce();
}
}, interval);
}
stop() {
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = null;
this.clearLastHighlight();
}
}
}
async function setupSolver() {
const success = await importSolver();
if (success) {
window.solverRunner = new SolverRunner();
console.log('求解器已准备好,可以使用 solverRunner.start() 开始自动运行');
//console.log = console.warn = console.error = console.info = console.debug = function(){};
} else {
console.log('失败了???');
}
}
setupSolver();