Here is the puzzle:Put il queens (a figure in chest) on the chessboard with quadratic dimensions a x a. The figures cannot attack each other!This is my solution (my first lengthy programme in Java):Chessboard:/** *  * @author Julian_ * *         Quadratic chessboard, the meaning of fields: 0 - free field 1 - field *         which is being attack by a figure/figures. *  *         Class contains 3 main methods: putting figures on the chessboard, *         taking away figures from the chess board and marking the fields which *         are being attack by a figure/figures. *          *         values of the chessboard: *         0 - empty field *         1 - field being attacked by any figure/figures */public class Chessboard {    int a; // length of the side of the chess board measured in the amount of            // fields    int[][] tab = null; // the chessboard    Chessboard(int a) {        super();        this.a = a;        tab = new int[a][a];    }    /**     * Put a figure on the selected field on the chessboard, if the field is     * free (there is other figure placed and the field is not being attacked by     * any other figure placed on the chess board. The putting is made by     * changing value of the field to FIGURE_ID of the figure. The method also     * calls updateFields() method.     *      * @param x     *            horizontal coordinate of the chessboard     * @param y     *            vertical coordinate of the chessboard     * @param figureId     *            the id of the figure which is to placed     * @return is it possible to putting there a figure, the side-effect is     *         changing the value of the field where figure is put to the value     *         of FIGURE_ID of the put figure.     */    public boolean putFigure(int x, int y, Figure fig) {        if (tab[y][x] != 0 && tab[y][x] != 1) {            return false;        } else {            tab[y][x] = fig.getId();            this.updateFields(fig);            return true;        }    }    /**     * Takes away a figure from the chosen field by changing it's value to 0     * with setFree() method.     *      * @param x     *            horizontal coordinate of the chessboard     * @param y     *            vertical coordinate of the chessboard     */    public void unputFigure(int x, int y, Figure fig) {        setFree(x, y);        this.updateFields(fig);    }    /**     * Marks attacked fields on the chessboard by analyse of the put figures on     * the chess board.     */    public void updateFields(Figure fig) {        for (int j = 0; j < tab.length; j++) {            for (int i = 0; i < tab.length; i++) {                if (tab[j][i] == 1) {                    this.setFree(i, j);                }            }        }        for (int j = 0; j < tab.length; j++) {            for (int i = 0; i < tab.length; i++) {                if (tab[j][i] > 1) {                    defineAttack(i, j, fig);                }            }        }    }    /**     * Calls setAttacked() method to set a field into being attacked.     *      * @param x     *            horizontal coordinate of the chessboard     * @param y     *            vertical coordinate of the chessboard     */    public void defineAttack(int x, int y, Figure fig) {        for (int j = 0; j < tab.length; j++) {            for (int i = 0; i < tab[j].length; i++) {                if (tab[j][i] == 0 && fig.isAttacked(x, y, i, j)) {                    this.setAttacked(i, j);                }            }        }    }    /**     * Set a field into being attacked by changing it's value to 0.     *      * @param x     *            horizontal coordinate of the chessboard     * @param y     *            vertical coordinate of the chessboard     */    public void setAttacked(int x, int y) {        tab[y][x] = 1;    }    /**     * Set free the selected field by changing it's value to 0.     *      * @param x     *            horizontal coordinate of the chessboard     * @param y     *            vertical coordinate of the chessboard     */    public void setFree(int x, int y) {        tab[y][x] = 0;    }    /**     *      * @return the lenght of the chessboard measured by the number of fields of     *         1 side.     */    public int getA() {        return a;    }    /**     * @param figureId     *            id figure to being count     * @return the number of selected figures located on the chessboard.     */    public int countFigures(Figure fig) {        int c = 0;        for (int j = 0; j < tab.length; j++) {            for (int i = 0; i < tab[j].length; i++) {                if (tab[j][i] == fig.getId()) {                    c++;                }            }        }        return c;    }    /**     *      * @param x     *            horizontal coordinate of the chessboard     * @param y     *            vertical coordinate of the chessboard     * @return value of the selected field. If the field is outside the chessboard it returns -1.     */    public int checkPosition(int x, int y) {        try {            return tab[y][x];        } catch (ArrayIndexOutOfBoundsException e) {            return -1;        }    }    /**     * Visualise the chessboard     */    public void show() {        for (int j = 0; j < tab.length; j++) {            for (int i = 0; i < tab[j].length; i++) {                System.out.print(tab[j][i] +  );            }            System.out.println();        }        System.out.println( ------------- );    }}Figure:/** * FIGURE_ID: * pawn: 2 * knight: 3 * runner: 4 * tower: 5 * the queen: 8 * the king: 9 *  * cannot be 0 or 1 * */public abstract class Figure{    private int FIGURE_ID;    public int getId(){        return FIGURE_ID;    }    public abstract boolean isAttacked(int x, int y, int xx, int yy);}The Queen:public class Queen extends Figure {    final private int FIGURE_ID = 8;    public int getId() {        return FIGURE_ID;    }    public boolean isAttacked(int x, int y, int xx, int yy) {        return (x == xx || y == yy || Math.abs(x - xx) == Math.abs(y - yy));    }}The solution:public class Solution {    /**     * First loops are for checking all possibile start - coordinates. For     * instance: if method would not find a solution for starting coordinates     * [0][0] it will loop to [0][1] and so on until if will find the solution     * or until [][] == [a][a] where a is the lenght of the chess board measured     * in fields number.     *      * Next it put a figure near the last figure and so on in recursive way. If     * the number of figures will be equal the expected value algorithms return     * true. If not it returns falls and backtrack to the last puting on figure     * and tryin to make it in a different way.     *      * @param cb     *            the chessboard     * @param il     *            how many figures do you want to put on the chessboard     * @return is the solution found, if yes it also write the chessboard on the     *         screen.     */    public static boolean rekur(Chessboard cb, Figure fig, int il) {        boolean x = false;        int j = 0, i = 0;        while (x == false && j < Math.min(il, cb.getA())) {            while (x == false && i < Math.min(il, cb.getA())) {                x = Solution.rekur(i, j, cb, fig, il);                i++;            }            j++;        }        if (x) {            cb.show();        }        return x;    }    /**     *      * @param x horizontal coordinate of the chessboard     * @param y vertical coordinate of the chessboard     * look at upward at the comments     */    public static boolean rekur(int x, int y, Chessboard cb, Figure fig, int il) {        int kx = 1, ky = 1;        cb.putFigure(x, y, fig);        if (cb.countFigures(fig) >= il) {            return true;        }        while (cb.countFigures(fig) < il && ky < cb.getA()) {            kx = 1;            while (cb.countFigures(fig) < il && kx < cb.getA()) {                // System.out.println(cb.checkPosition(x, y));                if (cb.checkPosition(x, y + ky) == 0 && rekur(x, y + ky, cb, fig, il)) {                    return true;                }                if (cb.checkPosition(x + kx, y + ky) == 0 && rekur(x + kx, y + ky, cb, fig, il)) {                    return true;                }                if (cb.checkPosition(x + kx, y) == 0 && rekur(x + kx, y, cb, fig, il)) {                    return true;                }                if (cb.checkPosition(x + kx, y - ky) == 0 && rekur(x + kx, y - ky, cb, fig, il)) {                    return true;                }                if (cb.checkPosition(x, y - ky) == 0 && rekur(x, y - ky, cb, fig, il)) {                    return true;                }                if (cb.checkPosition(x - kx, y - ky) == 0 && rekur(x - kx, y - ky, cb, fig, il)) {                    return true;                }                if (cb.checkPosition(x - kx, y) == 0 && rekur(x - kx, y, cb, fig, il)) {                    return true;                }                if (cb.checkPosition(x - kx, y + ky) == 0 && rekur(x - kx, y + ky, cb, fig, il)) {                    return true;                }                kx++;            }            ky++;        }        cb.unputFigure(x, y, fig);        return false;    }    /**     *      * GET THE BALL ROLLING!     *      */    public static void main(String args[]) {        Chessboard cb = new Chessboard(8);        Figure queen = new Queen();        //boolean x = cb.putFigure(1, 1, queen);        boolean x = Solution.rekur(cb, queen, 8);        //boolean x = cb.putFigure(1, 2, queen);        //cb.show();        System.out.println(x);    }}
Chessboard puzzle in Java
java;n queens
My first focus is on one method in this answer, and that is the last rekur() of the Solution class. I'm leaving the rest for others to review.Let me present an alternate solution based upon your code, and then discuss it afterwards related to your code:public static boolean rekur(int x, int y, Chessboard cb, Figure fig, int il) {    int kx, ky, dim, figureCount;    cb.putFigure(x, y, fig);    figureCount = cb.countFigures(fig);    dim = cb.getA();    // Have we reached the correct number of figures?     if (dim >= il) {        return true;    }    // Try placing more figures around the current position (i & j),    // in increasing distances (kx & ky)    for (int ky=1; figureCount < il && ky < dim; ky++) {      for (int kx=1; figureCount < il && kx < dim; kx++) {        // System.out.println(cb.checkPosition(x, y));        for (int i = -1; i <= 1; i++) {          for (int j = -1; j <= 1; j++) {            if (i==0 && j==0) {              continue;            }            if (cb.checkPosition(x + i*kx, y + j*ky) == 0 &&              rekur(x + i*kx, y + j*ky, cb, fig, il)) {              return true;            }          }        }      }    }    cb.unputFigure(x, y, fig);    return false;}Here are the changes and why:Calculate once, if possible At the start I find the dimension and number of figures once, and use the precalculated throughout the function. Simplify disguised for-loop  Your while-loop is simply a for-loop with a slightly more complicated end condition.Avoid multiple similar if-statements Whenever you find yourself copy-pasting if-loops, which are almost identical, you should start thinking if you can't replace it with some more general code. In this case you vary the coordinates by adding -kx, 0 and kx (and likewise for ky). This can be implemented using a double for-loop (looping through -1, 0, 1) and multiplication, as shown above. This kind of simplification avoids repeated code, and shows the pattern you've used clearer. To avoid the current position a special case when both are zero had to be added. Still I think, this is a neater solution to avoid the multiple similar if statements.Added comments related to why you are doing stuff I've added a few comments, but some more could be added around in your code to describe what you are trying to achieve in the various parts of your code.Some other general commentsThe defineAttack() ignores other pieces With the current definition of your Queen and its isAttacked() other pieces on the chess board is ignored. If a Queen is placed all surrounded by Pawns for example, the Queen is not attacking anything but the Pawns.In this particular implementation the other pieces are also Queens with the same attack pattern, so it doesn't matter that much. The moment you add another piece, though, your logic is broken.Strange order of i and j In most double loops the i is the outer loop, not the inner. It doesn't have to be so, but you're kind of breaking conventions. Similarly, you've chosen to do tab[j][i] switching the dimension. This might confuse future readers of your code.Why a and not dimension? Why have you chosen you use a for the dimension of your chess board? And a method getA()? That really doesn't make any sense, and would be better replaced by dimension or size or length. Why not have the board arrays of Figures? Instead of using the array of ints it makes more sense to me to have a board of Figures, with possibly some special figures to indicate the empty or attacked spaces. This way you would have avoided passing the fig all around, and could just check which figure is in a given spot. On a related note, that would also simplify the signature of updateFields(), as it isn't natural for this to receive the figure which have just been removed (or added). Why x in the first rekur() method? This a truly badly named variable. The x has a very strong conation of being a coordinate, but you're using it to test for solutions. Name it appropriatly like foundSolution. Don't test for x == false  Use !x instead, or with new name !foundSolution. Use the booleans directly in conditions and don't test for true or false.Disguised for-loop in the first rekur(), as well In your first version, where you use the x == false condition, you do a double while loop, which could be changed into a double for loop like the code above. In addition I really don't get the j < Math.min(il, cb.getA()) condition. Is that really correct and functional? Have you tried it after changing the dimensions or number of queens to place?checkPosition() is misleading It doesn't check the position, it returns the position content. And it reveals inner magic numbers of your chessboard. I think it would be better to have it named isEmpty() and return a true/false value depending on content. I would possibly also add an isAttacked() to the chessboard returning true if is already under attack.Change Figure's isAttacked to attacks() In my head that would be a slightly better name, and would reduce confusion if you implement my previous suggestion.I'm still not sure if I covered all bases, but I should covered enough for you to get something to think about!