Skip to content

Mohamed #71

Description

@23moha67

const { Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');

const client = new Client({
authStrategy: new LocalAuth()
});

let game = {
board: [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
currentPlayer: 'X',
playing: false
};

function printBoard() {
return `
${game.board[0]} | ${game.board[1]} | ${game.board[2]}

${game.board[3]} | ${game.board[4]} | ${game.board[5]}

${game.board[6]} | ${game.board[7]} | ${game.board[8]}
`;
}

function checkWinner() {
const b = game.board;
const wins = [
[0,1,2],[3,4,5],[6,7,8],
[0,3,6],[1,4,7],[2,5,8],
[0,4,8],[2,4,6]
];
for (let [a,b1,c] of wins) {
if (b[a] != ' ' && b[a] == b[b1] && b[a] == b[c]) {
return true;
}
}
return false;
}

client.on('qr', qr => {
qrcode.generate(qr, { small: true });
});

client.on('ready', () => {
console.log('✅ البوت شغال!');
});

client.on('message', message => {
const chat = message.from;
if (message.body === '!start') {
if (game.playing) {
client.sendMessage(chat, '⚠️ اللعبة جارية بالفعل.');
} else {
game.board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '];
game.currentPlayer = 'X';
game.playing = true;
client.sendMessage(chat, '🎮 بدأت لعبة X O!\nاكتب رقم الخانة (1-9) للعب.\n' + printBoard());
}
} else if (game.playing && /^[1-9]$/.test(message.body)) {
let pos = parseInt(message.body) - 1;
if (game.board[pos] === ' ') {
game.board[pos] = game.currentPlayer;
if (checkWinner()) {
client.sendMessage(chat, printBoard() + \n🏆 اللاعب ${game.currentPlayer} فاز!);
game.playing = false;
} else if (!game.board.includes(' ')) {
client.sendMessage(chat, printBoard() + '\n🤝 تعادل!');
game.playing = false;
} else {
game.currentPlayer = game.currentPlayer === 'X' ? 'O' : 'X';
client.sendMessage(chat, printBoard() + \nدور اللاعب ${game.currentPlayer});
}
} else {
client.sendMessage(chat, '⚠️ هذه الخانة مشغولة، اختر خانة أخرى.');
}
}
});

client.initialize();

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions