⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 queentest.java

📁 八皇后问题的图形界面实现
💻 JAVA
字号:
package test.myqueen;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class QueenTest extends JFrame {
	boolean used[][] = new boolean[8][8];
	// 临时存储used数组的值,对used数组起保护作用
	boolean temp[][] = null;
	// pos[i] 表示第i行皇后放置在第j列
	int pos[] = new int[8];
	// temp2用于保护pos
	int temp2 [] = new int[8];
	int count = 0;
	// 用来保护count
	int queenNum = 0;
	
	int hasAnswer = 0;

	public QueenTest() {
		super("八皇后问题");
		Container container = this.getContentPane();
		container.setLayout(new BorderLayout());
		final MyPanel panel = new MyPanel();
		container.add(panel, BorderLayout.CENTER);
		JButton button = new JButton("重新开始");
		for (int i = 0; i < pos.length; i++) {
			pos[i] = -1;
		}
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				for (int i = 0; i < 8; i++) {
					for (int j = 0; j < 8; j++) {
						used[i][j] = false;
					}
				}
				for (int i = 0; i < 8; i++) {
					pos[i] = -1;
				}
				count = 0;
				panel.repaint();
			}

		});
		container.add(button, BorderLayout.SOUTH);
		panel.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				int col = e.getX() / 40;
				int row = e.getY() / 40;
				if (used[row][col]) {
					JOptionPane.showMessageDialog(getParent(), "发生冲突,请重新放置");
				}
				if (!isConflict(row, col)) {
					used[row][col] = true;
					pos[row] = col;
					System.out.println("row:"+row+"col:"+pos[row]);
					if (!hasAnswer()) {
						JOptionPane.showMessageDialog(getParent(),
								"您当前的放置无解,请重新开始", "失败了",
								JOptionPane.INFORMATION_MESSAGE);
					}
					count++;
					repaint();
					if (count == 8) {
						JOptionPane.showMessageDialog(getParent(), "游戏完成");
					}
				} else {
					JOptionPane.showMessageDialog(getParent(), "发生冲突,请重新放置");
				}
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

		});
		this.setSize(330, 380);
		this.setVisible(true);
		this.setResizable(false);
		this.addWindowListener(new WindowListener() {

			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}

			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}

			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub

			}

		});
	}

	public boolean hasAnswer() {
		// TODO Auto-generated method stub
		temp = new boolean[8][8];
		temp2 = new int [8];
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				temp[i][j] = used[i][j];
			}
		}
		for(int i=0;i<8;i++){
			temp2[i] = pos[i];
		}
		queenNum = 0;
		hasAnswer = 0;
		put(0);
		if(hasAnswer == 1){
			return true;
		}
		else {
			return false;
		}
	}

	// 放置第i行皇后
	public void put(int row) {
		// TODO Auto-generated method stub
		System.out.println("i:"+row);
		if(row == 8 ) {
			hasAnswer = 1;
			return ;
		}
		if (temp2[row] != -1) {
			queenNum ++;
			if(queenNum == 8){
				hasAnswer = 1; 
				return;
			}
			put(row + 1);
		} else {
			for (int col = 0; col < 8;col++) {
				if (!temp[row][col] && !isConflict2(row, col)) {
					temp[row][col] = true;
					temp2[row] = col;
					queenNum ++;
					if (queenNum == 8)
					{
						hasAnswer = 1;
						return;
					}
					else {
						put(row + 1);
					}
					temp[row][col] = false;
				}
			}
		}
	}

	public boolean isConflict(int row, int col) {
		// TODO Auto-generated method stub
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				if (used[i][j] && (i == row || j == col)) {
					return true;
				}
				if (used[i][j] && (i + j == row + col)) {
					return true;
				}
				if (used[i][j] && (i - j == row - col)) {
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean isConflict2(int row ,int col){
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				if (temp[i][j] && (i == row || j == col)) {
					return true;
				}
				if (temp[i][j] && (i + j == row + col)) {
					return true;
				}
				if (temp[i][j] && (i - j == row - col)) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		QueenTest test = new QueenTest();
	}

	public class MyPanel extends JPanel /* implements MouseListener */{
		@Override
		public void paint(Graphics g) {
			// TODO Auto-generated method stub
			super.paint(g);
			for (int i = 0; i <= 8; i++) {
				g.drawLine(0, i * 40, 320, i * 40);
				g.drawLine(i * 40, 0, i * 40, 320);
			}
			for (int i = 0; i < 8; i++) {
				for (int j = 0; j < 8; j++) {
					if (used[i][j] == true) {
						g.setColor(Color.BLACK);
						g.fillOval(j * 40, i * 40, 40, 40);
					}
				}
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -