use !ttt to play tic tac toe with ai! Thanks for Nintendo_bot#7518 (503483896101470220) for idea! Also thanks for Jacob's tic tac toe snippet! (btw I'm sure you can't win this ai, can you win?)
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
var ttt = require('tic-tac-toe-ai-engine');
let emptyTile = 'β¬';
let piece1 = 'β';
let piece2 = 'π΅';
var gameState = ['', '', '', '', '', '', '', '', ''];
let event = context.params.event;
let reply = event.referenced_message;
let currentBoard;
function MakeEmoji() {
let temp = [];
for (let i = 0; i < 9; i++) {
if (gameState[i] == 'X') {
temp.push(piece1);
} else if (gameState[i] == 'O') {
temp.push(piece2);
} else {
temp.push(emptyTile);
}
}
let temp2 = ``;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
temp2 += temp[i * 3 + j];
}
temp2 += `\n`;
}
return temp2;
}
function EmojiToText() {
let temp = [];
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (currentBoard[i][j] == piece1) {
temp.push(`X`);
} else if (currentBoard[i][j] == emptyTile) {
temp.push(``);
} else {
temp.push(`O`);
}
}
}
return temp;
}
if (!reply) {
await lib.discord.channels['@0.1.1'].messages.create({
channel_id: `${event.channel_id}`,
content: [
`<@!${event.author.id}>, it's your turn! You are ${piece1}.`,
``,
`Reply to this message and type \`!ttt \` / \`!ttt \` that you want to place!`,
].join('\n'),
embed: {
type: 'rich',
description: MakeEmoji(),
color: 0x00aaaa,
},
});
} else if (reply.author.bot && reply.content.startsWith(`<@`)) {
await lib.discord.channels['@0.1.2'].messages.destroy({
message_id: `${context.params.event.id}`,
channel_id: `${context.params.event.channel_id}`,
});
currentBoard = reply.embeds[0].description.split('\n').map((row) => {
return [...row];
});
gameState = EmojiToText();
try {
let pos =
(event.content.split(` `)[2] - 1) * 3 + (event.content.split(` `)[1] - 1);
if (gameState[pos] == ``) {
gameState[pos] = `X`;
} else {
return lib.discord.channels['@0.1.2'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `Sorry <@!${event.author.id}>, ${event.content.substring(
5
)} has already been played!`,
});
}
if (ttt.computeMove(gameState).depth <= 1) {
gameState = ttt.computeMove(gameState).nextBestGameState;
let winner;
if (ttt.computeMove(gameState).winner == '') {
winner = `Tie!`;
} else if (ttt.computeMove(gameState).winner == `X`) {
winner = `<@${event.author.id}> Win!`;
} else {
winner = `<@${event.author.id}> Lost`;
}
await lib.discord.channels['@0.1.1'].messages.update({
message_id: reply.id,
channel_id: `${event.channel_id}`,
content: `${winner}`,
embed: {
type: 'rich',
description: MakeEmoji(),
color: 0x00aaaa,
},
});
return;
}
gameState = ttt.computeMove(gameState).nextBestGameState;
await lib.discord.channels['@0.1.1'].messages.update({
message_id: reply.id,
channel_id: `${event.channel_id}`,
content: [
`<@!${event.author.id}>, it's your turn! You are ${piece1}.`,
``,
`Reply to this message and type \`!ttt \` / \`!ttt \` that you want to place!`,
].join('\n'),
embed: {
type: 'rich',
description: MakeEmoji(),
color: 0x00aaaa,
},
});
} catch (e) {
return lib.discord.channels['@0.1.2'].messages.create({
channel_id: `${event.channel_id}`,
content: `Something went wrong. Please try again \`!ttt \` / \`!ttt \``,
});
}
}