Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- getCellsToFlip: function getCellsToFlip(board, lastRow, lastCol) {
- function searchDirection(direction, opponentsLetter) {
- let num = 0;
- let done = false;
- let resultArr = [];
- let nextRow = lastRow;
- let nextCol = lastCol;
- while (!done) {
- num += 1;
- switch (direction) {
- case "N":
- nextRow = lastRow - num;
- nextCol = lastCol;
- break;
- case "NW":
- nextRow = lastRow - num;
- nextCol = lastCol - num;
- break;
- case "W":
- nextRow = lastRow;
- nextCol = lastCol - num;
- break;
- case "SW":
- nextRow = lastRow + num;
- nextCol = lastCol - num;
- break;
- case "S":
- nextRow = lastRow + num;
- nextCol = lastCol;
- break;
- case "SE":
- nextRow = lastRow + num;
- nextCol = lastCol + num;
- break;
- case "E":
- nextRow = lastRow;
- nextCol = lastCol + num;
- break;
- case "NE":
- nextRow = lastRow - num;
- nextCol = lastCol + num;
- break;
- }
- validSpace = withinBoundaries(board, nextRow, nextCol);
- if (!validSpace) {
- return [];
- }
- nextIndex = rev.rowColToIndex(board, nextRow, nextCol);
- if (board[nextIndex] === " ") {
- return [];
- }
- if (board[nextIndex] !== opponentsLetter) {
- done = true;
- };
- if (board[nextIndex] === opponentsLetter) {
- resultArr.push([nextRow, nextCol]);
- }
- }
- return resultArr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement