Compare commits

..

No commits in common. "22b8c147b608ffc5f467c7bf813d8ae50628b746" and "52ee48bd862af549ae28903e1acca1878469ee7f" have entirely different histories.

1 changed files with 8 additions and 15 deletions

View File

@ -45,24 +45,20 @@ const hasWon = (serverGame: ServerGame) => {
return true; return true;
}; };
const getNewMinesCount = (width: number, height: number, stage: number) => {
const minePercentage = Math.log10(30 * stage) * 0.092;
const newMinesCount = Math.floor(width * height * minePercentage);
return newMinesCount;
};
const expandBoard = (serverGame: ServerGame) => { const expandBoard = (serverGame: ServerGame) => {
const { width, height, stage, mines, isFlagged, isRevealed, isQuestionMark } = const { width, height, stage, mines, isFlagged, isRevealed, isQuestionMark } =
serverGame; serverGame;
let dir = stage % 2 === 0 ? "down" : "right"; let dir = stage % 2 === 0 ? "down" : "right";
if (stage > 15) { if (stage > 13) {
dir = "down"; dir = "down";
} }
// Expand the board by the current board size 8x8 -> 16x8 // Expand the board by the current board size 8x8 -> 16x8
if (dir === "down") { if (dir === "down") {
const newHeight = Math.floor(Math.min(height + 7, height * 1.5)); const newHeight = Math.floor(Math.min(height + 7, height * 1.5));
const newWidth = width; const newWidth = width;
const newMinesCount = getNewMinesCount(width, newHeight - height, stage); const newMinesCount = Math.floor(
width * height * 0.5 * (0.2 + 0.0015 * stage),
);
// expand mines array // expand mines array
const newMines = Array.from({ length: newWidth }, () => const newMines = Array.from({ length: newWidth }, () =>
new Array(newHeight).fill(false), new Array(newHeight).fill(false),
@ -96,18 +92,13 @@ const expandBoard = (serverGame: ServerGame) => {
} }
// generate new mines // generate new mines
let remainingMines = newMinesCount; let remainingMines = newMinesCount;
let runs = 0; while (remainingMines > 0) {
while (remainingMines > 0 && runs < width * height) {
const x = Math.floor(Math.random() * width); const x = Math.floor(Math.random() * width);
const y = height + Math.floor(Math.random() * (newHeight - height)); const y = height + Math.floor(Math.random() * (newHeight - height));
if (!newMines[x][y]) { if (!newMines[x][y]) {
newMines[x][y] = true; newMines[x][y] = true;
remainingMines--; remainingMines--;
} }
runs++;
}
if (runs == width * height) {
console.error("Oops all mines!");
} }
Object.assign(serverGame, { Object.assign(serverGame, {
width: newWidth, width: newWidth,
@ -123,7 +114,9 @@ const expandBoard = (serverGame: ServerGame) => {
if (dir === "right") { if (dir === "right") {
const newWidth = Math.floor(Math.min(width + 7, width * 1.5)); const newWidth = Math.floor(Math.min(width + 7, width * 1.5));
const newHeight = height; const newHeight = height;
const newMinesCount = getNewMinesCount(newWidth - width, height, stage); const newMinesCount = Math.floor(
width * height * 0.5 * (0.2 + 0.0015 * stage),
);
// expand mines array // expand mines array
const newMines = Array.from({ length: newWidth }, () => const newMines = Array.from({ length: newWidth }, () =>
new Array(newHeight).fill(false), new Array(newHeight).fill(false),