Build minesweeper on a 5x5 grid of buttons: five hidden mines, clicking a safe square reveals how many mines touch it, clicking a mine ends the game and reveals the board
Play classic Minesweeper on a 5x5 button grid!
How to play:
/minesweeper to start a new gameFeatures:
Made by MEE6. Remix it to get a copy of your own and change anything you like.
💥 Game Over! @You hit a mine!
// Minesweeper game: 5x5 grid with 5 hidden mines
interface GameState {
board: number[][]; // -1 = mine, 0-8 = count of adjacent mines
revealed: boolean[][];
gameOver: boolean;
won: boolean;
player: string;
}
// Generate a new minesweeper board
function createBoard(): number[][] {
const board: number[][] = Array.from({ length: 5 }, () => Array(5).fill(0));
// Place 5 mines randomly
let minesPlaced = 0;
while (minesPlaced < 5) {
const row = Math.floor(Math.random() * 5);
const col = Math.floor(Math.random() * 5);
if (board[row][col] !== -1) {
board[row][col] = -1;
minesPlaced++;
// Update adjacent counts
for (let dr = -1; dr <= 1; dr++) {
for (let dc = -1; dc <= 1; dc++) {
const nr = row + dr;
const nc = col + dc;
if (nr >= 0 && nr < 5 && nc >= 0 && nc < 5 && board[nr][nc] !== -1) {
board[nr][nc]++;
}
}
}
}
}
return board;
}
// Create button grid for the board
function createButtons(game: GameState): any[] {
const rows: any[] = [];
for (let row = 0; row < 5; row++) {
const rowButtons: any[] = [];
for (let col = 0; col < 5; col++) {
if (game.revealed[row][col]) {
const value = game.board[row][col];
let label: string;
let style: string;
if (value === -1) {
label = '💣';
style = 'danger';
} else if (value === 0) {
label = '·';
style = 'secondary';
} else {
label = `${value}`;
style = 'secondary';
}
rowButtons.push({
label,
customId: `cell_${row}_${col}_done`,
style,
disabled: true,
});
} else {
rowButtons.push({
label: '⬜',
customId: `cell_${row}_${col}`,
style: 'primary',
disabled: game.gameOver || game.won,
});
}
}
rows.push(rowButtons);
}
return rows;
}
// Reveal a cell (and flood-fill if it's 0)
function revealCell(game: GameState, row: number, col: number): void {
if (row < 0 || row >= 5 || col < 0 || col >= 5 || game.revealed[row][col]) {
return;
}
game.revealed[row][col] = true;
// If it's a mine, game over
if (game.board[row][col] === -1) {
game.gameOver = true;
// Reveal all mines
for (let r = 0; r < 5; r++) {
for (let c = 0; c < 5; c++) {
if (game.board[r][c] === -1) {
game.revealed[r][c] = true;
}
}
}
return;
}
// If it's 0, flood-fill adjacent cells
if (game.board[row][col] === 0) {
for (let dr = -1; dr <= 1; dr++) {
for (let dc = -1; dc <= 1; dc++) {
if (dr !== 0 || dc !== 0) {
revealCell(game, row + dr, col + dc);
}
}
}
}
}
// Check if player won
function checkWin(game: GameState): boolean {
for (let row = 0; row < 5; row++) {
for (let col = 0; col < 5; col++) {
if (game.board[row][col] !== -1 && !game.revealed[row][col]) {
return false;
}
}
}
return true;
}
// Get game status message
function getStatus(game: GameState): string {
if (game.gameOver) {
return `💥 **Game Over!** <@${game.player}> hit a mine!`;
}
if (game.won) {
return `🎉 **You Win!** <@${game.player}> cleared all safe squares!`;
}
return `🎮 Minesweeper | Playing: <@${game.player}> | 5 mines hidden`;
}
mee6.command('minesweeper', { description: 'Start a new Minesweeper game' }, async (ctx) => {
const game: GameState = {
board: createBoard(),
revealed: Array.from({ length: 5 }, () => Array(5).fill(false)),
gameOver: false,
won: false,
player: ctx.author.id,
};
const sent = await ctx.reply({
content: getStatus(game),
buttons: createButtons(game),
});
await mee6.store.set(`game_${sent.messageId}`, game, { ttl: 3600 });
});
mee6.on('button', async (ctx) => {
if (!ctx.customId.startsWith('cell_')) return;
const game = await mee6.store.get<GameState>(`game_${ctx.message.id}`);
if (!game) {
await ctx.reply({ content: 'This game has expired.', ephemeral: true });
return;
}
// Only the player who started can play
if (ctx.author.id !== game.player) {
await ctx.reply({ content: 'This is not your game.', ephemeral: true });
return;
}
// Can't click if game is over
if (game.gameOver || game.won) {
await ctx.reply({ content: 'This game has already ended.', ephemeral: true });
return;
}
// Parse the cell coordinates
const parts = ctx.customId.split('_');
const row = parseInt(parts[1]);
const col = parseInt(parts[2]);
// Reveal the cell
revealCell(game, row, col);
// Check for win
if (!game.gameOver) {
game.won = checkWin(game);
}
// Update the board
await ctx.editOriginal({
content: getStatus(game),
buttons: createButtons(game),
});
// Save updated state (or delete if game ended)
if (game.gameOver || game.won) {
await mee6.store.delete(`game_${ctx.message.id}`);
} else {
await mee6.store.set(`game_${ctx.message.id}`, game, { ttl: 3600 });
}
});