Skip to content

Commit

Permalink
perf: disable eval in early turns
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdcvlsc committed Sep 8, 2023
1 parent 16f1662 commit 4ab94d6
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 212 deletions.
6 changes: 2 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# [1.1.0](https://github.com/mrdcvlsc/minimax-tic-tac-toe/compare/v1.0.1...v1.1.0) (2023-07-13)


### Features

* detect other diagonal wins for lower win piece counts ([9f7b8c9](https://github.com/mrdcvlsc/minimax-tic-tac-toe/commit/9f7b8c93b2c6686314b7b45249d0f4748b8f3143))

- detect other diagonal wins for lower win piece counts ([9f7b8c9](https://github.com/mrdcvlsc/minimax-tic-tac-toe/commit/9f7b8c93b2c6686314b7b45249d0f4748b8f3143))

### Performance Improvements

* micro optimized the evaluate function ([c40aace](https://github.com/mrdcvlsc/minimax-tic-tac-toe/commit/c40aace9ec18120af1348133d1383b43c83eaf93))
- micro optimized the evaluate function ([c40aace](https://github.com/mrdcvlsc/minimax-tic-tac-toe/commit/c40aace9ec18120af1348133d1383b43c83eaf93))

# [1.1.0](https://github.com/mrdcvlsc/minimax-tic-tac-toe/compare/v1.0.1...v1.1.0) (2023-07-03)

Expand Down
10 changes: 8 additions & 2 deletions lib/TicTacToe.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class TicTacToe {
this.board = new Uint8Array(this.grid * this.grid);
this.currentPlayer = P1;
this.turns = this.grid * this.grid;
this.evalThreshold = this.turns - ((pieceWinCount * 2) - 1);

this.makeMove = this.makeMove.bind(this);
this.makeComputerMove = this.makeComputerMove.bind(this);
Expand Down Expand Up @@ -175,7 +176,12 @@ class TicTacToe {
* In other board game engines like; chess and GO the evaluation function might be separated.
* @returns no winner `0` | player X `1` | player O `2`.
*/
evaluate() {
evaluate(isTest = false) {
if (this.turns > this.evalThreshold && !isTest) {
this.winner = NA;
return NA;
}

// check row -
for (let i = 0; i < this.grid; ++i) {
let samePiece = 1;
Expand Down Expand Up @@ -366,7 +372,7 @@ class TicTacToe {
this.board[j] = boardStates[i][j];
}

if (this.evaluate() === winners[i]) {
if (this.evaluate(true) === winners[i]) {
console.log('test ', i + 1, ' : PASSED');
} else {
console.log('test ', i + 1, ' : FAILED');
Expand Down
Loading

0 comments on commit 4ab94d6

Please sign in to comment.