Question
class NQueens { private int BOARD_SIZE; public static final int EMPTY = 0; public static final int QUEEN = -1; public int board[][]; private int
class NQueens { private int BOARD_SIZE; public static final int EMPTY = 0; public static final int QUEEN = -1; public int board[][]; private int backTracks; private int isAttackedCalls; private int numPlacedQueens; public NQueens(int size) { BOARD_SIZE = size; board = new int[BOARD_SIZE][BOARD_SIZE]; backTracks = 0; isAttackedCalls = 0; numPlacedQueens = 0; } // end constructor
public boolean placeQueens() { return placeQueens(0);} private boolean placeQueens(int column) { if (column == BOARD_SIZE) { return true; // base case } else { boolean queenPlaced = false; int row = 0; // number of square in column while ( !queenPlaced && (row Precondition: A queen was placed in this *position and it is being removed as part of the backtracking process.
/** Marks the column number col+distance
to denote the location that can be attacked by the queen being placed.
col+distance
column will be marked up @param oldMarker identifies the old marker @param newMarker identifies the new marker */ private void markForward (int row, int col, int distance, int oldMarker, int newMarker) { // TO COMPLETE } private boolean isAttacked(int row, int column) { isAttackedCalls ++; if (board[row][column]!= EMPTY) return true; else return false;} // end isAttacked public String getStatsInHTML(){ return "Statistics for NQueens on a "+BOARD_SIZE+" x "+BOARD_SIZE +" chess board " +"Number of Back Tracks: "+backTracks +" " +"Number of isAttacked() calls : "+isAttackedCalls +" " +"Number of times Queens were placed: "+numPlacedQueens +" ";}} // end NQueens
NQueensGUI .java
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.TableModelListener; import javax.swing.table.*;
public class NQueensGUI implements ActionListener{ NQueens nQueens; JFrame boardFrame; JPanel boardPanel; JTextField sizeField; JLabel sizeLabel; JButton placeButton; BoardModel bModel; JTable board; JLabel statusLabel; TableCellRenderer renderer; JLabel statsLabel; public NQueensGUI() { JFrame.setDefaultLookAndFeelDecorated(true); boardFrame = new JFrame("NQueens"); boardFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); boardPanel = new JPanel(); boardPanel.setOpaque(true); addWidgets(); boardFrame.getContentPane().add(boardPanel, BorderLayout.CENTER); boardFrame.getRootPane().setDefaultButton(placeButton); boardFrame.setSize(new Dimension(600, 600)); boardFrame.setVisible(true); } public void addWidgets() { sizeField = new JTextField(2); sizeField.setHorizontalAlignment(JTextField.TRAILING); sizeField.setSize(30, 10); sizeLabel = new JLabel("number of queens"); placeButton = new JButton("Place queens"); placeButton.setSize(30, 20); statusLabel = new JLabel(); statusLabel.setSize(40, 10); bModel = new BoardModel(); board = new JTable(bModel); board.setGridColor(Color.CYAN); board.setShowGrid(true); board.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); JScrollPane scrollpane = new JScrollPane(board); renderer = new CustomTableCellRenderer(); statsLabel = new JLabel(); statsLabel.setHorizontalTextPosition(4); board.setDefaultRenderer( Object.class, renderer ); placeButton.addActionListener(this); boardPanel.add(sizeLabel); boardPanel.add(sizeField); boardPanel.add(placeButton); boardPanel.add(statusLabel); boardPanel.add(scrollpane, BorderLayout.CENTER); boardPanel.add(statsLabel); } public void actionPerformed(ActionEvent event) { Object[][] data = null; statusLabel.setText("Computing ..."); bModel.update(data); statsLabel.setText( "" + "
Computing
" +""); data = new Object[size][size]; Object[] columnName = new String[size]; cputime = finish - start; for (int i = 0; i " + "CPU time =" +((float) cputime/1000)+ "sec " +nQueens.getStatsInHTML()+"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started