Skip to content

Commit

Permalink
Fixed game algorithm (hopefully for the last time)
Browse files Browse the repository at this point in the history
  • Loading branch information
scheng20 committed Mar 13, 2021
1 parent fdc8398 commit 86f9346
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions src/Game/Gomoku.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let STONE_BLACK = 1;
let STONE_WHITE = 2;

export default class GomokuGame {

constructor(size) {
this.currentColor = STONE_BLACK;
this.size = size;
Expand Down Expand Up @@ -137,35 +137,56 @@ export default class GomokuGame {
return this.testRows(transposed_board);

}

// Detects the right diagonal case of 5 in a row
testDiagonalR(board) {

let shifted_board = [];
let superString = "";

for(let i = 0; i < this.size; i++) {
shifted_board[i] = [];
for(let j = 0; j < this.size; j++) {
shifted_board[i][j] = board[i][i + j];

superString = "";

for(let k = 0; k < 5; k++) {
if(i + k < this.size && j + k < this.size) {
superString += board[i + k][j + k];
}
}

if(/(1{5,5})|(2{5,5})/.test(superString)) {
return true;
}

}
}

return this.testCols(shifted_board);

return false;
}

// Detects the left diagonal case of 5 in a row
testDiagonalL(board) {

let shifted_board = [];
let superString = "";

for(let i = 0; i < this.size; i++) {
shifted_board[i] = [];
for(let j = 0; j < this.size; j++) {
shifted_board[i][j] = board[i][this.size - 1 - i - j];

superString = "";

for(let k = 0; k < 5; k++) {
if(i + k < this.size && j - k >= 0) {
superString += board[i + k][j - k];
}
}

if(/(1{5,5})|(2{5,5})/.test(superString)) {
return true;
}

}
}

return this.testCols(shifted_board);
return false;
}
}

0 comments on commit 86f9346

Please sign in to comment.