Problem: The Eight Queens problem is to find a solution to place a queen in each row on a chessboard such that no two queens can attack each other. Code below package queenchees; import static java.lang.Byte.SIZE; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.Pane; import javafx.stage.Stage; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.shape.Line; public class QueenChees extends Application { // Declare a variable private static final int SIZE = 8; // Declare an array private int[] queens = new int[SIZE]; // Override start method @Override // Start method public void start(Stage primaryStage) { // Call the function search(0); // Create a chess CB ChessBoard CB = new ChessBoard(); // Create a scene Scene scene = new Scene(CB, 250,250); // Create a scene primaryStage.setTitle("Exercise18_34"); // Place the scene on the stage primaryStage.setScene(scene); // Display the stage primaryStage.show(); // Call the function CB.paint(); // SET THE width property scene.widthProperty().addListener(ov -> CB.paint()); } // Set the height property scene.heightProperty().addListener(ov -> CB.paint()); ERROR } // Function definition private boolean is_Valid(int brow, int bcolumn) ERROR { // Loop from 1 through brow for(int i=1; i<=brow; i++) // Check the column, up left diagonal and up right diagonal if(queens[brow-i]==bcolumn|| queens[brow-i]==bcolumn-i|| queens[brow-i]==bcolumn+i) ERROR // Return false return false; ERROR // Return true return true; } // Function defintion private boolean search(int brow) ERROR { // Check if the value of brow is equal to size if (brow==SIZE) // Return true return true; // Loop from 0 through size for (int bcolumn = 0; bcolumn

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Problem: The Eight Queens problem is to find a solution to place a queen in each row on a chessboard such that no two queens can attack each other.

Code below

package queenchees;

import static java.lang.Byte.SIZE;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.Line;


public class QueenChees extends Application {

    // Declare a variable
    private static final int SIZE = 8;

    // Declare an array
    private int[] queens = new int[SIZE];
    // Override start method

    @Override
    // Start method
    public void start(Stage primaryStage)
    {
        // Call the function
        search(0);

        // Create a chess CB
        ChessBoard CB = new ChessBoard();
        
        // Create a scene
        Scene scene = new Scene(CB, 250,250);

        // Create a scene
        primaryStage.setTitle("Exercise18_34");

        // Place the scene on the stage
        primaryStage.setScene(scene);

        // Display the stage
        primaryStage.show();

        // Call the function
        CB.paint();

        // SET THE width property
        scene.widthProperty().addListener(ov -> CB.paint());
       }

       // Set the height property
       scene.heightProperty().addListener(ov -> CB.paint());              ERROR
       }


      // Function definition
      private boolean is_Valid(int brow, int bcolumn)           ERROR
       {

     // Loop from 1 through brow
     for(int i=1; i<=brow; i++)
     // Check the column, up left diagonal and up right diagonal
     if(queens[brow-i]==bcolumn|| queens[brow-i]==bcolumn-i|| queens[brow-i]==bcolumn+i)    ERROR
    
     // Return false
     return false;                            ERROR

     // Return true
     return true;
      }

     // Function defintion
     private boolean search(int brow)           ERROR

{

    // Check if the value of brow is equal to size
    if (brow==SIZE)
    // Return true
     return true;

    // Loop from 0 through size
    for (int bcolumn = 0; bcolumn<SIZE; bcolumn++)
{
    // Place the queen
    queens[brow]=bcolumn;

      // Check if the block is valid
     if(is_Valid(brow,bcolumn)&&search(brow+1))
    // Return true
     return true;
    }
    // Return false
     return false;
     }


    // Class ChessBoard
    private class ChessBoard extends Pane
    {
        
    // Get the image
        Image queen_Image = new Image("queen.jpg");
        
        // Function to paint
        public void paint()
        {
            
            // Clear the pane
            this.getChildren().clear();
            
            // Loop to place the queen
            for(int i=0; i<SIZE; i++)
            {
                
                // Create a image view
                ImageView queen_ImageView = new ImageView(queen_Image);
                
                // Add the image view to the pane
                this.getChildren().add(queen_ImageView);
                
                
                // Set thje position of the queen
                int j = queens[i];                             ERROR  
                
                // Properly position the queen in the blocks
                queen_ImageView.setX(j*getWidth()/SIZE);
                queen_ImageView.setY(j*getHeight()/SIZE);
                queen_ImageView.setFitWidth(getWidth()/SIZE);
                queen_ImageView.setFitHeight(getHeight()/SIZE);
            }
            
            // Loop to draw the line
            for (int i = 1; i<=SIZE; i++)  
            {
            
                // Create two lines
                this.getChildren().add(new Line(0, i*getHeight()/SIZE, getWidth(), i*getHeight()/SIZE));
                
                this.getChildren().add(new Line(i*getWidth()/SIZE,0, getWidth()/SIZE, getHeight()));
                
            }
        }
    }
    public static void main(String[] args)    ERROR
{
        launch(args);   ERROR
    }                          ERROR
    
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Adjacency Matrix
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education