3 SHARES Share Tweet

The Eight Queens Problem refers to a puzzle where you place eight chess queens on an 8×8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal. This solver uses a recursive function, solveQueens, to attempt to place queens in every single combination until a solution is found. The solveQueens function adds queens one row at a time, but a separate isSolution function checks every queen’s diagonals to see whether any of them threaten each other. Finally, for dramatic effect, each frame of the computation is stored in an arraylist that is displayed one frame at a time for the user to see. By the time the animation starts loading, the problem has already been solved fully, and the sketch simply plays back the solution!

Code for the animation follows。 You can edit and run this code on the KTBYTE Coder