You are on page 1of 15

c 



import java.applet.Applet;
import java.awt.*;

import java.awt.event.*;
import javax.swing.*;
import java.awt.event.*;
public class QueensApplet extends Applet implements ActionListener {
JFrame frame;
Button solve = new Button("Solve");
Button clear = new Button("Clear");
Button finish = new Button("Finish");
Board b = new Board();
int c;

public void init() {


setLayout(new BorderLayout());
add("Center", b);
Panel bPanel = new Panel();
add("South", bPanel);
bPanel.setLayout(new GridLayout(1,5));
bPanel.add(new Label(" "));
bPanel.add(clear);

bPanel.add(new Label(" "));


bPanel.add(solve);
bPanel.add(new Label(" "));
bPanel.add(finish);
clear.addActionListener(this);
solve.addActionListener(this);

finish.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == clear)

{
b.initBoard();
b.paint(b.getGraphics());
}
else if (e.getSource() == solve)
{
b.initBoard();
b.paint(b.getGraphics());
b.solve(0);
}

else if (e.getSource() == finish)


{
c=0;
c=b.fin();
if(c==8)
{
JOptionPane.showMessageDialog(frame,"Queen Count = "+c);
JOptionPane.showMessageDialog(frame,"WIN!!!");

System.out.println("Winner; Queen Count = "+c);

}
else

{
JOptionPane.showMessageDialog(frame,"Queen Count = "+c);
JOptionPane.showMessageDialog(frame,"L@@SER");
System.out.println("Looser; Queen Count = "+c);
} } }}

/*<applet code=QueensApplet.class width=500 height=500>


</applet>*/

° 
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
import java.applet.*;
public class Board extends Canvas implements MouseListener {
public int count=0;
int[][] grid = new int[8][8];

Graphics gr = this.getGraphics();
int gWidth,gHeight;
public Board() {
addMouseListener(this);
initBoard();
}
public void initBoard() {
count=0;

for (int i=0;i<8;i++)


for (int j=0;j<8;j++)
grid[i][j] = 0;
} /**
* Paint the board
*/ public void paint(Graphics g) {

gr = g;
gWidth = getBounds().width/8;
gHeight = getBounds().height/8;
for (int i=0;i<8;i++)
for (int j=0;j<8;j++)

drawCell(i,j);
} /**
* Draw a specific cell on the board.
*/
private void drawCell(int x, int y) {
int dark = 0;
int q = 1;
gr.setColor(Color.black);
gr.drawRect(x*gWidth, y*gHeight, gWidth, gHeight);
if ((x+y)%2 == 0)

dark = 20;
switch (grid[x][y]) {
case 0: gr.setColor(new Color(230+dark,230+dark,230+dark));
break;
case 1: gr.setColor(new Color(160+dark,110+dark,110+dark));
break;
case 2: gr.setColor(new Color(0,0,0)); q = 0; break;
}
gr.fillRect(x*gWidth+1, y*gHeight+1, gWidth-1, gHeight-1);

if (q==0)

{
gr.setColor(new Color(230,230,230));
gr.drawString("Q",x*gWidth+25, y*gHeight+35);

} }
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
/**
* Handle a mouse click. */

public void mousePressed(MouseEvent e) {


int x,y;
x = e.getX()/gWidth;
y = e.getY()/gHeight;
if (legal(x,y)) {
if (grid[x][y] == 0)
{
addQueen(x,y,true);
count++;
}

else if (grid[x][y] == 2)
{
addQueen(x,y,false);
count--;
}
}
System.out.println("COUNT : "+count );
} /**

* Adds (or removes) a queen to the given coordinates.


* If add is true, it adds a queen. If it is false, it removes a queen.
*/
private void addQueen(int x, int y, boolean add) {
int i;
gr = this.getGraphics();

if (add)
grid[x][y] = 2;
else
grid[x][y] = 0;
for (i=0;i<8;i++) {

if (i!=y) changeCell(x,i,add);
if (i!=x) changeCell(i,y,add);
}
for (i=1; legal(x+i,y+i); i++)
changeCell(x+i,y+i,add);
for (i=1; legal(x+i,y-i); i++)
changeCell(x+i,y-i,add);
for (i=1; legal(x-i,y+i); i++)
changeCell(x-i,y+i,add);
for (i=1; legal(x-i,y-i); i++)

changeCell(x-i,y-i,add);
drawCell(x,y);
if (!add)
for (i=0;i<8;i++)
for (int j=0;j<8;j++)
if (grid[i][j] == 2)
addQueen(i,j,true);
} /**

* Add or remove a queen to the given cell


*/
private void changeCell(int x, int y, boolean add) {
if (add && grid[x][y] == 0)
{
grid[x][y] = 1;

}
else if (!add && grid[x][y] == 1)
{
grid[x][y] = 0;
}

drawCell(x,y);
} /**
* Returns true if the coordinates are a legal board position
*/
private boolean legal(int x, int y) {
return (x >= 0 && x < 8 && y >= 0 && y < 8);
} /**
* Solve the 8-queens puzzle with a very simple depth-first search.
*/
public boolean solve(int y) {

int i,j;
boolean r = false;
for (i=0;i<8;i++) {
// for each row, try placing a queen
if (grid[i][y] == 0) {
addQueen(i,y,true);
if (y == 7) {
return true; // we have placed all queens
successfully
} else {

if (solve(y+1)) {
return true; // we solved it, return

} else {
addQueen(i,y,false); // remove the queen
} } } }
// unable to solve down this branch -- retreat!
return false;
}
public int fin()
{

if(count==8){
return(count);
}
else
return(count);
}
}

Y
‘  Y

Y
Y

Y
Y
Y
Y
Y

Y
Y

Y
Y

Y
Y

Y
Y
Y