Build 2048: the board is drawn in an embed with four arrow buttons under it, every press slides the tiles and adds a new one, the message updates in place, and it keeps each member best score
Play the classic 2048 puzzle game in Discord! Slide tiles with arrow buttons to combine matching numbers and reach 2048.
Commands:
/2048 — Start a new game/2048-top — View high scoresHow to play:
Your best score is saved automatically!
Made by MEE6. Remix it to get a copy of your own and change anything you like.
Score: 24 Use the arrows to slide tiles!

Score: 0 Use the arrows to slide tiles!

Score: 0 Use the arrows to slide tiles!

No scores yet — be the first to play /2048!
// 2048 game: slide tiles on a 4x4 grid to combine matching numbers
type Board = number[][]; // 4x4 grid, 0 = empty
type GameState = { board: Board; score: number; userId: string };
// Create a new empty 4x4 board
function newBoard(): Board {
return [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
];
}
// Add a random tile (90% chance of 2, 10% chance of 4) to an empty cell
function addRandomTile(board: Board): void {
const empty: [number, number][] = [];
for (let r = 0; r < 4; r++) {
for (let c = 0; c < 4; c++) {
if (board[r][c] === 0) empty.push([r, c]);
}
}
if (empty.length > 0) {
const [r, c] = empty[Math.floor(Math.random() * empty.length)];
board[r][c] = Math.random() < 0.9 ? 2 : 4;
}
}
// Slide and merge tiles in a direction, return { moved, score }
function move(board: Board, dir: 'up' | 'down' | 'left' | 'right'): { moved: boolean; score: number } {
const old = board.map((r) => [...r]);
let score = 0;
if (dir === 'left') {
for (let r = 0; r < 4; r++) {
const row = board[r].filter((x) => x !== 0);
for (let i = 0; i < row.length - 1; i++) {
if (row[i] === row[i + 1]) {
row[i] *= 2;
score += row[i];
row.splice(i + 1, 1);
}
}
while (row.length < 4) row.push(0);
board[r] = row;
}
} else if (dir === 'right') {
for (let r = 0; r < 4; r++) {
const row = board[r].filter((x) => x !== 0);
for (let i = row.length - 1; i > 0; i--) {
if (row[i] === row[i - 1]) {
row[i] *= 2;
score += row[i];
row.splice(i - 1, 1);
i--;
}
}
while (row.length < 4) row.unshift(0);
board[r] = row;
}
} else if (dir === 'up') {
for (let c = 0; c < 4; c++) {
const col = [board[0][c], board[1][c], board[2][c], board[3][c]].filter((x) => x !== 0);
for (let i = 0; i < col.length - 1; i++) {
if (col[i] === col[i + 1]) {
col[i] *= 2;
score += col[i];
col.splice(i + 1, 1);
}
}
while (col.length < 4) col.push(0);
for (let r = 0; r < 4; r++) board[r][c] = col[r];
}
} else if (dir === 'down') {
for (let c = 0; c < 4; c++) {
const col = [board[0][c], board[1][c], board[2][c], board[3][c]].filter((x) => x !== 0);
for (let i = col.length - 1; i > 0; i--) {
if (col[i] === col[i - 1]) {
col[i] *= 2;
score += col[i];
col.splice(i - 1, 1);
i--;
}
}
while (col.length < 4) col.unshift(0);
for (let r = 0; r < 4; r++) board[r][c] = col[r];
}
}
const moved = old.some((r, i) => r.some((v, j) => v !== board[i][j]));
return { moved, score };
}
// Check if any moves are possible
function canMove(board: Board): boolean {
// Check for empty cells
for (let r = 0; r < 4; r++) {
for (let c = 0; c < 4; c++) {
if (board[r][c] === 0) return true;
}
}
// Check for adjacent matching tiles
for (let r = 0; r < 4; r++) {
for (let c = 0; c < 4; c++) {
if (c < 3 && board[r][c] === board[r][c + 1]) return true;
if (r < 3 && board[r][c] === board[r + 1][c]) return true;
}
}
return false;
}
// Build the board cells for mee6.board()
function buildCells(board: Board): any[][] {
const colors: { [key: number]: { color: string; background: string } } = {
0: { color: '#776E65', background: '#CDC1B4' },
2: { color: '#776E65', background: '#EEE4DA' },
4: { color: '#776E65', background: '#EDE0C8' },
8: { color: '#F9F6F2', background: '#F2B179' },
16: { color: '#F9F6F2', background: '#F59563' },
32: { color: '#F9F6F2', background: '#F67C5F' },
64: { color: '#F9F6F2', background: '#F65E3B' },
128: { color: '#F9F6F2', background: '#EDCF72' },
256: { color: '#F9F6F2', background: '#EDCC61' },
512: { color: '#F9F6F2', background: '#EDC850' },
1024: { color: '#F9F6F2', background: '#EDC53F' },
2048: { color: '#F9F6F2', background: '#EDC22E' },
};
return board.map((row) =>
row.map((val) => {
const style = colors[val] || { color: '#F9F6F2', background: '#3C3A32' };
return val === 0
? { text: '', color: style.color, background: style.background }
: { text: String(val), color: style.color, background: style.background };
})
);
}
// Start a new game
mee6.command('2048', { description: 'Start a new 2048 game' }, async (ctx) => {
const board = newBoard();
addRandomTile(board);
addRandomTile(board);
const state: GameState = { board, score: 0, userId: ctx.author.id };
const boardUrl = await mee6.board({ cells: buildCells(board), cell: 64 });
const sent = await ctx.reply({
embed: {
title: '🎮 2048',
description: `Score: **0**\nUse the arrows to slide tiles!`,
image_url: boardUrl,
color: '#EDC22E',
},
buttons: [
[{ label: '⬆️', customId: 'up', style: 'primary' }],
[
{ label: '⬅️', customId: 'left', style: 'primary' },
{ label: '⬇️', customId: 'down', style: 'primary' },
{ label: '➡️', customId: 'right', style: 'primary' },
],
[{ label: '🔄 New Game', customId: 'new', style: 'secondary' }],
],
});
await mee6.store.set(`game_${sent.messageId}`, state, { ttl: 3600 });
});
// Handle button clicks
mee6.on('button', async (ctx) => {
const key = `game_${ctx.message.id}`;
const state = await mee6.store.get<GameState>(key);
if (!state) return;
// Only the player who started can move
if (ctx.author.id !== state.userId) {
await ctx.reply({ content: '❌ This is not your game.', ephemeral: true });
return;
}
// New game button
if (ctx.customId === 'new') {
const board = newBoard();
addRandomTile(board);
addRandomTile(board);
state.board = board;
state.score = 0;
const boardUrl = await mee6.board({ cells: buildCells(board), cell: 64 });
await ctx.editOriginal({
embed: {
title: '🎮 2048',
description: `Score: **0**\nUse the arrows to slide tiles!`,
image_url: boardUrl,
color: '#EDC22E',
},
});
await mee6.store.set(key, state, { ttl: 3600 });
return;
}
// Movement buttons
const dir = ctx.customId as 'up' | 'down' | 'left' | 'right';
const result = move(state.board, dir);
if (!result.moved) {
await ctx.reply({ content: '⚠️ No tiles moved in that direction.', ephemeral: true });
return;
}
state.score += result.score;
addRandomTile(state.board);
// Check game over
const gameOver = !canMove(state.board);
let description = `Score: **${state.score}**\n`;
if (gameOver) {
// Save best score
const m = mee6.store.member(ctx.author.id);
const best = await m.get<number>('best_2048', 0);
if (state.score > best) {
await m.set('best_2048', state.score);
description += `🎉 **New best score!**\nGame Over — no more moves.`;
} else {
description += `Game Over — no more moves.\nYour best: **${best}**`;
}
} else {
description += 'Use the arrows to slide tiles!';
}
const boardUrl = await mee6.board({ cells: buildCells(state.board), cell: 64 });
await ctx.editOriginal({
embed: {
title: '🎮 2048',
description,
image_url: boardUrl,
color: gameOver ? '#8F7A66' : '#EDC22E',
},
});
await mee6.store.set(key, state, { ttl: 3600 });
});
// Show leaderboard
mee6.command('2048-top', { description: 'View 2048 high scores' }, async (ctx) => {
const top = await mee6.store.memberTop('best_2048', 10);
if (top.length === 0) {
await ctx.reply('No scores yet — be the first to play `/2048`!');
return;
}
const lines = top.map((entry, i) => `${i + 1}. <@${entry.user_id}> — **${entry.value}**`);
await ctx.reply({
embed: {
title: '🏆 2048 High Scores',
description: lines.join('\n'),
color: '#EDC22E',
},
});
});