I wrote a kind of N-Queens algorithm, processing only the detection of vertical and horizontal threats. So this is more of a search for N-Towers solutions.
For this I use recursion. This is a well-known algorithm. For each square of a chessboard, I place a tower. For each placed tower, I try to place another tower (this is a recursive call). If there is no tower to accommodate the remaining tower, this means that the program has found a solution, and the recursive level should return. If all the chessboards intersect with the remaining tower (s), this means that the program has not found a solution, and the recursive level should return.
My recursive function has two parameters: the number of towers to be placed, and the chessboard (array of string array, string equal to "T" to indicate that the tower was placed on this square of the chessboard and "-" to indicate that the square empty).
Problem
My algorithm seems to find all the solutions and display them like chessboards using a β-β (and, if that worked, a βTβ). This notation is explained above.
However, even if the number of solutions seems to be correct, the displayed solutions / chessboards contain only β-β.
I think that I am not passing my array array (i.e. a chessboard) correctly in my recursive call.
Illustration of this problem
2 2 * 2 , . "-" ""... . :
-
-
:
function placeTower(number_of_left_towers, array_array_chessboard) {
if (number_of_left_towers == 0) {
return solutions.push(array_array_chessboard);
}
for (var current_x = 0; current_x < number_of_lines; current_x++) {
for (var current_y = 0; current_y < number_of_columns; current_y++) {
if (array_array_chessboard[current_x][current_y] == "-" && canBePlaced(array_array_chessboard, current_x, current_y)) {
array_array_chessboard[current_x][current_y] = "T";
placeTower(number_of_left_towers - 1, array_array_chessboard);
array_array_chessboard[current_x][current_y] = "-";
}
}
}
}
: JSFiddle
https://jsfiddle.net/btcj6uzp/
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Recursive algorithm of the N-Towers</title>
</head>
<body>
<script type="text/javascript">
function nTowersSolutions(number_of_towers, number_of_lines, number_of_columns) {
var solutions = [];
var array_array_chessboard = [];
for(var i = 0; i < number_of_lines; i++) {
array_array_chessboard[i] = new Array(number_of_columns);
for(var j = 0; j < number_of_columns; j++) {
array_array_chessboard[i][j] = "-";
}
}
this.displaySolutions = function() {
var body = document.body;
solutions.forEach((array_array_chessboard) => {
array_array_chessboard.forEach(function(array_chessboard) {
array_chessboard.forEach((square) => {
body.innerHTML += square;
});
body.innerHTML += "<br />";
});
body.innerHTML += "<br /><br />";
});
};
function placeTower(number_of_left_towers, array_array_chessboard) {
if (number_of_left_towers == 0) {
return solutions.push(array_array_chessboard);
}
for (var current_x = 0; current_x < number_of_lines; current_x++) {
for (var current_y = 0; current_y < number_of_columns; current_y++) {
if (array_array_chessboard[current_x][current_y] == "-" && canBePlaced(array_array_chessboard, current_x, current_y)) {
array_array_chessboard[current_x][current_y] = "T";
placeTower(number_of_left_towers - 1, array_array_chessboard);
array_array_chessboard[current_x][current_y] = "-";
}
}
}
}
function canBePlaced(array_array_chessboard, new_x, new_y) {
for(var i = 0; i < array_array_chessboard.length; i++) {
for(var z = 0; z < array_array_chessboard[i].length; z++) {
if(array_array_chessboard[i][z] == "T"
&& (
new_x == z || new_y == i
)
) {
return false;
}
}
}
return true;
}
placeTower(number_of_towers, array_array_chessboard);
return this;
}
nTowersSolutions(2, 2, 2).displaySolutions();
</script>
</body>
</html>