Advertisement
Shisui_Daniel

Untitled

Jul 16th, 2025
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 17.18 KB | Source Code | 0 0
  1. const fetch = require('node-fetch');
  2.  
  3. const damierGames = {};
  4. const damierStats = {}; // Pour stocker les statistiques
  5.  
  6. const EMPTY = "๐ŸŸฉ";
  7. const PION_B = "โšช";
  8. const PION_N = "โšซ";
  9. const DAME_B = "๐Ÿ”ต";
  10. const DAME_N = "๐Ÿ”ด";
  11.  
  12. function createDamierBoard() {
  13.   const board = Array.from({ length: 8 }, () => Array(8).fill(EMPTY));
  14.   for (let i = 0; i < 3; i++) {
  15.     for (let j = 0; j < 8; j++) {
  16.       if ((i + j) % 2 === 1) board[i][j] = PION_N;
  17.     }
  18.   }
  19.   for (let i = 5; i < 8; i++) {
  20.     for (let j = 0; j < 8; j++) {
  21.       if ((i + j) % 2 === 1) board[i][j] = PION_B;
  22.     }
  23.   }
  24.   return board;
  25. }
  26.  
  27. function displayDamier(board) {
  28.   let s = "  a b c d e f g h\n";
  29.   for (let i = 0; i < 8; i++) {
  30.     s += (8 - i) + " ";
  31.     for (let j = 0; j < 8; j++) {
  32.       s += board[i][j] + " ";
  33.     }
  34.     s += "\n";
  35.   }
  36.   return s;
  37. }
  38.  
  39. function parseDamierMove(move) {
  40.   const regex = /^([a-h][1-8])\s+([a-h][1-8])$/i;
  41.   const match = move.match(regex);
  42.   if (!match) return null;
  43.   const pos = (p) => [8 - Number(p[1]), p.charCodeAt(0) - 97];
  44.   return [pos(match[1].toLowerCase()), pos(match[2].toLowerCase())];
  45. }
  46.  
  47. function isInside(x, y) {
  48.   return x >= 0 && x < 8 && y >= 0 && y < 8;
  49. }
  50.  
  51. function hasPieces(board, pion, dame) {
  52.   return board.flat().some(cell => cell === pion || cell === dame);
  53. }
  54.  
  55. function isValidMoveDamier(board, from, to, player) {
  56.   const [fx, fy] = from, [tx, ty] = to;
  57.   if (!isInside(fx, fy) || !isInside(tx, ty)) return false;
  58.   const piece = board[fx][fy];
  59.   if (board[tx][ty] !== EMPTY) return false;
  60.  
  61.   // Pion blanc
  62.   if (piece === PION_B) {
  63.     if (fx - tx === 1 && Math.abs(ty - fy) === 1) return true;
  64.     if (fx - tx === 2 && Math.abs(ty - fy) === 2) {
  65.       const midX = fx - 1;
  66.       const midY = fy + (ty - fy) / 2;
  67.       if (board[midX][midY] === PION_N || board[midX][midY] === DAME_N) return "prise";
  68.     }
  69.   }
  70.   // Pion noir
  71.   if (piece === PION_N) {
  72.     if (tx - fx === 1 && Math.abs(ty - fy) === 1) return true;
  73.     if (tx - fx === 2 && Math.abs(ty - fy) === 2) {
  74.       const midX = fx + 1;
  75.       const midY = fy + (ty - fy) / 2;
  76.       if (board[midX][midY] === PION_B || board[midX][midY] === DAME_B) return "prise";
  77.     }
  78.   }
  79.   // Dame blanche
  80.   if (piece === DAME_B) {
  81.     if (Math.abs(fx - tx) === Math.abs(fy - ty)) {
  82.       const dx = tx > fx ? 1 : -1, dy = ty > fy ? 1 : -1;
  83.       let x = fx + dx, y = fy + dy, found = false;
  84.       while (x !== tx && y !== ty) {
  85.         if (board[x][y] === PION_N || board[x][y] === DAME_N) {
  86.           if (found) return false;
  87.           found = true;
  88.         } else if (board[x][y] !== EMPTY) return false;
  89.         x += dx; y += dy;
  90.       }
  91.       return found ? "prise" : true;
  92.     }
  93.   }
  94.   // Dame noire
  95.   if (piece === DAME_N) {
  96.     if (Math.abs(fx - tx) === Math.abs(fy - ty)) {
  97.       const dx = tx > fx ? 1 : -1, dy = ty > fy ? 1 : -1;
  98.       let x = fx + dx, y = fy + dy, found = false;
  99.       while (x !== tx && y !== ty) {
  100.         if (board[x][y] === PION_B || board[x][y] === DAME_B) {
  101.           if (found) return false;
  102.           found = true;
  103.         } else if (board[x][y] !== EMPTY) return false;
  104.         x += dx; y += dy;
  105.       }
  106.       return found ? "prise" : true;
  107.     }
  108.   }
  109.   return false;
  110. }
  111.  
  112. function checkPromotion(board) {
  113.   for (let j = 0; j < 8; j++) {
  114.     if (board[0][j] === PION_B) board[0][j] = DAME_B;
  115.     if (board[7][j] === PION_N) board[7][j] = DAME_N;
  116.   }
  117. }
  118.  
  119. function getAllLegalMoves(board, player) {
  120.   const moves = [];
  121.   const myPion = player === 0 ? PION_B : PION_N;
  122.   const myDame = player === 0 ? DAME_B : DAME_N;
  123.   for (let fx = 0; fx < 8; fx++) {
  124.     for (let fy = 0; fy < 8; fy++) {
  125.       if ([myPion, myDame].includes(board[fx][fy])) {
  126.         for (let tx = 0; tx < 8; tx++) {
  127.           for (let ty = 0; ty < 8; ty++) {
  128.             if ((fx !== tx || fy !== ty) && isValidMoveDamier(board, [fx, fy], [tx, ty], player === 0 ? "blanc" : "noir")) {
  129.               moves.push([[fx, fy], [tx, ty]]);
  130.             }
  131.           }
  132.         }
  133.       }
  134.     }
  135.   }
  136.   return moves;
  137. }
  138.  
  139. function updateStats(winnerID, loserID) {
  140.   if (!damierStats[winnerID]) damierStats[winnerID] = { wins: 0, losses: 0 };
  141.   if (!damierStats[loserID]) damierStats[loserID] = { wins: 0, losses: 0 };
  142.   damierStats[winnerID].wins++;
  143.   damierStats[loserID].losses++;
  144. }
  145.  
  146. const DAMES_HELP = `
  147. ใ€Ž ๐™ณ๐™ฐ๐™ผ๐™ด๐š‚ ใ€- ๐—ฅรจ๐—ด๐—น๐—ฒ๐˜€ & ๐—–๐—ผ๐—บ๐—บ๐—ฎ๐—ป๐—ฑ๐—ฒ๐˜€
  148.  
  149. ๐ŸŽฒ ๐™ฑ๐šž๐š ๐š๐šž ๐š“๐šŽ๐šž :
  150. Dรฉplacer vos pions en diagonale pour capturer tous ceux de lโ€™adversaire ou le bloquer.
  151.  
  152. ๐Ÿ”ธ Un pion avance en diagonale.
  153. ๐Ÿ”น Capture : sautez par-dessus un pion adverse.
  154. ๐Ÿ”ธ Dame : atteint le dernier rang (๐Ÿ”ต ou ๐Ÿ”ด), peut avancer/reculer en diagonale.
  155.  
  156. ๐Ÿ“ ๐™ฒ๐š˜๐š–๐š–๐šŠ๐š—๐š๐šŽ๐šœ ๐š™๐š›๐š’๐š—๐šŒ๐š’๐š™๐šŠ๐š•๐šŽ๐šœ :
  157. - dames @ami : commencez contre un ami mentionnรฉ.
  158. - dames <ID> : dรฉfiez un joueur par son ID.
  159. - dames bot : joue contre le bot.
  160. - dames stats : affiche vos statistiques.
  161. - dames help : affiche ce message dโ€™aide.
  162.  
  163. ๐Ÿ’ฌ ๐™ด๐š— ๐šŒ๐š˜๐šž๐š›๐šœ ๐š๐šŽ ๐š™๐šŠ๐š›๐š๐š’๐šŽ :
  164. - Ex: b6 a5 pour dรฉplacer un pion.
  165. - forfait / abandon : quitter la partie.
  166. - restart / rejouer : recommencer une partie.
  167.  
  168. Bon jeu ! ๐ŸŽ‰
  169. `;
  170.  
  171. async function botPlay(game, api, threadID) {
  172.   const board = game.board;
  173.   const moves = getAllLegalMoves(board, 1);
  174.   if (moves.length === 0) {
  175.     game.inProgress = false;
  176.     const winner = game.players[0];
  177.     if (winner.id !== "BOT") updateStats(winner.id, "BOT");
  178.     await api.sendMessage(
  179.       `${displayDamier(board)}\n\n๐ŸŽ‰| ${winner.name} ๐š›๐šŽ๐š–๐š™๐š˜๐š›๐š๐šŽ ๐š•๐šŠ ๐š™๐šŠ๐š›๐š๐š’๐šŽ !`,
  180.       threadID
  181.     );
  182.     return;
  183.   }
  184.   let botMove = moves.find(([from, to]) => isValidMoveDamier(board, from, to, "noir") === "prise");
  185.   if (!botMove) botMove = moves[0];
  186.  
  187.   const [[fx, fy], [tx, ty]] = botMove;
  188.   const piece = board[fx][fy];
  189.   board[tx][ty] = piece;
  190.   board[fx][fy] = EMPTY;
  191.   if (isValidMoveDamier(board, [fx, fy], [tx, ty], "noir") === "prise") {
  192.     board[(fx + tx) / 2][(fy + ty) / 2] = EMPTY;
  193.   }
  194.   checkPromotion(board);
  195.  
  196.   const hasBlanc = hasPieces(board, PION_B, DAME_B);
  197.   const hasNoir = hasPieces(board, PION_N, DAME_N);
  198.   if (!hasBlanc || !hasNoir) {
  199.     game.inProgress = false;
  200.     const winner = hasBlanc ? game.players[0] : game.players[1];
  201.     if (winner.id !== "BOT") updateStats(winner.id, "BOT");
  202.     await api.sendMessage(
  203.       `${displayDamier(board)}\n\n๐ŸŽ‰| ${winner.name} ๐š๐šŽ๐š–๐š™๐š˜๐š›๐š๐šŽ ๐š•๐šŠ ๐š™๐šŠ๐š›๐š๐š’๐šŽ !`,
  204.       threadID
  205.     );
  206.     return;
  207.   }
  208.  
  209.   game.turn = 0;
  210.   await api.sendMessage(
  211.     `${displayDamier(board)}\n\n๐™ฒ'๐šŽ๐šœ๐š ๐šŸ๐š˜๐š๐š›๐šŽ ๐š๐š˜๐šž๐š› !๐Ÿ”„`,
  212.    threadID
  213.  );
  214. }
  215.  
  216. module.exports = {
  217.  config: {
  218.    name: "dames",
  219.    aliases: ["damiers", "checkers"],
  220.    version: "1.2",
  221.    author: "ใƒŸโ˜…๐’๐Ž๐๐ˆ๐‚โœ„๐„๐—๐„ 3.0โ˜…ๅฝก",
  222.    category: "game",
  223.    shortDescription: "Jouez aux dames contre un ami ou le bot.",
  224.    usage: "dames @ami | dames <ID> | dames bot | dames stats | dames help"
  225.  },
  226.  
  227.  onStart: async function ({ api, event, args }) {
  228.    const threadID = event.threadID;
  229.    const senderID = event.senderID;
  230.    let opponentID;
  231.    let playWithBot = false;
  232.  
  233.    // Aide
  234.    if (args[0] && args[0].toLowerCase() === "help") {
  235.      return api.sendMessage(DAMES_HELP, threadID, event.messageID);
  236.    }
  237.  
  238.    // Stats
  239.    if (args[0] && args[0].toLowerCase() === "stats") {
  240.      const userStats = damierStats[senderID] || { wins: 0, losses: 0 };
  241.      return api.sendMessage(
  242.        `ใ€Ž ๐™ณ๐™ฐ๐™ผ๐™ด๐š‚ ใ€- ๐—ฆ๐˜๐—ฎ๐˜๐˜€\n๐Ÿ‘ค Nom : ${event.senderName || "Inconnu"}\n๐Ÿ†” ID : ${senderID}\nโœ… Victoires : ${userStats.wins}\nโŒ Dรฉfaites : ${userStats.losses}`,
  243.        threadID,
  244.        event.messageID
  245.      );
  246.    }
  247.  
  248.    // Jouer contre le bot uniquement si on รฉcrit "bot"
  249.    if (args[0] && args[0].toLowerCase() === "bot") playWithBot = true;
  250.    else if (event.mentions && Object.keys(event.mentions).length > 0)
  251.      opponentID = Object.keys(event.mentions)[0];
  252.    else if (args[0] && /^\d+$/.test(args[0])) opponentID = args[0];
  253.  
  254.    if (!opponentID && !playWithBot) {
  255.      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);
  256.    }
  257.  
  258.    if (opponentID && opponentID == senderID)
  259.      return api.sendMessage("Vous ne pouvez pas jouer contre vous-mรชme !", threadID, event.messageID);
  260.  
  261.    // Rรฉcupรฉration nom auteur via API
  262.    let authorName = "ใƒŸโ˜…๐’๐Ž๐๐ˆ๐‚โœ„๐„๐—๐„ 3.0โ˜…ๅฝก";
  263.    try {
  264.      const authorResponse = await fetch('https://author-name.vercel.app/');
  265.       const authorJson = await authorResponse.json();
  266.       authorName = authorJson.author || authorName;
  267.     } catch (e) {}
  268.  
  269.     const gameID = playWithBot
  270.       ? `${threadID}:${senderID}:BOT`
  271.       : `${threadID}:${Math.min(senderID, opponentID)}:${Math.max(senderID, opponentID)}`;
  272.  
  273.     if (damierGames[gameID] && damierGames[gameID].inProgress)
  274.       return api.sendMessage("โŒ| ๐š„๐š—๐šŽ ๐š™๐šŠ๐š›๐š๐š’๐šŽ ๐šŽ๐šœ๐š ๐š๐šŽ๐š“๐šŠ ๐šŽ๐š— ๐šŒ๐š˜๐šž๐š›๐šœ ๐šŽ๐š—๐š๐š›๐šŽ ๐š๐šŽ๐šœ ๐š“๐š˜๐šž๐šŽ๐šž๐š›๐šœ. ๐š…๐šŽ๐šž๐š’๐š•๐š•๐šŽ๐šฃ ๐š™๐šŠ๐š๐š’๐šŽ๐š—๐š๐šŽ๐š› โณ.", threadID, event.messageID);
  275.  
  276.     let player1Info, player2Info, botName = "โžคใ€Ž ๐™ท๐™ด๐™ณ๐™ถ๐™ด๐™ท๐™พ๐™ถ๐„ž๐™ถ๐™ฟ๐šƒ ใ€โ˜œใƒ…";
  277.     if (playWithBot) {
  278.       player1Info = await api.getUserInfo([senderID]);
  279.       damierGames[gameID] = {
  280.         board: createDamierBoard(),
  281.         players: [
  282.           { id: senderID, name: player1Info[senderID].name, color: "blanc" },
  283.           { id: "BOT", name: botName, color: "noir" }
  284.         ],
  285.         turn: 0,
  286.         inProgress: true,
  287.         vsBot: true
  288.       };
  289.       api.sendMessage(
  290.         `๐Ÿ“ฃ| ๐™ป๐šŠ๐š—๐šŒ๐šŽ๐š–๐šŽ๐š—๐š ๐š'๐šž๐š—๐šŽ ๐š—๐š˜๐šž๐šŸ๐šŽ๐š•๐š•๐šŽ ๐š™๐šŠ๐š›๐š๐š’๐šŽ ๐š๐šŽ ๐š๐šŠ๐š–๐šŽ๐šœ ๐šŽ๐š—๐š๐š›๐šŽ ${player1Info[senderID].name} (โšช) ๐šŽ๐š ${botName} (โšซ) !\nโ”โ”โ”โ”โ”โ”โ”โ”โชโโซโ”โ”โ”โ”โ”โ”โ”โ”\n${displayDamier(damierGames[gameID].board)}\nโ”โ”โ”โ”โ”โ”โ”โ”โชโโซโ”โ”โ”โ”โ”โ”โ”โ”\n${player1Info[senderID].name}, ร  ๐šŸ๐š˜๐šž๐šœ ๐š๐šŽ ๐šŒ๐š˜๐š–๐š–๐šŽ๐š—๐šŒ๐šŽ๐š› (๐šŽ๐šก: b6 a5).\n๐Ÿ“›| ๐š…๐š˜๐šž๐šœ ๐š™๐š˜๐šž๐šŸ๐šŽ๐šฃ ๐šŽ๐š๐šŠ๐š•๐šŽ๐š–๐šŽ๐š—๐š ๐šœ๐šŠ๐š’๐šœ๐š’๐š› ๐š๐š˜๐šž๐š ๐šœ๐š’๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š \"๐š๐š˜๐š›๐š๐šŠ๐š’๐š\" ๐š™๐š˜๐šž๐š› ๐šœ๐š๐š˜๐š™๐š™๐šŽ๐š› ๐š•๐šŽ ๐š“๐šŽ๐šž !`,
  291.        threadID,
  292.        event.messageID
  293.      );
  294.    } else {
  295.      player1Info = await api.getUserInfo([senderID]);
  296.      player2Info = await api.getUserInfo([opponentID]);
  297.      if (!player2Info[opponentID]) return api.sendMessage("Impossible de rรฉcupรฉrer les infos du joueur invitรฉ.", threadID, event.messageID);
  298.  
  299.      damierGames[gameID] = {
  300.        board: createDamierBoard(),
  301.        players: [
  302.          { id: senderID, name: player1Info[senderID].name, color: "blanc" },
  303.          { id: opponentID, name: player2Info[opponentID].name, color: "noir" }
  304.        ],
  305.        turn: 0,
  306.        inProgress: true,
  307.        vsBot: false
  308.      };
  309.  
  310.      api.sendMessage(
  311.        `๐Ÿ“ฃ| ๐™ป๐šŠ๐š—๐šŒ๐šŽ๐š–๐šŽ๐š—๐š ๐š'๐šž๐š—๐šŽ ๐š—๐š˜๐šž๐šŸ๐šŽ๐š•๐š•๐šŽ ๐š™๐šŠ๐š›๐š๐š’๐šŽ ๐š๐šŽ ๐š๐šŠ๐š–๐šŽ๐šœ ๐šŽ๐š—๐š๐š›๐šŽ ${player1Info[senderID].name} (โšช) ๐šŽ๐š ${player2Info[opponentID].name} (โšซ) !\nโ”โ”โ”โ”โ”โ”โ”โ”โชโโซโ”โ”โ”โ”โ”โ”โ”โ”\n${displayDamier(damierGames[gameID].board)}\nโ”โ”โ”โ”โ”โ”โ”โ”โชโโซโ”โ”โ”โ”โ”โ”โ”โ”\n${player1Info[senderID].name}, ร  ๐šŸ๐š˜๐šž๐šœ ๐š๐šŽ ๐šŒ๐š˜๐š–๐š–๐šŽ๐š—๐šŒ๐šŽ๐š› (๐šŽ๐šก: b6 a5).\n๐Ÿ“›| ๐š…๐š˜๐šž๐šœ ๐š™๐š˜๐šž๐šŸ๐šŽ๐šฃ ๐šŽ๐š๐šŠ๐š•๐šŽ๐š–๐šŽ๐š—๐š ๐šœ๐šŠ๐š’๐šœ๐š’๐š› ๐š๐š˜๐šž๐š ๐šœ๐š’๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š \"๐š๐š˜๐š›๐š๐šŠ๐š’๐š\" ๐š™๐š˜๐šž๐š› ๐šœ๐š๐š˜๐š™๐š™๐šŽ๐š› ๐š•๐šŽ ๐š“๐šŽ๐šž !`,
  312.        threadID,
  313.        event.messageID
  314.      );
  315.    }
  316.  },
  317.  
  318.  onChat: async function ({ api, event }) {
  319.    const threadID = event.threadID;
  320.    const senderID = event.senderID;
  321.    const messageBody = event.body.trim();
  322.  
  323.    // HELP et STATS mรชme pendant la partie
  324.    if (messageBody.toLowerCase() === "help") return api.sendMessage(DAMES_HELP, threadID, event.messageID);
  325.    if (messageBody.toLowerCase() === "stats") {
  326.      const userStats = damierStats[senderID] || { wins: 0, losses: 0 };
  327.      return api.sendMessage(
  328.        `ใ€Ž ๐™ณ๐™ฐ๐™ผ๐™ด๐š‚ ใ€- ๐—ฆ๐˜๐—ฎ๐˜๐˜€\n๐Ÿ‘ค Nom : ${event.senderName || "Inconnu"}\n๐Ÿ†” ID : ${senderID}\nโœ… Victoires : ${userStats.wins}\nโŒ Dรฉfaites : ${userStats.losses}`,
  329.        threadID,
  330.        event.messageID
  331.      );
  332.    }
  333.  
  334.    const gameID = Object.keys(damierGames).find((id) =>
  335.      id.startsWith(`${threadID}:`) && (id.includes(senderID) || id.endsWith(':BOT'))
  336.    );
  337.    if (!gameID) return;
  338.    const game = damierGames[gameID];
  339.    if (!game.inProgress) return;
  340.  
  341.    const board = game.board;
  342.    const currentPlayer = game.players[game.turn];
  343.  
  344.    if (!game.vsBot && senderID != currentPlayer.id) {
  345.      return api.sendMessage(`Ce n'est pas votre tour !`, threadID, event.messageID);
  346.    }
  347.    if (game.vsBot && game.turn === 1) return;
  348.  
  349.    if (["forfait", "abandon"].includes(messageBody.toLowerCase())) {
  350.      const opponent = game.players.find(p => p.id != senderID);
  351.      game.inProgress = false;
  352.      if (opponent.id !== "BOT") updateStats(opponent.id, senderID);
  353.      return api.sendMessage(`๐Ÿณ๏ธ| ${currentPlayer.name} ๐šŠ ๐šŠ๐š‹๐šŠ๐š—๐š๐š˜๐š—๐š—รฉ ๐š•๐šŠ ๐š™๐šŠ๐š›๐š๐š’๐šŽ. ${opponent.name} ๐š•๐šŠ ๐š›๐šŽ๐š–๐š™๐š˜๐š›๐š๐šŽ ๐ŸŽ‰โœจ !`, threadID);
  354.    }
  355.  
  356.    if (["restart", "rejouer"].includes(messageBody.toLowerCase())) {
  357.      const [player1, player2] = game.players;
  358.      damierGames[gameID] = {
  359.        board: createDamierBoard(),
  360.        players: [player1, player2],
  361.        turn: 0,
  362.        inProgress: true,
  363.        vsBot: game.vsBot
  364.      };
  365.      return api.sendMessage(
  366.        `๐Ÿ“ฃ| ๐™ฝ๐š˜๐šž๐šŸ๐šŽ๐š•๐š•๐šŽ ๐š™๐šŠ๐š›๐š๐š’๐šŽ ๐š๐šŽ ๐š๐šŠ๐š–๐šŽ๐šœ ๐šŽ๐š—๐š๐š›๐šŽ ${player1.name} (โšช) ๐šŽ๐š ${player2.name} (โšซ) !\nโ”โ”โ”โ”โ”โ”โ”โ”โชโโซโ”โ”โ”โ”โ”โ”โ”โ”\n${displayDamier(damierGames[gameID].board)}\nโ”โ”โ”โ”โ”โ”โ”โ”โชโโซโ”โ”โ”โ”โ”โ”โ”โ”\n${player1.name}, ๐™ฒ'๐šŽ๐šœ๐š ๐šŸ๐š˜๐šž๐šœ ๐šš๐šž๐š’ ๐šŒ๐š˜๐š–๐š–๐šŽ๐š—๐šŒ๐šŽ๐šฃ (ex: b6 a5).\n๐Ÿ“›| ๐š…๐š˜๐šž๐šœ ๐š™๐š˜๐šž๐šŸ๐šŽ๐šฃ ๐šŠ๐šž๐šœ๐šœ๐š’ ๐šœ๐š๐š˜๐š™๐š™๐šŽ๐š› ๐š•๐šŽ ๐š“๐šŽ๐šž ๐šŽ๐š— ๐šœ๐šŠ๐š’๐šœ๐š’๐šœ๐šŠ๐š—๐š ๐šœ๐š’๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š "๐š๐š˜๐š›๐š๐šŠ๐š’๐š"`,
  367.        threadID
  368.      );
  369.    }
  370.  
  371.    const move = parseDamierMove(messageBody);
  372.    if (!move) {
  373.      return api.sendMessage(`Mouvement invalide. Utilisez la notation : b6 a5`, threadID, event.messageID);
  374.    }
  375.  
  376.    const [[fx, fy], [tx, ty]] = move;
  377.    const piece = board[fx][fy];
  378.  
  379.    if (
  380.      (game.turn === 0 && ![PION_B, DAME_B].includes(piece)) ||
  381.      (game.turn === 1 && ![PION_N, DAME_N].includes(piece))
  382.    ) {
  383.      return api.sendMessage(`Vous ne pouvez dรฉplacer que vos propres pions !`, threadID, event.messageID);
  384.    }
  385.  
  386.    const moveState = isValidMoveDamier(board, [fx, fy], [tx, ty], game.turn === 0 ? "blanc" : "noir");
  387.    if (!moveState) {
  388.      return api.sendMessage(`Coup illรฉgal ou impossible.`, threadID, event.messageID);
  389.    }
  390.  
  391.    board[tx][ty] = piece;
  392.    board[fx][fy] = EMPTY;
  393.    if (moveState === "prise") {
  394.      board[(fx + tx) / 2][(fy + ty) / 2] = EMPTY;
  395.    }
  396.    checkPromotion(board);
  397.  
  398.    const hasBlanc = hasPieces(board, PION_B, DAME_B);
  399.    const hasNoir = hasPieces(board, PION_N, DAME_N);
  400.    if (!hasBlanc || !hasNoir) {
  401.      game.inProgress = false;
  402.      const winner = hasBlanc ? game.players[0] : game.players[1];
  403.      const loser = hasBlanc ? game.players[1] : game.players[0];
  404.      if (winner.id !== "BOT" && loser.id !== "BOT") updateStats(winner.id, loser.id);
  405.      if (winner.id !== "BOT" && loser.id === "BOT") updateStats(winner.id, "BOT");
  406.      return api.sendMessage(
  407.        `${displayDamier(board)}\n\n๐ŸŽ‰| ${winner.name} ๐š›๐šŽ๐š–๐š™๐š˜๐š›๐š๐šŽ ๐š•๐šŠ ๐š™๐šŠ๐š›๐š๐š’๐šŽ  !`,
  408.        threadID
  409.      );
  410.    }
  411.  
  412.    game.turn = (game.turn + 1) % 2;
  413.    const nextPlayer = game.players[game.turn];
  414.  
  415.    if (game.vsBot && game.turn === 1) {
  416.      await api.sendMessage(
  417.        `${displayDamier(board)}\n\nโžคใ€Ž ๐™ท๐™ด๐™ณ๐™ถ๐™ด๐™ท๐™พ๐™ถ๐„ž๐™ถ๐™ฟ๐šƒ ใ€โ˜œใƒ… rรฉflรฉchit...๐Ÿค”`,
  418.        threadID
  419.      );
  420.      setTimeout(async () => {
  421.        await botPlay(game, api, threadID);
  422.      }, 10000);
  423.    } else {
  424.      api.sendMessage(
  425.        `${displayDamier(board)}\n\n${nextPlayer.name}, ๐šŒ'๐šŽ๐šœ๐š ๐šŸ๐š˜๐š๐š›๐šŽ ๐š๐š˜๐šž๐š› !๐Ÿ”„`,
  426.        threadID
  427.      );
  428.    }
  429.  }
  430. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement