Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server T-SQL Recipes

Authors: David Dye, Jason Brimhall

4th Edition

1484200616, 9781484200612

Students also viewed these Databases questions