package showcase.nqueenspuzzle;
import java.util.ArrayList;
import bits.BooleanLiteral;
import bits.Conjunction;
import bits.INaturalNumber;
import bits.IProblem;
import bits.IProblemMessage;
import bits.Problem;
import naturalnumbers.NaturalNumber;
import naturalnumbers.NaturalNumberAbsoluteDifferencer;
import naturalnumbers.NaturalNumberBounder;
import naturalnumbers.NaturalNumberPair;
import naturalnumbers.NaturalNumberUnequalizer;
public class NQueensDemo
{
public static void main(String[] args) throws Exception
{
/**
* Set Java constants:
*/
int N = 10;
/**
* Set globals:
*/
NaturalNumber.setLargestNaturalNumber(N - 1);
/**
* Create Saffron objects and arrays:
*/
NaturalNumberPair[] placement = new NaturalNumberPair[N];
{
placement[i] = new NaturalNumberPair(new NaturalNumber(), new NaturalNumber());
}
/**
* Create problems which constrain the values of Saffron objects:
*/
for (int i = 0; i < N; i++)
{
parray.add(new NaturalNumberBounder(placement[i].getFirst(), N - 1));
parray.add(new NaturalNumberBounder(placement[i].getSecond(), N - 1));
}
for (int i = 0; i < N; i++)
for (int j = i + 1; j < N; j++)
{
parray.add(notAttacking(placement[i], placement[j]));
}
/**
* Create the Conjunction of all of these constraining problems:
*/
IProblem problem = new Conjunction(parray);
/**
* Solve the Conjunction:
*/
IProblemMessage s = problem.findModel(Problem.defaultSolver());
if (s.getStatus() == IProblemMessage.SATISFIABLE)
{
BooleanLiteral.interpret(s.getLiterals());
System.out.println("\n\tSOLUTION\n--------------------------");
for (int i = 0; i < N; i++)
{
System.out.println("Queen on row " + placement[i].getSecond() + ", column " + placement[i].getFirst());
}
Object[][] board=new Object[N][N];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
{
board[i][j]=" .";
}
for (int i = 0; i < N; i++)
{
board[(int) placement[i].getFirst().getValue()][ (int) placement[i].getSecond().getValue()]=" Q";
}
for (int i = 0; i < N; i++)
{
System.out.print("\n");
for (int j = 0; j < N; j++)
{
System.out.print(board[j][i]);
}
}
}
else
System.out.println("No solution.");
SOLUTION
--------------------------
Queen on row 1, column 2
Queen on row 7, column 4
Queen on row 3, column 7
Queen on row 2, column 5
Queen on row 5, column 0
Queen on row 9, column 3
Queen on row 6, column 8
Queen on row 0, column 6
Queen on row 4, column 9
Queen on row 8, column 1
. . . . . . Q . . .
. . Q . . . . . . .
. . . . . Q . . . .
. . . . . . . Q . .
. . . . . . . . . Q
Q . . . . . . . . .
. . . . . . . . Q .
. . . . Q . . . . .
. Q . . . . . . . .
. . . Q . . . . . .