Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. function validCell (board, row, col) {
  2. if (row < 0 || row > 4 || col < 0 || col > 4) {
  3. return false;
  4. }
  5. if (board[row][col] !== 1) {
  6. return false;
  7. }
  8. return true;
  9. }
  10.  
  11. function validMoves (board, row, col) {
  12. let moves = [];
  13. if (!validCell(board, row, col)) {
  14. return moves;
  15. }
  16.  
  17. if (validCell(board, row+1, col)) {
  18. moves.push([row+1, col]);
  19. }
  20. if (validCell(board, row, col+1)) {
  21. moves.push([row,col+1]);
  22. }
  23. if (validCell(board, row-1, col)) {
  24. moves.push([row-1,col]);
  25. }
  26. if (validCell(board, row, col-1)) {
  27. moves.push([row, col-1]);
  28. }
  29.  
  30. return moves;
  31. }
  32.  
  33. let board = [
  34. [0, 1, 0, 0, 1],
  35. [1, 1, 0, 0, 0],
  36. [0, 0, 0, 0, 1],
  37. [1, 1, 0, 1, 1],
  38. [0, 1, 0, 0, 0]
  39. ];
  40. let row = 4;
  41. let column = 1;
  42.  
  43. console.log("Origin: (" + row + " , " + column + ")");
  44. console.log(validMoves(board, row, column));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement