From b41266ed47b531b9c5569af2455d954a588f35d8 Mon Sep 17 00:00:00 2001 From: CortezSMz Date: Tue, 12 Apr 2022 22:41:06 -0300 Subject: [PATCH] implement move exploration order from center to edges --- src/engine/Board.ts | 9 ++++++--- src/engine/GameManager.ts | 2 +- src/engine/Minimax.ts | 25 +++++++++++++++++++++---- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/engine/Board.ts b/src/engine/Board.ts index 734e683..b6f36f6 100644 --- a/src/engine/Board.ts +++ b/src/engine/Board.ts @@ -66,11 +66,14 @@ export class Board { return null; } - public allValidLocations(board: GridSlot[][]) { + public allValidLocations( + board: GridSlot[][], + colOrder: number[] = [...Array(this.grid[0].length).keys()] + ) { const validMoves: GridSlot[] = []; - for (let i = 0; i < board[0].length; i++) { - const isValid = this.isValidLocation(board, i); + for (const col of colOrder) { + const isValid = this.isValidLocation(board, col); if (isValid) validMoves.push(isValid); } diff --git a/src/engine/GameManager.ts b/src/engine/GameManager.ts index 9f2c1a0..d074b42 100644 --- a/src/engine/GameManager.ts +++ b/src/engine/GameManager.ts @@ -74,7 +74,7 @@ export default class GameManager { this.dropping = false; setTimeout(() => { - this.drop(move[0] || (move as unknown as number)); + this.drop(move[0]); }, 100); } diff --git a/src/engine/Minimax.ts b/src/engine/Minimax.ts index 49eaac1..7685fa7 100644 --- a/src/engine/Minimax.ts +++ b/src/engine/Minimax.ts @@ -11,6 +11,7 @@ export class Minimax { this.depth = 5; } + public getBestMove() { const [moves, bestScore] = this.getMoves(); @@ -26,7 +27,17 @@ export class Minimax { return randomBestMove; } - public getMoves(depth: number = this.depth) { + private colOrder() { + const arr = [...Array(this.manager.board.grid[0].length).keys()]; + + return arr.sort( + (a, b) => + Math.abs(a - Math.floor(arr.length / 2)) - + Math.abs(b - Math.floor(arr.length / 2)) || b - a + ); + } + + private getMoves(depth: number = this.depth) { const board = [...this.manager.board.grid]; const moves = this.minimax(board, depth, -Infinity, Infinity, true); @@ -50,9 +61,12 @@ export class Minimax { if (playing) { const max: (number | number[][])[] = [[], alpha]; - const validMoves = this.manager.board.allValidLocations(board); + const validMoves = this.manager.board.allValidLocations( + board, + this.colOrder() + ); - for (const { row, col, x, z } of validMoves) { + for (const { row, col, x, z } of validMoves.sort()) { board[row][col].disc = { dropped: false, color: "YELLOW", @@ -73,7 +87,10 @@ export class Minimax { return max; } else { const min: (number | number[][])[] = [[], beta]; - const validMoves = this.manager.board.allValidLocations(board); + const validMoves = this.manager.board.allValidLocations( + board, + this.colOrder() + ); for (const { row, col, x, z } of validMoves) { board[row][col].disc = {