Eight Queens

We’ve been dancing around Prolog’s core capabilities as we gather enough information to solve a nontrivial problem. It’s time to attack one. Eight Queens is an iconic programming problem involving the queen piece in a game of chess. Chess is played on an eight by eight grid, and the queen is the strongest piece that can attack along rows, columns, or diagonals. In this problem, we’re looking for a solution that places eight queens on a board without any two attacking each other. That means no two queens can share rows, columns, or diagonals.

We’re going to solve the problem in a naive way first and then refactor our solution to be more efficient. Let’s first think about the problem with some exploratory code. Then, we’ll hone in ...

Get Programmer Passport: Prolog now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.