Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let chessTable = [
- new Array(" "," "," ","K"," "," "," "," "),
- new Array(" ","Q"," "," "," "," "," "," "),
- new Array(" "," "," "," "," "," "," "," "),
- new Array(" "," ","P"," ","P"," "," "," "),
- new Array("P"," "," "," "," "," "," "," "),
- new Array(" "," "," "," "," ","P"," "," "),
- new Array(" "," "," "," "," "," "," "," "),
- new Array(" "," "," "," "," "," "," "," "),
- ];
- let coordinatesK = [0, 3];
- let theKnightCanAttackTheQueen = false;
- function moveTheKnight (chessTable, row, col) {
- if(row - 1 > 0 && col - 2 > 0 && chessTable[row - 1][col - 2] != "P") {
- moveTheKnight (chessTable, row - 1, col - 2);
- if(chessTable[row- 1][col - 2] == "Q") {
- theKnightCanAttackTheQueen = true;
- chessTable[row- 1][col - 2] == "K";
- }
- } else if(row - 1 > 0 && col + 2 < chessTable[row].length && chessTable[row][col] == " ") {
- moveTheKnight (chessTable, row - 1, col + 2);
- if(chessTable[row- 1][col + 2] == "Q") {
- theKnightCanAttackTheQueen = true;
- chessTable[row- 1][col + 2] == "K";
- }
- } else if(row + 1 < chessTable.length && col - 2 > 0 && chessTable[row][col] != "P") {
- moveTheKnight (chessTable, row + 1, col - 2);
- if(chessTable[row + 1][col - 2] == "Q") {
- theKnightCanAttackTheQueen = true;
- chessTable[row + 1][col - 2] == "K";
- }
- } else if(row + 1 < chessTable.length && col + 2 < chessTable[row].length && chessTable[row][col] == " ") {
- moveTheKnight (chessTable, row + 1, col + 2);
- if(chessTable[row + 1][col + 2] == "Q") {
- theKnightCanAttackTheQueen = true;
- chessTable[row + 1][col + 2] == "K";
- }
- } else if(row - 2 > 0 && col - 1 > 0 && chessTable[row][col] == " ") {
- moveTheKnight (chessTable, row - 2, col - 1);
- if(chessTable[row - 2][col - 1] == "Q") {
- theKnightCanAttackTheQueen = true;
- chessTable[row - 2][col - 1] == "K";
- }
- } else if(row - 2 > 0 && col + 1 < chessTable[row.length] && chessTable[row][col] == " ") {
- moveTheKnight (chessTable, row - 2, col + 1);
- if(chessTable[row - 2][col + 1] == "Q") {
- theKnightCanAttackTheQueen = true;
- chessTable[row - 2][col + 1] == "K";
- }
- } else if(row + 2 < chessTable.length && col - 1 > 0 && chessTable[row][col] == " ") {
- moveTheKnight (chessTable, row + 2, col - 1);
- if(chessTable[row + 2][col - 1] == "Q") {
- theKnightCanAttackTheQueen = true;
- chessTable[row + 2][col - 1] == "K";
- }
- } else if(row + 2 < chessTable.length && col + 1 < chessTable[row].length && chessTable[row][col] == " ") {
- moveTheKnight (chessTable, row + 2, col + 1);
- if(chessTable[row + 2][col + 1] == "Q") {
- theKnightCanAttackTheQueen = true;
- chessTable[row + 2][col + 1] == "K";
- }
- }
- }
- moveTheKnight (chessTable, 0, 3);
- console.log(theKnightCanAttackTheQueen);
- console.table(chessTable);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement