Javascript Source Code
// eight-queens
// Author: Hannes du Plooy
// Date: 20 Sep 2016
// Objective: To solve the following
// Search for a chess board solution with 8 queens and no one
// endangering any other.
var board = [0,0,0,0,0,0,0,0];
function EightQueens(line) {
if (line > 7) {
return true;
}
for(var column=0;column<8;column++) {
var endangered = false;
for(var line2=0;line2<ine;line2++) {
if (board[line2] == column || Math.abs(line-line2) == Math.abs(column-board[line2])) {
endangered = true;
break;
}
}
if (!endangered) {
board[line] = column;
if (EightQueens(line+1)) {
return true;
}
}
}
return false;
}
if (EightQueens(0)) {
for(var i=0;i<8;i++) {
var tmp = "";
for(var j=0;j<8;j++) {
if (board[i] == j) {
tmp += "Q";
} else {
tmp += ".";
}
}
console.log(tmp);
}
}