Haskell program that solves "8 Queens" type puzzles
Haskell
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
ChessPuzzle.hs
Main.hs
README.md

README.md

ChessPuzzle

This is a haskell program that solves "8 Queens" type puzzles: i.e. list all of the possible ways in which some set of chess pieces can be placed on a chess board.

I wrote it over a weekend as part of a job interview.

It is my first serious effort written in Haskell, so please excuse me for anything that's horrible!

Any comments or suggestions on improving it are very welcome.

To compile:

  ghc -o chess ChessPuzzle.hs Main.hs

The output program runs with no arguments:

C:\...\ChessPuzzle>chess
Enter rows
4
Enter columns
4
Enter pieces. Any of ['PRNBQK']
KKBBBB
+----+
|BB.B|
|...B|
|K...|
|..K.|
+----+

+----+
|BB..|
|...K|
|B...|
|B.K.|
+----+

+----+
|B.BB|
|B...|
|...K|
|.K..|
+----+

+----+
|B.K.|
|B...|
|...K|
|BB..|
+----+

+----+
|.K.B|
|...B|
|K...|
|..BB|
+----+

+----+
|.K..|
|...K|
|B...|
|B.BB|
+----+

+----+
|..BB|
|K...|
|...B|
|.K.B|
+----+

+----+
|..K.|
|K...|
|...B|
|BB.B|
+----+

Total solutions: 8

If there are too many solutions to print, it instead works like this:

Enter rows
6
Enter columns
9
Enter pieces. Any of ['PRNBQK']
QRNBKK
Over 100 solutions...
Total solutions: 20136752

On my PC at home, this last problem took a little under 4 minutes to solve.