Created
March 9, 2024 15:03
-
-
Save kowalski7cc/f4bc721f72d50b3d3ae14c8e37ab232f to your computer and use it in GitHub Desktop.
Eight queens puzzle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function checkBoard(board) { | |
const rowsValidity = board.map((row) => | |
checkSingleQueen(row) | |
) | |
const firstRow = board[0]; | |
const columns = getColumnsValidity(firstRow, board); | |
const diagonal = [] | |
board.forEach((row, x) => { | |
row.forEach((cell, y) => { | |
if (x === y) { | |
diagonal.push(board[x][y]); | |
} | |
}) | |
}) | |
return isListValid(rowsValidity) && isListValid(columns) && isListValid(checkSingleQueen(diagonal)) | |
} | |
function checkSingleQueen(boardElement) { | |
return boardElement.reduce( | |
(accumulator, cell) => accumulator + (cell === 'X' ? 1 : 0), | |
0, | |
) <= 1; | |
} | |
function getColumnsValidity(firstRow, board) { | |
return firstRow.map((_, i) => { | |
const currentColumn = getColumn(board, i) | |
return checkSingleQueen(currentColumn); | |
}); | |
} | |
function getColumn(board, columnIndex) { | |
return board.map((row) => { | |
return row[columnIndex] | |
}); | |
} | |
function isListValid(columns) { | |
return columns.filter((column) => column === false).length === 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {checkBoard} from "../src/index.js"; | |
describe("index", () => { | |
it("Data una griglia 1X1 vuota la griglia è valida", () => { | |
const a = [ | |
[''], | |
] | |
expect(checkBoard(a)).toEqual(true); | |
}); | |
it("Data una griglia 0X0 vuota la griglia è valida", () => { | |
const a = [ | |
[], | |
] | |
expect(checkBoard(a)).toEqual(true); | |
}); | |
it("Data una griglia 1X1 controllo se valida", () => { | |
const a = [['X']] | |
expect(checkBoard(a)).toEqual(true); | |
}); | |
it("Data una griglia 1X2 con due regine la griglia non è valida", () => { | |
const a = [['X', 'X']] | |
expect(checkBoard(a)).toEqual(false); | |
}); | |
it("Data una griglia 1X2 con una regina la griglia è valida", () => { | |
const a = [['X', '']] | |
expect(checkBoard(a)).toEqual(true); | |
}); | |
it("Data una griglia 2X2 con due regine la griglia non è valida", () => { | |
const a = [ | |
['X', ''], | |
['X', ''] | |
] | |
expect(checkBoard(a)).toEqual(false); | |
}); | |
it("Data una griglia 2X2 con due regine verticali la griglia non è valida", () => { | |
const a = [ | |
['', 'X'], | |
['', 'X'] | |
] | |
expect(checkBoard(a)).toEqual(false); | |
}); | |
it("Data una griglia 2X2 con due regine orizzontali la griglia non è valida", () => { | |
const a = [ | |
['', ''], | |
['X', 'X'] | |
] | |
expect(checkBoard(a)).toEqual(false); | |
}); | |
it("Data una griglia 2X2 con due regine in diagonale la griglia non è valida", () => { | |
const a = [ | |
['X', ''], | |
['', 'X'] | |
] | |
expect(checkBoard(a)).toEqual(false); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment