Skip to content

Commit

Permalink
implement move exploration order from center to edges
Browse files Browse the repository at this point in the history
  • Loading branch information
CortezSMz committed Apr 13, 2022
1 parent 1313208 commit b41266e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/engine/Board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/engine/GameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
25 changes: 21 additions & 4 deletions src/engine/Minimax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class Minimax {

this.depth = 5;
}

public getBestMove() {
const [moves, bestScore] = this.getMoves();

Expand All @@ -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);
Expand All @@ -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",
Expand All @@ -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 = {
Expand Down

0 comments on commit b41266e

Please sign in to comment.