From cf5576df219caf203a24775e037c98dc299ce211 Mon Sep 17 00:00:00 2001 From: mrdcvlsc Date: Thu, 7 Sep 2023 08:31:18 +0800 Subject: [PATCH] perf: scan only single diag lines if win counts == grid size --- lib/TicTacToe.js | 129 ++++++++++++++++++++++++++++++----------------- 1 file changed, 84 insertions(+), 45 deletions(-) diff --git a/lib/TicTacToe.js b/lib/TicTacToe.js index 38e1d8f..956ac5c 100644 --- a/lib/TicTacToe.js +++ b/lib/TicTacToe.js @@ -218,13 +218,15 @@ class TicTacToe { } } - // check diag \ - for (let i = 1; i < this.grid; ++i) { - let samePiece = 1, - prevPiece = null; - for (let j = 0; j < i + 1; ++j) { - const CURRENT_INDEX = this.grid - i + (this.grid + 1) * j - 1; - if (prevPiece === this.board[CURRENT_INDEX] && this.board[CURRENT_INDEX] !== NA) { + if (this.pieceWinCount == this.grid) { + // scans only the longest diagonals + let samePiece = 1; + for (let i = 0; i < this.grid - 1; ++i) { + const CURRENT_INDEX = i * this.grid + i; + if ( + this.board[CURRENT_INDEX] !== NA && + this.board[CURRENT_INDEX] === this.board[(i + 1) * this.grid + (i + 1)] + ) { samePiece++; if (samePiece === this.pieceWinCount) { this.winner = this.board[CURRENT_INDEX]; @@ -233,16 +235,15 @@ class TicTacToe { } else { samePiece = 1; } - prevPiece = this.board[CURRENT_INDEX]; } - } - for (let i = 1; i < this.grid - 1; ++i) { - let samePiece = 1, - prevPiece = null; - for (let j = 0; this.grid - i - j > 0; ++j) { - const CURRENT_INDEX = i * this.grid + (this.grid + 1) * j; - if (prevPiece === this.board[CURRENT_INDEX] && this.board[CURRENT_INDEX] !== NA) { + samePiece = 1; + for (let i = this.grid - 1; i > 0; --i) { + const CURRENT_INDEX = i * this.grid + (this.grid - 1 - i); + if ( + this.board[CURRENT_INDEX] !== NA && + this.board[CURRENT_INDEX] === this.board[(i - 1) * this.grid + (this.grid - i)] + ) { samePiece++; if (samePiece === this.pieceWinCount) { this.winner = this.board[CURRENT_INDEX]; @@ -251,44 +252,82 @@ class TicTacToe { } else { samePiece = 1; } - prevPiece = this.board[CURRENT_INDEX]; } - } + } else { + // scan all diagonals + + // check diag \ + for (let i = 1; i < this.grid; ++i) { + let samePiece = 1, + prevPiece = null; + for (let j = 0; j < i + 1; ++j) { + const CURRENT_INDEX = this.grid - i + (this.grid + 1) * j - 1; + if (prevPiece === this.board[CURRENT_INDEX] && this.board[CURRENT_INDEX] !== NA) { + samePiece++; + if (samePiece === this.pieceWinCount) { + this.winner = this.board[CURRENT_INDEX]; + return this.board[CURRENT_INDEX]; + } + } else { + samePiece = 1; + } + prevPiece = this.board[CURRENT_INDEX]; + } + } - // check diag / - for (let i = 1; i < this.grid; ++i) { - let samePiece = 1, - prevPiece = null; - for (let j = 0; j < i + 1; ++j) { - const CURRENT_INDEX = i + j * this.grid - j; - if (prevPiece === this.board[CURRENT_INDEX] && this.board[CURRENT_INDEX] !== NA) { - samePiece++; - if (samePiece === this.pieceWinCount) { - this.winner = this.board[CURRENT_INDEX]; - return this.board[CURRENT_INDEX]; + for (let i = 1; i < this.grid - 1; ++i) { + let samePiece = 1, + prevPiece = null; + for (let j = 0; this.grid - i - j > 0; ++j) { + const CURRENT_INDEX = i * this.grid + (this.grid + 1) * j; + if (prevPiece === this.board[CURRENT_INDEX] && this.board[CURRENT_INDEX] !== NA) { + samePiece++; + if (samePiece === this.pieceWinCount) { + this.winner = this.board[CURRENT_INDEX]; + return this.board[CURRENT_INDEX]; + } + } else { + samePiece = 1; } - } else { - samePiece = 1; + prevPiece = this.board[CURRENT_INDEX]; } - prevPiece = this.board[CURRENT_INDEX]; } - } - for (let i = 1; i < this.grid - 1; ++i) { - let samePiece = 1, - prevPiece = null; - for (let j = 0; this.grid - i - j > 0; ++j) { - const CURRENT_INDEX = this.grid * (i + 1) - 1 + (this.grid - 1) * j; - if (prevPiece === this.board[CURRENT_INDEX] && this.board[CURRENT_INDEX] !== NA) { - samePiece++; - if (samePiece === this.pieceWinCount) { - this.winner = this.board[CURRENT_INDEX]; - return this.board[CURRENT_INDEX]; + // check diag / + for (let i = 1; i < this.grid; ++i) { + let samePiece = 1, + prevPiece = null; + for (let j = 0; j < i + 1; ++j) { + const CURRENT_INDEX = i + j * this.grid - j; + if (prevPiece === this.board[CURRENT_INDEX] && this.board[CURRENT_INDEX] !== NA) { + samePiece++; + if (samePiece === this.pieceWinCount) { + this.winner = this.board[CURRENT_INDEX]; + return this.board[CURRENT_INDEX]; + } + } else { + samePiece = 1; } - } else { - samePiece = 1; + prevPiece = this.board[CURRENT_INDEX]; + } + } + + for (let i = 1; i < this.grid - 1; ++i) { + let samePiece = 1, + prevPiece = null; + for (let j = 0; this.grid - i - j > 0; ++j) { + const CURRENT_INDEX = this.grid * (i + 1) - 1 + (this.grid - 1) * j; + if (prevPiece === this.board[CURRENT_INDEX] && this.board[CURRENT_INDEX] !== NA) { + samePiece++; + if (samePiece === this.pieceWinCount) { + this.winner = this.board[CURRENT_INDEX]; + return this.board[CURRENT_INDEX]; + } + } else { + samePiece = 1; + } + prevPiece = this.board[CURRENT_INDEX]; } - prevPiece = this.board[CURRENT_INDEX]; } }