Tuesday, 17 September 2013

8 Queen Problem

HTML Online Editor Sample

 

/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
import java.applet.Applet;
import java.awt.event.*;
        import java.awt.*;
 
/**
 *
 * @author gaurav
 */
public class eightqueen extends Applet implements Runnable,ActionListener {
 
    /**
     * Initialization method that will be called after the applet is loaded into
     * the browser.
     */
    int a=0,b=0,c=0,d=0;
    Button [][]chess;
    int [][]check;
    Thread t;
    Button start;
    public void init() {
        setLayout(null);
        t=new Thread(this);
        start=new Button("START");
        add(start);
        start.reshape(250,525,100,30);
        start.setBackground(Color.red);
        start.addActionListener(this);
        check=new int[8][8];
        for(int i=0;i<8;i++)
        {for(int j=0;j<8;j++){
            check[i][j]=0;
        
        
        }}
        chess=new Button[8][8];
        for(int i=0;i<8;i++){
            for(int j=0;j<8;j++){chess[i][j]=new Button();
            add(chess[i][j]);
            if((i+j)%2==0){
            chess[i][j].setBackground(Color.black);}
            else
            {chess[i][j].setBackground(Color.white);}
            chess[i][j].reshape(100+(50*j),100+(50*i),50,50);
            chess[i][j].addActionListener(this);
                
        }}
        queen();
        // TODO start asynchronous download of heavy resources
    }
    // TODO overwrite start(), stop() and destroy() methods
    public void run()
    {try
    {
        do{chess[6][4].setLabel("Q");
    chess[6][4].setBackground(Color.green);
    chess[1][3].setLabel("Q");
    chess[1][3].setBackground(Color.green);
    chess[3][0].setLabel("Q");
    chess[3][0].setBackground(Color.green);
    chess[5][1].setLabel("Q");
    chess[5][1].setBackground(Color.green);
    chess[7][2].setLabel("Q");
    chess[7][2].setBackground(Color.green);
    chess[4][7].setLabel("Q");
    chess[4][7].setBackground(Color.green);
    chess[2][6].setLabel("Q");
    chess[2][6].setBackground(Color.green);
    chess[0][5].setLabel("Q");
    chess[0][5].setBackground(Color.green);
    Thread.sleep(5000);
    clear();
    chess[4][6].setLabel("Q");
    chess[4][6].setBackground(Color.green);
    chess[3][1].setLabel("Q");
    chess[3][1].setBackground(Color.green);
    chess[0][3].setLabel("Q");
    chess[0][3].setBackground(Color.green);
    chess[1][5].setLabel("Q");
    chess[1][5].setBackground(Color.green);
    chess[2][7].setLabel("Q");
    chess[2][7].setBackground(Color.green);
    chess[7][4].setLabel("Q");
    chess[7][4].setBackground(Color.green);
    chess[6][2].setLabel("Q");
    chess[6][2].setBackground(Color.green);
    chess[5][0].setLabel("Q");
    chess[5][0].setBackground(Color.green);
    Thread.sleep(5000);
    clear();
    chess[1][4].setLabel("Q");
    chess[1][4].setBackground(Color.green);
    chess[4][0].setLabel("Q");
    chess[4][0].setBackground(Color.green);
    chess[2][1].setLabel("Q");
    
    chess[2][1].setBackground(Color.green);
    chess[0][2].setLabel("Q");
    chess[0][2].setBackground(Color.green);
    chess[6][3].setLabel("Q");
    chess[6][3].setBackground(Color.green);
    chess[3][7].setLabel("Q");
    chess[3][7].setBackground(Color.green);
    chess[5][6].setLabel("Q");
    chess[5][6].setBackground(Color.green);
    chess[7][5].setLabel("Q");
    chess[7][5].setBackground(Color.green);
     Thread.sleep(5000);
    clear();
    
    
    chess[4][1].setLabel("Q");
    chess[4][1].setBackground(Color.green);
    chess[0][4].setLabel("Q");
    chess[0][4].setBackground(Color.green);
    chess[1][2].setLabel("Q");
    
    chess[1][2].setBackground(Color.green);
    chess[2][0].setLabel("Q");
    chess[2][0].setBackground(Color.green);
    chess[3][6].setLabel("Q");
    chess[3][6].setBackground(Color.green);
    chess[7][3].setLabel("Q");
    chess[7][3].setBackground(Color.green);
    chess[6][5].setLabel("Q");
    chess[6][5].setBackground(Color.green);
    chess[5][7].setLabel("Q");
    chess[5][7].setBackground(Color.green);
     Thread.sleep(5000);
    clear();
   /* chess[4][6].setLabel("Q");
    chess[4][6].setBackground(Color.green);
    chess[4][6].setLabel("Q");
    chess[4][6].setBackground(Color.green);
    chess[4][6].setLabel("Q");
    chess[4][6].setBackground(Color.green);
    chess[4][6].setLabel("Q");
    chess[4][6].setBackground(Color.green);
    */
    }while(true);}
    
    
    
    
    
    
    catch(InterruptedException e){System.out.print("gaurav");
    }
    
    
    
    
    
    }
    public void actionPerformed(ActionEvent ae){
        if(ae.getSource()==start)
            t.start();
       
    
    }
    public void queen(){}
    public void clear()
    {for(int i=0;i<8;i++){for(int j=0;j<8;j++){if((i+j)%2==1){chess[i][j].setBackground(Color.white);chess[i][j].setLabel(" ");}
    else
    {chess[i][j].setBackground(Color.black);chess[i][j].setLabel(" ");}
            }}}
    
}

No comments:

Post a Comment