Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. getCellsToFlip: function getCellsToFlip(board, lastRow, lastCol) {
  2. function searchDirection(direction, opponentsLetter) {
  3. let num = 0;
  4. let done = false;
  5. let resultArr = [];
  6. let nextRow = lastRow;
  7. let nextCol = lastCol;
  8. while (!done) {
  9. num += 1;
  10. switch (direction) {
  11. case "N":
  12. nextRow = lastRow - num;
  13. nextCol = lastCol;
  14. break;
  15. case "NW":
  16. nextRow = lastRow - num;
  17. nextCol = lastCol - num;
  18. break;
  19. case "W":
  20. nextRow = lastRow;
  21. nextCol = lastCol - num;
  22. break;
  23. case "SW":
  24. nextRow = lastRow + num;
  25. nextCol = lastCol - num;
  26. break;
  27. case "S":
  28. nextRow = lastRow + num;
  29. nextCol = lastCol;
  30. break;
  31. case "SE":
  32. nextRow = lastRow + num;
  33. nextCol = lastCol + num;
  34. break;
  35. case "E":
  36. nextRow = lastRow;
  37. nextCol = lastCol + num;
  38. break;
  39. case "NE":
  40. nextRow = lastRow - num;
  41. nextCol = lastCol + num;
  42. break;
  43. }
  44. validSpace = withinBoundaries(board, nextRow, nextCol);
  45. if (!validSpace) {
  46. return [];
  47. }
  48. nextIndex = rev.rowColToIndex(board, nextRow, nextCol);
  49. if (board[nextIndex] === " ") {
  50. return [];
  51. }
  52.  
  53. if (board[nextIndex] !== opponentsLetter) {
  54. done = true;
  55. };
  56. if (board[nextIndex] === opponentsLetter) {
  57. resultArr.push([nextRow, nextCol]);
  58. }
  59. }
  60. return resultArr;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement