I'm currently in the process of learning JS and trying to recreate my first real project, a connect 4 minimax algorithm. I've somewhat gotten the algorithm to work, however it makes multiple moves inbetween every player move. I've tried moving around the inputs and changing up the algorithms a bit. Below I have provided the main game loop which it cycles through. If you want to see any of the other functions that are called I'd be happy to provide them.
Gif link that shows the phenomena
window.onload = function() {
let app = new PIXI.Application(
{
width: 700,
height: 700,
backgroundColor: 0X0047AB,
}
);
document.body.appendChild(app.view);
app.stage.interactive = true;
let text = new PIXI.Text("", {
fontFamily: "Arial",
fontSize: 24,
fill: 0x000000,
});
text.x = 10;
text.y = 650;
app.stage.addChild(text);
for (let r = 0; r < rows; r++) {
let row = [];
for (let c = 0; c < cols; c++) {
row.push(null);
}
grid.push(row);
}
app.stage.on('pointerdown', function (event) {
waitForPlayerInput(event, app, grid);
let winner = checkForWin(grid);
if (winner !== null) {
text.text = `Player ${winner} wins!`;
app.stage.off('pointerdown');
} else {
changeTurn();
if (turn === 1) {
text.text = `Player ${turn}'s turn`;
} else {
let bestMove = minimax(grid, turn, depth, -Infinity, Infinity);
grid[bestMove.row][bestMove.col] = turn;
changeTurn();
}
}
});
Via Active questions tagged javascript - Stack Overflow https://ift.tt/YEZ2VX8
Comments
Post a Comment