Skip to content

Commit

Permalink
Connect-four updates
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMonDon committed Oct 5, 2023
1 parent e63f987 commit 26918af
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions commands/Games/connect4.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ class Connect4 extends Command {
const playerTwo = opponent.user;
const board = generateBoard();
let lastMove = 'None';
let gameOver = false;
let winner = null;

// Global var for updating the collected interaction
Expand Down Expand Up @@ -249,13 +248,11 @@ class Connect4 extends Command {
let displayName = opponentUser.displayName;

// Replace content and move if the game is over
if (gameOver) {
if (AIEngine.gameStatus().gameOver) {
content = winner ? `Congrats, ${winner}!` : "Looks like it's a draw...";
move = `Final Move: **${lastMove}**`;
displayName = currentUser.displayName;
}

if (opponentUser.bot) {
} else if (opponentUser.bot) {
content = '';
move = `I placed mine in **${lastMove}**.`;
}
Expand All @@ -271,7 +268,7 @@ class Connect4 extends Command {
`,
);

const buttons = await getButtons(gameOver);
const buttons = await getButtons(AIEngine.gameStatus().gameOver);

// Return an object without content built in so it doesn't edit a space
if (!content) return { embeds: [embed], components: buttons };
Expand All @@ -287,7 +284,7 @@ class Connect4 extends Command {
let currentEmoji;
let opponentEmoji;

while (!gameOver && board.some((row) => row.includes(null))) {
while (!AIEngine.gameStatus().gameOver && board.some((row) => row.includes(null))) {
let sign;
if (turn === 1) {
currentUser = playerOne;
Expand Down Expand Up @@ -317,9 +314,13 @@ class Connect4 extends Command {
const content = await getContent(currentUser, opponentUser, opponentEmoji, currentEmoji);
await collected.editReply(content).catch(console.error);

if (AIEngine.winner) console.log(`The winner is ${AIEngine.winner}`);
if (verifyWin(board)) {
console.log('verifiyWin');
winner = currentUser;
gameOver = true;
AIEngine.gameOver = true;
// Log this to see what I can use
console.log(AIEngine);
break;
}
turn === 1 ? (turn = 2) : (turn = 1);
Expand All @@ -337,7 +338,7 @@ class Connect4 extends Command {

if (!collected) {
// They never pressed the button, end the game due to time and edit the message directly since collected doesn't exist
gameOver = true;
AIEngine.gameOver = true;
winner = 'time';
this.client.games.delete(msg.channel.id);

Expand All @@ -362,7 +363,7 @@ class Connect4 extends Command {
} else if (selectedColumn.toLowerCase() === 'stop') {
// The player pressed the stop sign, stop the game and let the other person win.
winner = opponentUser;
gameOver = true;
AIEngine.gameOver = true;
break;
} else {
i = Number.parseInt(selectedColumn, 10) - 1;
Expand All @@ -385,7 +386,7 @@ class Connect4 extends Command {
// Check if the last move made them a winner, stop the game
if (verifyWin(board)) {
winner = currentUser;
gameOver = true;
AIEngine.gameOver = true;
break;
}

Expand All @@ -396,6 +397,7 @@ class Connect4 extends Command {
// Change turn to the opposite of what it is
turn === 1 ? (turn = 2) : (turn = 1);
}
console.log('game over');

// Delete the game and set content to function rather than pasting it three times
this.client.games.delete(msg.channel.id);
Expand All @@ -410,6 +412,7 @@ class Connect4 extends Command {
// Check if the game ever played so we can edit the interaction, or just edit the message
if (collected) return collected.editReply(content).catch(console.error);

console.log('finished');
return message.edit(content).catch(console.error);
} catch (err) {
// An error occurred, delete the game and send the error to chat.
Expand Down

0 comments on commit 26918af

Please sign in to comment.