Advertisement
dimytar11

Untitled

Nov 3rd, 2021
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. let chessTable = [
  2. new Array(" "," "," ","K"," "," "," "," "),
  3. new Array(" ","Q"," "," "," "," "," "," "),
  4. new Array(" "," "," "," "," "," "," "," "),
  5. new Array(" "," ","P"," ","P"," "," "," "),
  6. new Array("P"," "," "," "," "," "," "," "),
  7. new Array(" "," "," "," "," ","P"," "," "),
  8. new Array(" "," "," "," "," "," "," "," "),
  9. new Array(" "," "," "," "," "," "," "," "),
  10. ];
  11.  
  12. let coordinatesK = [0, 3];
  13.  
  14. let theKnightCanAttackTheQueen = false;
  15.  
  16. function moveTheKnight (chessTable, row, col) {
  17.  
  18. if(row - 1 > 0 && col - 2 > 0 && chessTable[row - 1][col - 2] != "P") {
  19. moveTheKnight (chessTable, row - 1, col - 2);
  20. if(chessTable[row- 1][col - 2] == "Q") {
  21. theKnightCanAttackTheQueen = true;
  22. chessTable[row- 1][col - 2] == "K";
  23. }
  24.  
  25. } else if(row - 1 > 0 && col + 2 < chessTable[row].length && chessTable[row][col] == " ") {
  26. moveTheKnight (chessTable, row - 1, col + 2);
  27. if(chessTable[row- 1][col + 2] == "Q") {
  28. theKnightCanAttackTheQueen = true;
  29. chessTable[row- 1][col + 2] == "K";
  30. }
  31.  
  32. } else if(row + 1 < chessTable.length && col - 2 > 0 && chessTable[row][col] != "P") {
  33. moveTheKnight (chessTable, row + 1, col - 2);
  34. if(chessTable[row + 1][col - 2] == "Q") {
  35. theKnightCanAttackTheQueen = true;
  36. chessTable[row + 1][col - 2] == "K";
  37. }
  38.  
  39. } else if(row + 1 < chessTable.length && col + 2 < chessTable[row].length && chessTable[row][col] == " ") {
  40. moveTheKnight (chessTable, row + 1, col + 2);
  41. if(chessTable[row + 1][col + 2] == "Q") {
  42. theKnightCanAttackTheQueen = true;
  43. chessTable[row + 1][col + 2] == "K";
  44. }
  45.  
  46. } else if(row - 2 > 0 && col - 1 > 0 && chessTable[row][col] == " ") {
  47. moveTheKnight (chessTable, row - 2, col - 1);
  48. if(chessTable[row - 2][col - 1] == "Q") {
  49. theKnightCanAttackTheQueen = true;
  50. chessTable[row - 2][col - 1] == "K";
  51. }
  52.  
  53. } else if(row - 2 > 0 && col + 1 < chessTable[row.length] && chessTable[row][col] == " ") {
  54. moveTheKnight (chessTable, row - 2, col + 1);
  55. if(chessTable[row - 2][col + 1] == "Q") {
  56. theKnightCanAttackTheQueen = true;
  57. chessTable[row - 2][col + 1] == "K";
  58. }
  59.  
  60. } else if(row + 2 < chessTable.length && col - 1 > 0 && chessTable[row][col] == " ") {
  61. moveTheKnight (chessTable, row + 2, col - 1);
  62. if(chessTable[row + 2][col - 1] == "Q") {
  63. theKnightCanAttackTheQueen = true;
  64. chessTable[row + 2][col - 1] == "K";
  65. }
  66.  
  67. } else if(row + 2 < chessTable.length && col + 1 < chessTable[row].length && chessTable[row][col] == " ") {
  68. moveTheKnight (chessTable, row + 2, col + 1);
  69. if(chessTable[row + 2][col + 1] == "Q") {
  70. theKnightCanAttackTheQueen = true;
  71. chessTable[row + 2][col + 1] == "K";
  72. }
  73. }
  74. }
  75.  
  76. moveTheKnight (chessTable, 0, 3);
  77.  
  78. console.log(theKnightCanAttackTheQueen);
  79.  
  80. console.table(chessTable);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement