Creating/ Solving the 8 Queens Problem by Creating Own GUI Chessboard and Messag
ID: 3829456 • Letter: C
Question
Creating/ Solving the 8 Queens Problem by Creating Own GUI Chessboard and Message Display
Hello! I need help with my second big project this semester in my course. The is the second introductry course to Java, so I do not know a lot of advanced programming. To create the GUI, use the Java Swing documentation. The user should be placing the Queens. Also, there should be a tip but (does not have to be using Artificial Intelligence but recommended) that shows the next viable partial solution. I HAVE POSTED PART OF CODE BELOW. Need to check if Queen is not attacking other Queen. I have a Queen class but in the listener for the submit button. It needs to check to see if the partial solution is correct. I uploaded a Queen image for piece so the code wont work unless you put in your own icon in click listener.
public class Queen {
private int row;
private int column;
public Queen(int r, int c){
row = r;
column = c;
}
public boolean attacks(Queen other){
return row == other.row
|| column == other.column
|| Math.abs(row - other.row) == Math.abs(column - other.column);
}
public String toString(){
return "" + "abcdefgh".charAt(column) + (row + 1);
}
}
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) throws Exception {
EightQueenPanel queenFrame = new EightQueenPanel();
JFrame frame = new JFrame();
frame.setSize(1000,1000);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(queenFrame);
frame.setVisible(true);
}
}
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
class EightQueenPanel extends JPanel {
private MyCanvas canvas;
private EightQueen queen;
private ArrayList list = null;
private int count = 0;
private JPanel board;
private JButton submitButton = new JButton("Submit");
private JButton tipButton = new JButton("Tip?");
private JButton[] buttons = new JButton[65];
private JButton btnForClick;
private int coordinate = 0;
private int row = 1;
private int column = 1;
public EightQueenPanel() {
board = createBoard();
ActionListener submitListener = new SubmitListener();
ActionListener tipListener = new TipListener();
add(board);
add(submitButton);
add(tipButton);
}
public class SubmitListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent event) {
// A submit listener checks to see if the current queen layout is correct
//Queen queen = new Queen();
if(btnForClick.getIcon()==null){
btnForClick.toString();
}else{
btnForClick.setIcon(null);
btnForClick.toString();
}
}
}
public class TipListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
}
}
public JPanel createBoard() {
class ClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
JButton btnForClick =(JButton)event.getSource();
if(btnForClick.getIcon()==null){
try {
String iconPath = "BQ.gif";
final BufferedImage queen = ImageIO.read(new File(iconPath));
btnForClick.setIcon(new ImageIcon(queen));
} catch (Exception ex) {
System.out.println("Something wrong with icon import.");
}
}else{
btnForClick.setIcon(null);
}
}
}
ClickListener listener = new ClickListener();
JPanel panel = new JPanel(new GridLayout(8, 8));
boolean evenColumn = true;
for (int i = 1; i <= 64; i++) {
JButton btn = new JButton(String.valueOf(i));
btn.setPreferredSize(new Dimension(64, 64));
if (evenColumn == true) {
if (i % 8 == 0) {
evenColumn = false;
}
if (i % 2 == 0) {
btn.setBackground(Color.WHITE);
panel.add(btn);
} else {
btn.setBackground(Color.BLACK);
panel.add(btn);
}
btn.addActionListener(listener);
buttons[i] = btn;
} else {
if (i % 8 == 0) {
evenColumn = true;
}
if (i % 2 == 0) {
btn.setBackground(Color.BLACK);
panel.add(btn);
} else {
btn.setBackground(Color.WHITE);
panel.add(btn);
}
btn.addActionListener(listener);
buttons[i] = btn;
}
}
return panel;
}
}
Explanation / Answer
The graphics because you want to have a bitmap for all the pieces I wouldn't even get too complicated with it, into and using that instead of anything else. Another way would be to create static windows for each board spot with a background color of black or white, then draw on it if you have a piece there but that might be too much for something this simple.Though now that I actually read it, he says he hasn't done any GUI.. Spend a night on that, and then you should be good to using the rest of the API to design your GUI
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.