📄 nqueen.java
字号:
/* * nqueen.java * * Created on January 21, 2008, 12:50 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. *//** * * @author Gopal Lal and Sandeepan Jindal */ import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class nqueen extends JApplet implements ActionListener{ String po = JOptionPane.showInputDialog(this,"Dimension of The Board"); int poo = Integer.parseInt(po); int BOARD_SIZE = poo; private final static int WINDOW_WIDTH = 800; private final static int WINDOW_HEIGHT = 700; final static int IDEL = 0; final static int CLEAR = 1; final static int STEEPEST_WITHOUT_RANDOM = 2; final static int STEEPEST_WITH_RANDOM = 3; final static int FIRST__WITHOUT_RANDOM = 4; final static int FIRST__WITH_RANDOM = 5; JButton clear = new JButton("Clear"); JButton Steepest_Random = new JButton("Steepest & Random"); JButton Steepest_Normal = new JButton("Steepest Normal"); JButton FirstChoice_Random = new JButton("First Choice & Random"); JButton FirstChoice_Normal = new JButton("First Choice Normal"); JTextArea stepCounterTextArea = new JTextArea("0", 1, 10); JTextArea DeadEndTextArea = new JTextArea("0", 1, 10); MyBoard b = new MyBoard(); //------------------------------------------------ // Init the entrire JFrame. //------------------------------------------------ public void init() { getContentPane().setLayout(new BorderLayout()); getContentPane().add("Center", b); Panel textPanel = new Panel(); textPanel.setLayout(new BorderLayout(4, 1)); textPanel.add("North", new JLabel(" ")); textPanel.add("West", new JLabel(" ")); textPanel.add("East", new JLabel(" ")); textPanel.add("South", new JLabel(" ")); Panel textCenterPanel = new Panel(); textCenterPanel.setLayout(new GridLayout(5, 1)); textCenterPanel.add(new JLabel("Step Counter :")); textCenterPanel.add(stepCounterTextArea); textCenterPanel.add(new JLabel(" ")); textCenterPanel.add(new JLabel("Dead End or complte solution :")); textCenterPanel.add(DeadEndTextArea); textPanel.add("Center", textCenterPanel); //textPanel.add("Center", textCenterPanel); getContentPane().add("East", textPanel); // Add the button Panel onto the south of the frame. Panel buttonPanel = new Panel(); buttonPanel.setLayout(new GridLayout(2, 4)); buttonPanel.add(new JLabel(" ")); buttonPanel.add(new JLabel(" ")); buttonPanel.add(new JLabel(" ")); buttonPanel.add(new JLabel(" ")); buttonPanel.add(new JLabel(" ")); buttonPanel.add(clear); buttonPanel.add(Steepest_Random); buttonPanel.add(Steepest_Normal); buttonPanel.add(FirstChoice_Random); buttonPanel.add(FirstChoice_Normal); getContentPane().add("South", buttonPanel); clear.addActionListener(this); Steepest_Random.addActionListener(this); Steepest_Normal.addActionListener(this); FirstChoice_Random.addActionListener(this); FirstChoice_Normal.addActionListener(this); } //------------------------------------------------------------- // Listen to the actions of the buttons and the slider. //------------------------------------------------------------- public void actionPerformed(ActionEvent e) { if (e.getSource() == clear) { b.initBoard(); b.paint(b.getGraphics()); stepCounterTextArea.setText("0"); DeadEndTextArea.setText(""); //soluCounterTextArea.setText("0"); } else if (e.getSource() == Steepest_Random) { b.initBoard(); b.paint(b.getGraphics()); b.firstRandom(2); //b.steepestRandom(); } else if (e.getSource() == Steepest_Normal) { b.initBoard(); b.paint(b.getGraphics()); b.firstRandom(0); //b.steepestWithoutRandom(); } else if (e.getSource() == FirstChoice_Random) { b.initBoard(); b.paint(b.getGraphics()); b.firstRandom(3); } else if (e.getSource() == FirstChoice_Normal) { b.initBoard(); b.paint(b.getGraphics()); b.firstRandom(1); //b.firstWithoutRandom(); } } public static void main(String[] args) { JFrame applicationFrame = new JFrame("NQueens Problem - cs433"); // kill application when window closes applicationFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); nqueen appletObject = new nqueen(); appletObject.init(); applicationFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); applicationFrame.getContentPane().add(appletObject); applicationFrame.pack(); applicationFrame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); applicationFrame.setVisible(true); } //---------------------------------------------------------------- // Define the board and the operations on it. //---------------------------------------------------------------- private class MyBoard extends Canvas { final static int NO_QUEEN_AND_SAFE = 0; final static int QUEEN = 2; int status = IDEL; int stepCounter = 0; int soluCounter = 0; int delay = 0; int[][] grid = new int[BOARD_SIZE][BOARD_SIZE]; MyBoard next; Graphics gr = this.getGraphics(); int gWidth; int gHeight; // Constructor of the class of MyBoard //--------------------------------------------------------- public MyBoard() { initBoard(); } // Reset the status of the board. //-------------------------------------------------------- public void setStatus(int newStatus) { status = newStatus; } // Initializes the configuration of this board. //----------------------------------------------------------- public void initBoard() { for (int i = 0; i < BOARD_SIZE; i++) for (int j = 0; j < BOARD_SIZE; j++) grid[i][j] = 0; next = null; stepCounter = 0; soluCounter = 0; } public void paint(Graphics g) { gr = g; gWidth = getBounds().width / BOARD_SIZE; gHeight = getBounds().height / BOARD_SIZE; for (int i = 0; i < BOARD_SIZE; i++) for (int j = 0; j < BOARD_SIZE; j++) drawCell(i, j); } // Draw a specific cell on the board. //--------------------------------------------------------- private void drawCell(int x, int y) { int dark = 0; gr.setColor(Color.black); gr.drawRect(x * gWidth, y * gHeight, gWidth, gHeight); if (((x + y) % 2) == 0) { dark = 20; } switch (grid[x][y]) { case NO_QUEEN_AND_SAFE: gr.setColor(new Color(160 + dark, 110 + dark, 110 + dark)); break; case QUEEN: gr.setColor(new Color(0, 0, 0)); break; } gr.fillRect((x * gWidth) + 1, (y * gHeight) + 1, gWidth - 1, gHeight - 1); } public void firstRandom(int flag) { MyBoard frand = new MyBoard(); MyBoard fRand = frand; frand.initBoard(); stepCounter = 0; NQueensBoard DV= new NQueensBoard(BOARD_SIZE); DV.randomPlaceQueens(); stepCounter = DV.reduceConflict(flag); boolean status = DV.isConflict(); stepCounterTextArea.setText(Integer.toString(stepCounter)); if(status == true) {DeadEndTextArea.setText("TRY AGAIN,Press CLEAR");} else{DeadEndTextArea.setText("Hurray!!! You got a solution");} for (int i = 0; i < BOARD_SIZE; i++) { grid[i][DV.index[i]] =2; paint(this.getGraphics()); } }} }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -