Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fetch = require('node-fetch');
- const damierGames = {};
- const damierStats = {}; // Pour stocker les statistiques
- const EMPTY = "๐ฉ";
- const PION_B = "โช";
- const PION_N = "โซ";
- const DAME_B = "๐ต";
- const DAME_N = "๐ด";
- function createDamierBoard() {
- const board = Array.from({ length: 8 }, () => Array(8).fill(EMPTY));
- for (let i = 0; i < 3; i++) {
- for (let j = 0; j < 8; j++) {
- if ((i + j) % 2 === 1) board[i][j] = PION_N;
- }
- }
- for (let i = 5; i < 8; i++) {
- for (let j = 0; j < 8; j++) {
- if ((i + j) % 2 === 1) board[i][j] = PION_B;
- }
- }
- return board;
- }
- function displayDamier(board) {
- let s = " a b c d e f g h\n";
- for (let i = 0; i < 8; i++) {
- s += (8 - i) + " ";
- for (let j = 0; j < 8; j++) {
- s += board[i][j] + " ";
- }
- s += "\n";
- }
- return s;
- }
- function parseDamierMove(move) {
- const regex = /^([a-h][1-8])\s+([a-h][1-8])$/i;
- const match = move.match(regex);
- if (!match) return null;
- const pos = (p) => [8 - Number(p[1]), p.charCodeAt(0) - 97];
- return [pos(match[1].toLowerCase()), pos(match[2].toLowerCase())];
- }
- function isInside(x, y) {
- return x >= 0 && x < 8 && y >= 0 && y < 8;
- }
- function hasPieces(board, pion, dame) {
- return board.flat().some(cell => cell === pion || cell === dame);
- }
- function isValidMoveDamier(board, from, to, player) {
- const [fx, fy] = from, [tx, ty] = to;
- if (!isInside(fx, fy) || !isInside(tx, ty)) return false;
- const piece = board[fx][fy];
- if (board[tx][ty] !== EMPTY) return false;
- // Pion blanc
- if (piece === PION_B) {
- if (fx - tx === 1 && Math.abs(ty - fy) === 1) return true;
- if (fx - tx === 2 && Math.abs(ty - fy) === 2) {
- const midX = fx - 1;
- const midY = fy + (ty - fy) / 2;
- if (board[midX][midY] === PION_N || board[midX][midY] === DAME_N) return "prise";
- }
- }
- // Pion noir
- if (piece === PION_N) {
- if (tx - fx === 1 && Math.abs(ty - fy) === 1) return true;
- if (tx - fx === 2 && Math.abs(ty - fy) === 2) {
- const midX = fx + 1;
- const midY = fy + (ty - fy) / 2;
- if (board[midX][midY] === PION_B || board[midX][midY] === DAME_B) return "prise";
- }
- }
- // Dame blanche
- if (piece === DAME_B) {
- if (Math.abs(fx - tx) === Math.abs(fy - ty)) {
- const dx = tx > fx ? 1 : -1, dy = ty > fy ? 1 : -1;
- let x = fx + dx, y = fy + dy, found = false;
- while (x !== tx && y !== ty) {
- if (board[x][y] === PION_N || board[x][y] === DAME_N) {
- if (found) return false;
- found = true;
- } else if (board[x][y] !== EMPTY) return false;
- x += dx; y += dy;
- }
- return found ? "prise" : true;
- }
- }
- // Dame noire
- if (piece === DAME_N) {
- if (Math.abs(fx - tx) === Math.abs(fy - ty)) {
- const dx = tx > fx ? 1 : -1, dy = ty > fy ? 1 : -1;
- let x = fx + dx, y = fy + dy, found = false;
- while (x !== tx && y !== ty) {
- if (board[x][y] === PION_B || board[x][y] === DAME_B) {
- if (found) return false;
- found = true;
- } else if (board[x][y] !== EMPTY) return false;
- x += dx; y += dy;
- }
- return found ? "prise" : true;
- }
- }
- return false;
- }
- function checkPromotion(board) {
- for (let j = 0; j < 8; j++) {
- if (board[0][j] === PION_B) board[0][j] = DAME_B;
- if (board[7][j] === PION_N) board[7][j] = DAME_N;
- }
- }
- function getAllLegalMoves(board, player) {
- const moves = [];
- const myPion = player === 0 ? PION_B : PION_N;
- const myDame = player === 0 ? DAME_B : DAME_N;
- for (let fx = 0; fx < 8; fx++) {
- for (let fy = 0; fy < 8; fy++) {
- if ([myPion, myDame].includes(board[fx][fy])) {
- for (let tx = 0; tx < 8; tx++) {
- for (let ty = 0; ty < 8; ty++) {
- if ((fx !== tx || fy !== ty) && isValidMoveDamier(board, [fx, fy], [tx, ty], player === 0 ? "blanc" : "noir")) {
- moves.push([[fx, fy], [tx, ty]]);
- }
- }
- }
- }
- }
- }
- return moves;
- }
- function updateStats(winnerID, loserID) {
- if (!damierStats[winnerID]) damierStats[winnerID] = { wins: 0, losses: 0 };
- if (!damierStats[loserID]) damierStats[loserID] = { wins: 0, losses: 0 };
- damierStats[winnerID].wins++;
- damierStats[loserID].losses++;
- }
- const DAMES_HELP = `
- ใ ๐ณ๐ฐ๐ผ๐ด๐ ใ- ๐ฅรจ๐ด๐น๐ฒ๐ & ๐๐ผ๐บ๐บ๐ฎ๐ป๐ฑ๐ฒ๐
- ๐ฒ ๐ฑ๐๐ ๐๐ ๐๐๐ :
- Dรฉplacer vos pions en diagonale pour capturer tous ceux de lโadversaire ou le bloquer.
- ๐ธ Un pion avance en diagonale.
- ๐น Capture : sautez par-dessus un pion adverse.
- ๐ธ Dame : atteint le dernier rang (๐ต ou ๐ด), peut avancer/reculer en diagonale.
- ๐ ๐ฒ๐๐๐๐๐๐๐๐ ๐๐๐๐๐๐๐๐๐๐๐ :
- - dames @ami : commencez contre un ami mentionnรฉ.
- - dames <ID> : dรฉfiez un joueur par son ID.
- - dames bot : joue contre le bot.
- - dames stats : affiche vos statistiques.
- - dames help : affiche ce message dโaide.
- ๐ฌ ๐ด๐ ๐๐๐๐๐ ๐๐ ๐๐๐๐๐๐ :
- - Ex: b6 a5 pour dรฉplacer un pion.
- - forfait / abandon : quitter la partie.
- - restart / rejouer : recommencer une partie.
- Bon jeu ! ๐
- `;
- async function botPlay(game, api, threadID) {
- const board = game.board;
- const moves = getAllLegalMoves(board, 1);
- if (moves.length === 0) {
- game.inProgress = false;
- const winner = game.players[0];
- if (winner.id !== "BOT") updateStats(winner.id, "BOT");
- await api.sendMessage(
- `${displayDamier(board)}\n\n๐| ${winner.name} ๐๐๐๐๐๐๐๐ ๐๐ ๐๐๐๐๐๐ !`,
- threadID
- );
- return;
- }
- let botMove = moves.find(([from, to]) => isValidMoveDamier(board, from, to, "noir") === "prise");
- if (!botMove) botMove = moves[0];
- const [[fx, fy], [tx, ty]] = botMove;
- const piece = board[fx][fy];
- board[tx][ty] = piece;
- board[fx][fy] = EMPTY;
- if (isValidMoveDamier(board, [fx, fy], [tx, ty], "noir") === "prise") {
- board[(fx + tx) / 2][(fy + ty) / 2] = EMPTY;
- }
- checkPromotion(board);
- const hasBlanc = hasPieces(board, PION_B, DAME_B);
- const hasNoir = hasPieces(board, PION_N, DAME_N);
- if (!hasBlanc || !hasNoir) {
- game.inProgress = false;
- const winner = hasBlanc ? game.players[0] : game.players[1];
- if (winner.id !== "BOT") updateStats(winner.id, "BOT");
- await api.sendMessage(
- `${displayDamier(board)}\n\n๐| ${winner.name} ๐๐๐๐๐๐๐๐ ๐๐ ๐๐๐๐๐๐ !`,
- threadID
- );
- return;
- }
- game.turn = 0;
- await api.sendMessage(
- `${displayDamier(board)}\n\n๐ฒ'๐๐๐ ๐๐๐๐๐ ๐๐๐๐ !๐`,
- threadID
- );
- }
- module.exports = {
- config: {
- name: "dames",
- aliases: ["damiers", "checkers"],
- version: "1.2",
- author: "ใโ ๐๐๐๐๐โ๐๐๐ 3.0โ ๅฝก",
- category: "game",
- shortDescription: "Jouez aux dames contre un ami ou le bot.",
- usage: "dames @ami | dames <ID> | dames bot | dames stats | dames help"
- },
- onStart: async function ({ api, event, args }) {
- const threadID = event.threadID;
- const senderID = event.senderID;
- let opponentID;
- let playWithBot = false;
- // Aide
- if (args[0] && args[0].toLowerCase() === "help") {
- return api.sendMessage(DAMES_HELP, threadID, event.messageID);
- }
- // Stats
- if (args[0] && args[0].toLowerCase() === "stats") {
- const userStats = damierStats[senderID] || { wins: 0, losses: 0 };
- return api.sendMessage(
- `ใ ๐ณ๐ฐ๐ผ๐ด๐ ใ- ๐ฆ๐๐ฎ๐๐\n๐ค Nom : ${event.senderName || "Inconnu"}\n๐ ID : ${senderID}\nโ Victoires : ${userStats.wins}\nโ Dรฉfaites : ${userStats.losses}`,
- threadID,
- event.messageID
- );
- }
- // Jouer contre le bot uniquement si on รฉcrit "bot"
- if (args[0] && args[0].toLowerCase() === "bot") playWithBot = true;
- else if (event.mentions && Object.keys(event.mentions).length > 0)
- opponentID = Object.keys(event.mentions)[0];
- else if (args[0] && /^\d+$/.test(args[0])) opponentID = args[0];
- if (!opponentID && !playWithBot) {
- return api.sendMessage(`Veuillez mentionner un ami, donner son ID, ou tapez "dames bot" pour jouer contre le bot. Tapez "dames help" pour lโaide.`, threadID, event.messageID);
- }
- if (opponentID && opponentID == senderID)
- return api.sendMessage("Vous ne pouvez pas jouer contre vous-mรชme !", threadID, event.messageID);
- // Rรฉcupรฉration nom auteur via API
- let authorName = "ใโ ๐๐๐๐๐โ๐๐๐ 3.0โ ๅฝก";
- try {
- const authorResponse = await fetch('https://author-name.vercel.app/');
- const authorJson = await authorResponse.json();
- authorName = authorJson.author || authorName;
- } catch (e) {}
- const gameID = playWithBot
- ? `${threadID}:${senderID}:BOT`
- : `${threadID}:${Math.min(senderID, opponentID)}:${Math.max(senderID, opponentID)}`;
- if (damierGames[gameID] && damierGames[gameID].inProgress)
- return api.sendMessage("โ| ๐๐๐ ๐๐๐๐๐๐ ๐๐๐ ๐๐๐๐ ๐๐ ๐๐๐๐๐ ๐๐๐๐๐ ๐๐๐ ๐๐๐๐๐๐๐. ๐ ๐๐๐๐๐๐๐ฃ ๐๐๐๐๐๐๐๐๐ โณ.", threadID, event.messageID);
- let player1Info, player2Info, botName = "โคใ ๐ท๐ด๐ณ๐ถ๐ด๐ท๐พ๐ถ๐๐ถ๐ฟ๐ ใโใ ";
- if (playWithBot) {
- player1Info = await api.getUserInfo([senderID]);
- damierGames[gameID] = {
- board: createDamierBoard(),
- players: [
- { id: senderID, name: player1Info[senderID].name, color: "blanc" },
- { id: "BOT", name: botName, color: "noir" }
- ],
- turn: 0,
- inProgress: true,
- vsBot: true
- };
- api.sendMessage(
- `๐ฃ| ๐ป๐๐๐๐๐๐๐๐ ๐'๐๐๐ ๐๐๐๐๐๐๐๐ ๐๐๐๐๐๐ ๐๐ ๐๐๐๐๐ ๐๐๐๐๐ ${player1Info[senderID].name} (โช) ๐๐ ${botName} (โซ) !\nโโโโโโโโโชโโซโโโโโโโโ\n${displayDamier(damierGames[gameID].board)}\nโโโโโโโโโชโโซโโโโโโโโ\n${player1Info[senderID].name}, ร ๐๐๐๐ ๐๐ ๐๐๐๐๐๐๐๐๐ (๐๐ก: b6 a5).\n๐| ๐ ๐๐๐ ๐๐๐๐๐๐ฃ ๐๐๐๐๐๐๐๐๐ ๐๐๐๐๐๐ ๐๐๐๐ ๐๐๐๐๐๐๐๐๐๐ \"๐๐๐๐๐๐๐\" ๐๐๐๐ ๐๐๐๐๐๐๐ ๐๐ ๐๐๐ !`,
- threadID,
- event.messageID
- );
- } else {
- player1Info = await api.getUserInfo([senderID]);
- player2Info = await api.getUserInfo([opponentID]);
- if (!player2Info[opponentID]) return api.sendMessage("Impossible de rรฉcupรฉrer les infos du joueur invitรฉ.", threadID, event.messageID);
- damierGames[gameID] = {
- board: createDamierBoard(),
- players: [
- { id: senderID, name: player1Info[senderID].name, color: "blanc" },
- { id: opponentID, name: player2Info[opponentID].name, color: "noir" }
- ],
- turn: 0,
- inProgress: true,
- vsBot: false
- };
- api.sendMessage(
- `๐ฃ| ๐ป๐๐๐๐๐๐๐๐ ๐'๐๐๐ ๐๐๐๐๐๐๐๐ ๐๐๐๐๐๐ ๐๐ ๐๐๐๐๐ ๐๐๐๐๐ ${player1Info[senderID].name} (โช) ๐๐ ${player2Info[opponentID].name} (โซ) !\nโโโโโโโโโชโโซโโโโโโโโ\n${displayDamier(damierGames[gameID].board)}\nโโโโโโโโโชโโซโโโโโโโโ\n${player1Info[senderID].name}, ร ๐๐๐๐ ๐๐ ๐๐๐๐๐๐๐๐๐ (๐๐ก: b6 a5).\n๐| ๐ ๐๐๐ ๐๐๐๐๐๐ฃ ๐๐๐๐๐๐๐๐๐ ๐๐๐๐๐๐ ๐๐๐๐ ๐๐๐๐๐๐๐๐๐๐ \"๐๐๐๐๐๐๐\" ๐๐๐๐ ๐๐๐๐๐๐๐ ๐๐ ๐๐๐ !`,
- threadID,
- event.messageID
- );
- }
- },
- onChat: async function ({ api, event }) {
- const threadID = event.threadID;
- const senderID = event.senderID;
- const messageBody = event.body.trim();
- // HELP et STATS mรชme pendant la partie
- if (messageBody.toLowerCase() === "help") return api.sendMessage(DAMES_HELP, threadID, event.messageID);
- if (messageBody.toLowerCase() === "stats") {
- const userStats = damierStats[senderID] || { wins: 0, losses: 0 };
- return api.sendMessage(
- `ใ ๐ณ๐ฐ๐ผ๐ด๐ ใ- ๐ฆ๐๐ฎ๐๐\n๐ค Nom : ${event.senderName || "Inconnu"}\n๐ ID : ${senderID}\nโ Victoires : ${userStats.wins}\nโ Dรฉfaites : ${userStats.losses}`,
- threadID,
- event.messageID
- );
- }
- const gameID = Object.keys(damierGames).find((id) =>
- id.startsWith(`${threadID}:`) && (id.includes(senderID) || id.endsWith(':BOT'))
- );
- if (!gameID) return;
- const game = damierGames[gameID];
- if (!game.inProgress) return;
- const board = game.board;
- const currentPlayer = game.players[game.turn];
- if (!game.vsBot && senderID != currentPlayer.id) {
- return api.sendMessage(`Ce n'est pas votre tour !`, threadID, event.messageID);
- }
- if (game.vsBot && game.turn === 1) return;
- if (["forfait", "abandon"].includes(messageBody.toLowerCase())) {
- const opponent = game.players.find(p => p.id != senderID);
- game.inProgress = false;
- if (opponent.id !== "BOT") updateStats(opponent.id, senderID);
- return api.sendMessage(`๐ณ๏ธ| ${currentPlayer.name} ๐ ๐๐๐๐๐๐๐๐รฉ ๐๐ ๐๐๐๐๐๐. ${opponent.name} ๐๐ ๐๐๐๐๐๐๐๐ ๐โจ !`, threadID);
- }
- if (["restart", "rejouer"].includes(messageBody.toLowerCase())) {
- const [player1, player2] = game.players;
- damierGames[gameID] = {
- board: createDamierBoard(),
- players: [player1, player2],
- turn: 0,
- inProgress: true,
- vsBot: game.vsBot
- };
- return api.sendMessage(
- `๐ฃ| ๐ฝ๐๐๐๐๐๐๐ ๐๐๐๐๐๐ ๐๐ ๐๐๐๐๐ ๐๐๐๐๐ ${player1.name} (โช) ๐๐ ${player2.name} (โซ) !\nโโโโโโโโโชโโซโโโโโโโโ\n${displayDamier(damierGames[gameID].board)}\nโโโโโโโโโชโโซโโโโโโโโ\n${player1.name}, ๐ฒ'๐๐๐ ๐๐๐๐ ๐๐๐ ๐๐๐๐๐๐๐๐๐ฃ (ex: b6 a5).\n๐| ๐ ๐๐๐ ๐๐๐๐๐๐ฃ ๐๐๐๐๐ ๐๐๐๐๐๐๐ ๐๐ ๐๐๐ ๐๐ ๐๐๐๐๐๐๐๐๐ ๐๐๐๐๐๐๐๐๐๐ "๐๐๐๐๐๐๐"`,
- threadID
- );
- }
- const move = parseDamierMove(messageBody);
- if (!move) {
- return api.sendMessage(`Mouvement invalide. Utilisez la notation : b6 a5`, threadID, event.messageID);
- }
- const [[fx, fy], [tx, ty]] = move;
- const piece = board[fx][fy];
- if (
- (game.turn === 0 && ![PION_B, DAME_B].includes(piece)) ||
- (game.turn === 1 && ![PION_N, DAME_N].includes(piece))
- ) {
- return api.sendMessage(`Vous ne pouvez dรฉplacer que vos propres pions !`, threadID, event.messageID);
- }
- const moveState = isValidMoveDamier(board, [fx, fy], [tx, ty], game.turn === 0 ? "blanc" : "noir");
- if (!moveState) {
- return api.sendMessage(`Coup illรฉgal ou impossible.`, threadID, event.messageID);
- }
- board[tx][ty] = piece;
- board[fx][fy] = EMPTY;
- if (moveState === "prise") {
- board[(fx + tx) / 2][(fy + ty) / 2] = EMPTY;
- }
- checkPromotion(board);
- const hasBlanc = hasPieces(board, PION_B, DAME_B);
- const hasNoir = hasPieces(board, PION_N, DAME_N);
- if (!hasBlanc || !hasNoir) {
- game.inProgress = false;
- const winner = hasBlanc ? game.players[0] : game.players[1];
- const loser = hasBlanc ? game.players[1] : game.players[0];
- if (winner.id !== "BOT" && loser.id !== "BOT") updateStats(winner.id, loser.id);
- if (winner.id !== "BOT" && loser.id === "BOT") updateStats(winner.id, "BOT");
- return api.sendMessage(
- `${displayDamier(board)}\n\n๐| ${winner.name} ๐๐๐๐๐๐๐๐ ๐๐ ๐๐๐๐๐๐ !`,
- threadID
- );
- }
- game.turn = (game.turn + 1) % 2;
- const nextPlayer = game.players[game.turn];
- if (game.vsBot && game.turn === 1) {
- await api.sendMessage(
- `${displayDamier(board)}\n\nโคใ ๐ท๐ด๐ณ๐ถ๐ด๐ท๐พ๐ถ๐๐ถ๐ฟ๐ ใโใ rรฉflรฉchit...๐ค`,
- threadID
- );
- setTimeout(async () => {
- await botPlay(game, api, threadID);
- }, 10000);
- } else {
- api.sendMessage(
- `${displayDamier(board)}\n\n${nextPlayer.name}, ๐'๐๐๐ ๐๐๐๐๐ ๐๐๐๐ !๐`,
- threadID
- );
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement