Skip to content

Instantly share code, notes, and snippets.

@huytd
Created March 24, 2020 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huytd/d9c162e8d6392c39595395521896c028 to your computer and use it in GitHub Desktop.
Save huytd/d9c162e8d6392c39595395521896c028 to your computer and use it in GitHub Desktop.
8-queen test
// Run online at:
// https://algorithm-pad.now.sh/?gist=https://gist.githubusercontent.com/huytd/d9c162e8d6392c39595395521896c028/raw/343c11a97c8edc5538ae88a3144e754be1162a74/8-queen.js
board = [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
]
debug(board)
const nextAvailable = () => {
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board.length; j++) {
if (board[i][j] === 0) return [i, j];
}
}
return false;
}
const placeQueen = (y, x) => {
if (board[y][x] === 0) {
board[y][x] = '*';
} else {
return false;
}
let i; let j;
// north
i = y - 1; j = x;
while (i >= 0) {
if (board[i][j] !== '*') {
board[i][j] = ' ';
i--;
} else {
return false;
}
}
// south
i = y + 1; j = x;
while (i < board.length) {
if (board[i][j] !== '*') {
board[i][j] = ' ';
i++;
} else {
return false;
}
}
// west
i = y; j = x - 1;
while (j >= 0) {
if (board[i][j] !== '*') {
board[i][j] = ' ';
j--;
} else {
return false;
}
}
// east
i = y; j = x + 1;
while (j < board.length) {
if (board[i][j] !== '*') {
board[i][j] = ' ';
j++;
} else {
return false;
}
}
// north west
i = y - 1; j = x - 1;
while (i >= 0 && j >= 0) {
if (board[i][j] !== '*') {
board[i][j] = ' ';
i--; j--;
} else {
return false;
}
}
// south west
i = y + 1; j = x - 1;
while (i < board.length && j >= 0) {
if (board[i][j] !== '*') {
board[i][j] = ' ';
i++; j--;
} else {
return false;
}
}
// south east
i = y + 1; j = x + 1;
while (i < board.length && j < board.length) {
if (board[i][j] !== '*') {
board[i][j] = ' ';
i++; j++;
} else {
return false;
}
}
// north east
i = y - 1; j = x + 1;
while (i >= 0 && j < board.length) {
if (board[i][j] !== '*') {
board[i][j] = ' ';
i--; j++;
} else {
return false;
}
}
return true;
}
const removeQueen = (y, x) => {
board[y][x] = 0;
let i; let j;
// north
i = y - 1; j = x;
while (i >= 0) {
board[i][j] = 0;
i--;
}
// south
i = y + 1; j = x;
while (i < board.length) {
board[i][j] = 0;
i++;
}
// west
i = y; j = x - 1;
while (j >= 0) {
board[i][j] = 0;
j--;
}
// east
i = y; j = x + 1;
while (j < board.length) {
board[i][j] = 0;
j++;
}
// north west
i = y - 1; j = x - 1;
while (i >= 0 && j >= 0) {
board[i][j] = 0;
i--; j--;
}
// south west
i = y + 1; j = x - 1;
while (i < board.length && j >= 0) {
board[i][j] = 0;
i++; j--;
}
// south east
i = y + 1; j = x + 1;
while (i < board.length && j < board.length) {
board[i][j] = 0;
i++; j++;
}
// north east
i = y - 1; j = x + 1;
while (i >= 0 && j < board.length) {
board[i][j] = 0;
i--; j++;
}
}
placeQueen(2, 3);
let cell = nextAvailable();
while (cell) {
let [y, x] = cell;
let ok = placeQueen(y, x);
if (ok) {
cell = nextAvailable();
} else {
removeQueen(y, x);
}
}
debug(board);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment