Question: 8queens problem with recursion package queens; import java.awt.Graphics; import java.awt.Image; import java.io.File; import javax.swing.ImageIcon; import javax.swing.JPanel; public class ControlPanel extends JPanel { private static final

8queens problem with recursion

package queens;

import java.awt.Graphics; import java.awt.Image; import java.io.File; import javax.swing.ImageIcon; import javax.swing.JPanel;

public class ControlPanel extends JPanel { private static final long serialVersionUID = 5L; private static final int SIZE = 8; private String imageLocation; private ImageIcon queenIcon; private Image queenImage; private int[] rowPositions; private int startingColumn; public ControlPanel() { this(0); } public ControlPanel(int start) { startingColumn = start; imageLocation = "images" + File.separator + "queen.jpg"; queenIcon = new ImageIcon(imageLocation); queenImage = queenIcon.getImage(); rowPositions = new int[SIZE]; } public void draw(Graphics _graphics) { if (search(0, startingColumn)) { drawQueens(_graphics); drawChessBoard(_graphics); } } public void drawChessBoard(Graphics _graphics) { for (int i = 0; i < SIZE; i++) { _graphics.drawLine(0, i * getHeight() / SIZE, getWidth(), i * getHeight() / SIZE); _graphics.drawLine(i * getWidth() / SIZE, 0, i * getWidth() / SIZE, getHeight()); } } public void drawQueens(Graphics _graphics) { int j; // position of queen in row i int imageX = 0; int imageY = 0; int imageWidth = 0; int imageHeight = 0; if(queenImage != null) { for (int i = 0; i < SIZE; i++) { j = rowPositions[i]; imageX = j * getWidth() / SIZE; imageY = i * getHeight() / SIZE; imageWidth = getWidth() / SIZE; imageHeight = getHeight() / SIZE; _graphics.drawImage(queenImage, imageX, imageY, imageWidth, imageHeight, this); } } } /************************************************************ * checks if a queen can be placed in row and column * **********************************************************/ public boolean isSafe(int row, int col) { return true; } public void paintComponent(Graphics _graphics) { super.paintComponent(_graphics); draw(_graphics); }

/************************************************************ * search for a solution starting from a specified column in * the first row. * **********************************************************/ public boolean search(int row, int start) { return false; }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!