//============================================================================ // http://www.apl.jhu.edu/~hall/java/NQueens.java // // Finds a solution to the NQueens problem for any N 4 or greater. // Requires no search, and solutions are found in constant time per // queen. So printing the board is the rate-limiting step. // Taken from algorithm in ACM SIGART Bulletin, Vol 2, Number 2, // page 7. // // 3/96 Marty Hall. marty_hall@jhuapl.edu //============================================================================ import java.applet.Applet; import java.awt.*; public class NQueens extends Applet { private int Top=45, Left=20, Board_Width=500, Width, Height, N=8; private Label N_Label; private Scrollbar N_Slider; private TextField N_Value; private Button Draw; //------------------------------------------------------------------- private boolean Is_Even(int X) { return(X == 2*(X/2)); } //------------------------------------------------------------------- // First square is red. After that, they alternate red/black. private Color Pos_Color(int Row, int Col) { if ( (Is_Even(Row) && Is_Even(Col)) || (!Is_Even(Row) && !Is_Even(Col))) return(Color.black); else return(Color.red); } //------------------------------------------------------------------- // I probably need to understand how to use GridBagLayout. But // I wasn't too good at controlling the widget layout using the // existing layout managers, so I used absolute positioning. private void Add_Widgets() { int L = Board_Width + 2*Left + 5, W = Width - L, C = L+W/2, H = Height, T = 10, N_Label_Top=T, N_Label_Height=20, Draw_Button_Top = H-100, Draw_Button_Height=80, N_Value_Height=30, N_Value_Top=Draw_Button_Top-N_Value_Height-20, N_Slider_Top=N_Label_Top+N_Label_Height, N_Slider_Height=N_Value_Top-N_Slider_Top; setLayout(null); // Turn off layout managers N_Label = new Label("N (4-60)", Label.CENTER); N_Label.reshape(L, N_Label_Top, W, N_Label_Height); add(N_Label); N_Slider = new Scrollbar(Scrollbar.VERTICAL, 8, 10, 4, 60); N_Slider.reshape(L+W/2-10, N_Slider_Top, 20, N_Slider_Height); add(N_Slider); N_Value = new TextField("8", 2); N_Value.setEditable(false); N_Value.reshape(C-20, N_Value_Top, 40, N_Value_Height); add(N_Value); Draw = new Button("Draw"); Draw.setFont(new Font("Helvetica", Font.BOLD, 20)); Draw.reshape(L+10, Draw_Button_Top, W-20, Draw_Button_Height); add(Draw); } //------------------------------------------------------------------- private void Draw_Title(Graphics G) { int Big_Size = Top-5; G.setColor(Color.yellow); G.setFont(new Font("TimesRoman", Font.BOLD, Big_Size)); G.drawString("N Queens", Left, Top-10); G.setColor(Color.black); G.setFont(new Font("TimesRoman", Font.ITALIC, 12)); G.drawString("http://www.apl.jhu.edu/~hall/java/NQueens.html", Left+Board_Width/2, Big_Size/2+2); G.drawString("marty_hall@jhuapl.edu", Left+Board_Width/2, Big_Size-5); } //------------------------------------------------------------------- private void Draw_Separator(Graphics G) { G.setColor(Color.black); G.drawLine(Board_Width+2*Left, 0, Board_Width+2*Left, Height); } //------------------------------------------------------------------- private void Draw_Queens(int[] Positions, Graphics G) { int Square_Width = Board_Width/N, // Rounds down Char_Width = (Board_Width*4)/(N*5), Offset = (Square_Width-Char_Width), // Rounds down Col; String Q = "Q"; G.setFont(new Font("Helvetica", Font.BOLD, Char_Width)); for (int Row=0; Row= M) { N -= M; } return(N); } //------------------------------------------------------------------- // For debugging only. private void Print_Positions(int[] Positions) { System.out.println("N=" + Positions.length); for(int I=0; I