Skip to content

Commit

Permalink
perf: disable scanning further in rows & columns for win counts < gri…
Browse files Browse the repository at this point in the history
…d size
  • Loading branch information
mrdcvlsc committed Sep 6, 2023
1 parent 3df277a commit c1ceb62
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/TicTacToe.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,12 @@ class TicTacToe {
return NA;
}

let scanSize = this.grid - (this.grid - this.pieceWinCount);

// check row -
for (let i = 0; i < this.grid; ++i) {
let samePiece = 1;
for (let j = 0; j < this.grid - 1; ++j) {
for (let j = 0; j < scanSize - 1; ++j) {
const CURRENT_INDEX = i * this.grid + j;
if (this.board[CURRENT_INDEX] === this.board[CURRENT_INDEX + 1] && this.board[CURRENT_INDEX] !== NA) {
samePiece++;
Expand All @@ -202,7 +204,7 @@ class TicTacToe {
// check columns |
for (let j = 0; j < this.grid; ++j) {
let samePiece = 1;
for (let i = 0; i < this.grid - 1; ++i) {
for (let i = 0; i < scanSize - 1; ++i) {
const CURRENT_INDEX = i * this.grid + j;
if (this.board[CURRENT_INDEX] === this.board[(i + 1) * this.grid + j] && this.board[CURRENT_INDEX] !== NA) {
samePiece++;
Expand Down

0 comments on commit c1ceb62

Please sign in to comment.