# PROLOG 8 QUEENS PROBLEM.

## Presentation on theme: "PROLOG 8 QUEENS PROBLEM."— Presentation transcript:

PROLOG 8 QUEENS PROBLEM

8 QUEENS Place 8 queens on an 8 X 8 chessboard so that:
Queens do not attack each other

8 QUEENS Solution will be programmed as a unary predicate
solution(Position) Will be true if Position represents a valid chessboard position for the 8 queens and no 2 queens can attack each other

8 QUEENS Representation of the chess board position
Each queen will be represented by X and Y (row and column) We will define a new operator, : X:Y Potential solution: [X1:Y1, X2:Y2, X3:Y3, …, X8:Y8]

8 QUEENS Since every queen will have to be in a different row 
We can hard code all the Xis to be 1 to 8 A potential solution can be templated as : [1:Y1, 2:Y2, 3:Y3, .., 8:Y8]

8 QUEENS Recursive formulation of the problem
We will “think” about this problem in terms of placing N queens not just 8  recursive relation between N queens and (N – 1) queens Use this to solve when number of queens is 8

8 QUEENS Recursive formulation of the problem
List of N queens Base case: list of queens is empty General case: list of queens looks like [X:Y | Others] and Others itself is a solution X and Y must be integers between 1 and 8 The X:Y queen must not attack any queen in Others

8 QUEENS solution([]). % no queen  good solution([X:Y | Others]) :-
solution(Others), member(Y,[1,2,3,4,5,6,7,8]), noattack(X:Y,Others). % need to define % X will be hard coded 1 to 8

8 QUEENS Need to define predicate noattack noattack takes 2 arguments:
A queen A list of queens Base case: list of queens is empty The queen cannot attack the queens in the empty list (there is not any)

8 QUEENS Base case for noattack noattack(_,[]).
A queen (1st argument) cannot be attacked by 0 queen

8 QUEENS General case If the list of queens is not empty, then it can be written [X1:Y1 | Others] where X1:Y1 represents the first queen (head) and Others the other queens (tail) noattack(X:Y,[X1:Y1 | Others]) :- ????

8 QUEENS noattack(X:Y,[X1:Y1 | Others]) :- ???? That will be true if:
Queen X:Y cannot attack queen X1:Y1, and Queen X:Y cannot attack any of the queens in the list of queens Others

8 QUEENS One queen vs another (X:Y vs X1:Y1)
X and X1 must be different but that is automatic (the Xis are hard coded) Y and Y1 must be different (column) Y1 – Y must be different from X1 – X (main diagonal) Y1 – Y must be different from X – X1 (other diagonal)

8 QUEENS One queen vs another (X:Y vs X1:Y1)
Y1 and Y must be different X1 and X are hard coded and automatically different Y1 – Y different from X1 – X Y1 – Y different from X – X1 We will use the =\= operator , arithmetic not equal

8 QUEENS General case for noattack noattack(X:Y,[X1:Y1 | Others]) :-
Y =\= Y1, Y1 – Y =\= X1 – X, Y1 – Y =\= X – X1, noattack(X:Y, Others). % X and X1 are hard coded different

8 QUEENS solution([]). % no queen solution([X:Y | Others]) :-
solution(Others), member(Y,[1,2,3,4,5,6,7,8]), noattack(X:Y,Others).

8 QUEENS noattack(_,[]). noattack(X:Y,[X1:Y1 | Others]) :- Y =\= Y1,
Y1 – Y =\= X1 – X, Y1 – Y =\= X – X1, noattack(X:Y, Others).

8 QUEENS Hard code the Xis into 1 to 8
template([1:Y1, 2:Y2, 3:Y3, …, 8:Y8]). To run the program: template(S), solution(S).