CS 100: Lecture L27

May 4

| Back to Lecture Index |


 


import java.io.*;
import java.awt.*;

public class CUCSDrawing extends Frame
{
    public void paint(Graphics g)
	{
	   chess C = eightQ();
	   showThreats(g,C);
	}
	
	// Yields true if no Queen is threatened
	public static boolean queensAreSafe(chess C)
	{
	   for (int i=0;i< 8;i++)
	      for (int j=0;j< 8;j++)
	          if(C.getPiece(i,j).equals("Q") && C.getThreat(i,j))
	             return false;
	   return true;
	}
	
	// Displays the chessboard showing threatened tiles.
    public void showThreats(Graphics g, chess C)
    {
       int L = 50; 
       int T = 50;
       int s = 60;
       String piece;
       g.setFont(new Font("TimesRoman",Font.PLAIN, 18));
       for(int r=0;r<=7;r++)
          for(int c=0;c<=7;c++)
          {
             if(C.getThreat(r,c))
                g.setColor(Color.magenta);
             else
                g.setColor(Color.white);
             g.fillRect(L+c*s,T+r*s,s,s);
             g.setColor(Color.black);
             g.drawRect(L+c*s,T+r*s,s,s);
             g.drawString(C.getPiece(r,c),25+L+c*s,35+T+r*s);     
          }
    }
    
    
	// Yields a chess object that represents a solution to the 8 queens problem.
	// The idea is to place 8 queens on the board so that none are threatened.
    public static chess eightQ()
	{
	
	   String[][] B = {{"","","","","","","",""},
	                   {"","","","","","","",""},
	                   {"","","","","","","",""},
	                   {"","","","","","","",""},
	                   {"","","","","","","",""},
	                   {"","","","","","","",""},
	                   {"","","","","","","",""},
	                   {"","","","","","","",""}};
	              
	   chess C = new chess(B);
	   int[] j = new int[8];
	   int count = 0;
	   for (j[0]=0;j[0]< 8;j[0]++)
	      for (j[1]=0;j[1]< 8;j[1]++)
	         if(j[1]!=j[0])
	            for (j[2]=0;j[2]< 8;j[2]++)
	               if(j[2]!=j[1] && j[2]!=j[0])
	                  for (j[3]=0;j[3]< 8;j[3]++)
	                     if(j[3]!=j[2] && j[3]!=j[1] && j[3]!=j[0])
	                        for (j[4]=0;j[4]< 8;j[4]++)
	                           if(j[4]!=j[3] && j[4]!=j[2] && j[4]!=j[1] && j[4]!=j[0]) 
	                              for (j[5]=0;j[5]< 8;j[5]++)
	                                 if(j[5]!=j[4] && j[5]!=j[3] && j[5]!=j[2] && j[5]!=j[1] && j[5]!=j[0])
	                                    for (j[6]=0;j[6]< 8;j[6]++)
	                                       if(j[6]!=j[5] && j[6]!=j[4] && j[6]!=j[3] && j[6]!=j[2] && j[6]!=j[1] && j[6]!=j[0])
	                                          for (j[7]=0;j[7]< 8;j[7]++)
	                                             if(j[7]!=j[6] && j[7]!=j[5] && j[7]!=j[4] && j[7]!=j[3] && j[7]!=j[2] && j[7]!=j[1] && j[7]!=j[0]) 
	                                             {
	                                                // Set up the board.
	                                                for(int i=0;i< 8;i++)
	                                                {
	                                                   for(int k=0;k< 8;k++)
	                                                      B[i][k] = "";
	                                                   B[i][j[i]]="Q";
	                                                }
	                                                C = new chess(B);
	                                                count++;
	                                                if(queensAreSafe(C))
	                                                {
	                                                   System.out.println(count);
	                                                   return C;
	                                                }
	                                              }
	                                              
	   return C;
	}
	
	
	
}

public class Queen
{
	public static void main(String args[])
	{
		CUCSDrawing d = new CUCSDrawing();
		d.resize(800,650);                     
		d.move(0,75);
		d.setTitle("Eight Queens");                 
		d.show();
		d.toFront();
	}
}