Skip to content

Mebanseibor/N-Queens-Visualiser-Java-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

♛ N-Queens Visualiser ♛

Table of Content

Content
🧩Problem Statement
🎯Objective
🖼️Preview
📖Terminology
💾About the program
🔀Flowchart
🔮Future Enhancements
🚩Development Issues
🎓Things Learnt
🔗Links

Problem Statement🧩

The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.

Objective🎯

Given an integer n, we must display all distinct solutions to the n-queens puzzle

Preview🖼️

Main Window

Main Window of the program

Result Window

Result Window of the program

Terminology📖

  • Rank
    • In chess, a rank refers to a straight full set of squares on the horizontal axis
  • File
    • In chess, a file refers to a straight full set of squares on the vertical axis
  • Frame
    • A frame is a window using by java
  • Panel
    • A panel is a section within a frame

About the program💾

  • Programming Language used

    • Java
  • Package(s) used

    • java.util.Scanner
      • To take input from the user
    • java.util.List
      • To store the answer/results with a list
    • java.util.ArrayList
      • To instantiate an abstract list
    • import java.awt
      • To create Abstract Window Toolkit import javax.swing.*;
      • Easy to use GUI components

Flowchart🔀

Over View

---
title: Overview
---
flowchart
    Start([Start])
    CreateMainUI[Create the Main UI]
    RunAlgorithm[Run Algorithm]
    CreateResultUI[Create the Result UI]
    End([End])

    Start --> CreateMainUI --> RunAlgorithm --> CreateResultUI --> End
Loading

Rendering the Live Preview

---
title: Rendering the Live Preview
---
flowchart
    StartPlaceQueen(["Start of placeQueen(Function)"])
    VisuallyPlaceQueen[Visually Place Queen]
    DecisionQueenPositionIsValid{"
        Is
        Queen placed at
        Valid Square
    "}
    VisuallyRemoveQueen["
        Visually Remove
        Last Placed Queen
    "]
    BackTrack([Perform Back Tracking])
    EndPlaceQueen(["
        Run placeQueen()
        for next queen
    "])

    
    StartPlaceQueen --> VisuallyPlaceQueen --> CheckingForOtherQueens
    
    %% Checking for other queens
    subgraph CheckingForOtherQueens [Checking for other queens]
        direction TB
        StartChecking([Start Checking])
        CheckReturnStateSameFile{"
            Is
            The file clear of
            other queens
        "}
        CheckReturnStateLeftDiagonal{"
            Is
            The Left Diagonal
            clear of other queens
        "}
        CheckReturnStateRightDiagonal{"
            Is
            The Right Diagonal
            clear of other queens
        "}
        
        ReturnTrue([Return True])
        ReturnFalse([Return False])
        
        %% SubgraphSameFileCheck
        subgraph SubgraphSameFileCheck [Checking in the same file]
            direction TB
            SameFileCheckIfSquareIsValid{"
                If
                Square
                is Valid
            "}
            SameFileHighlightValid["HighLight as Valid Square✅"]
            SameFileSqaureWithinOfBoard{"
                Is
                Next Sqaure on
                the same file still
                within the Board
            "}
            SameFileReturnTrue([Return true])
            SameFileHighlightInvalid["HighLight as Invalid Square❌"]
            SameFileNavigateToNextSquare["
                Navigate to Next Square
                of Same File
            "]
            SameFileClearHighlights[Clear All Highlights]
            SameFileRevertPosition["
                Revert board position to
                last non-conflict position
            "]
            SameFileReturnFalse([Return false])
            
            %% SameFileCheckIfSquareIsValid
                SameFileCheckIfSquareIsValid --> |Yes🟢| SameFileHighlightValid --> SameFileNavigateToNextSquare --> SameFileSqaureWithinOfBoard 
                SameFileCheckIfSquareIsValid --> |No🔴| SameFileHighlightInvalid --> SameFileClearHighlights --> SameFileRevertPosition --> SameFileReturnFalse
            
            %% SameFileSqaureWithinOfBoard
                SameFileSqaureWithinOfBoard -->|Yes🟢| SameFileCheckIfSquareIsValid
                SameFileSqaureWithinOfBoard -->|No🔴| SameFileReturnTrue
        end

        %% SubgraphLeftDiagonalCheck
        subgraph SubgraphLeftDiagonalCheck [Checking in the Left Diagonal]
            direction TB
            LeftDiagonalCheckIfSquareIsValid{"
                If
                Square
                is Valid
            "}
            LeftDiagonalHighlightValid["HighLight as Valid Square✅"]
            LeftDiagonalSqaureWithinOfBoard{"
                Is
                Next Sqaure on
                the Left Diagonal still
                within the Board
            "}
            LeftDiagonalReturnTrue([Return true])
            LeftDiagonalHighlightInvalid["HighLight as Invalid Square❌"]
            LeftDiagonalNavigateToNextSquare["
                Navigate to Next Square
                of Left Diagonal
            "]
            LeftDiagonalClearHighlights[Clear All Highlights]
            LeftDiagonalRevertPosition["
                Revert board position to
                last non-conflict position
            "]
            LeftDiagonalReturnFalse([Return false])
            
            %% LeftDiagonalCheckIfSquareIsValid
                LeftDiagonalCheckIfSquareIsValid --> |Yes🟢| LeftDiagonalHighlightValid --> LeftDiagonalNavigateToNextSquare --> LeftDiagonalSqaureWithinOfBoard 
                LeftDiagonalCheckIfSquareIsValid --> |No🔴| LeftDiagonalHighlightInvalid --> LeftDiagonalClearHighlights --> LeftDiagonalRevertPosition --> LeftDiagonalReturnFalse
            
            %% LeftDiagonalSqaureWithinOfBoard
                LeftDiagonalSqaureWithinOfBoard -->|Yes🟢| LeftDiagonalCheckIfSquareIsValid
                LeftDiagonalSqaureWithinOfBoard -->|No🔴| LeftDiagonalReturnTrue
        end

        %% SubgraphRightDiagonalCheck
        subgraph SubgraphRightDiagonalCheck [Checking in the Right Diagonal]
            direction TB
            RightDiagonalCheckIfSquareIsValid{"
                If
                Square
                is Valid
            "}
            RightDiagonalHighlightValid["HighLight as Valid Square✅"]
            RightDiagonalSqaureWithinOfBoard{"
                Is
                Next Sqaure on
                the Right Diagonal still
                within the Board
            "}
            RightDiagonalReturnTrue([Return true])
            RightDiagonalHighlightInvalid["HighLight as Invalid Square❌"]
            RightDiagonalNavigateToNextSquare["
                Navigate to Next Square
                of Right Diagonal
            "]
            RightDiagonalClearHighlights[Clear All Highlights]
            RightDiagonalRevertPosition["
                Revert board position to
                last non-conflict position
            "]
            RightDiagonalReturnFalse([Return false])
            
            %% RightDiagonalCheckIfSquareIsValid
                RightDiagonalCheckIfSquareIsValid --> |Yes🟢| RightDiagonalHighlightValid --> RightDiagonalNavigateToNextSquare --> RightDiagonalSqaureWithinOfBoard 
                RightDiagonalCheckIfSquareIsValid --> |No🔴| RightDiagonalHighlightInvalid --> RightDiagonalClearHighlights --> RightDiagonalRevertPosition --> RightDiagonalReturnFalse
            
            %% RightDiagonalSqaureWithinOfBoard
                RightDiagonalSqaureWithinOfBoard -->|Yes🟢| RightDiagonalCheckIfSquareIsValid
                RightDiagonalSqaureWithinOfBoard -->|No🔴| RightDiagonalReturnTrue
        end
        
        StartChecking --> SubgraphSameFileCheck --> CheckReturnStateSameFile
        %% CheckReturnStateSameFile
            CheckReturnStateSameFile --> |Yes🟢| SubgraphLeftDiagonalCheck
            CheckReturnStateSameFile --> |No🔴| ReturnFalse

        SubgraphLeftDiagonalCheck --> CheckReturnStateLeftDiagonal
        %% CheckReturnStateLeftDiagonal
            CheckReturnStateLeftDiagonal --> |Yes🟢| SubgraphRightDiagonalCheck
            CheckReturnStateLeftDiagonal --> |No🔴| ReturnFalse
        
        SubgraphRightDiagonalCheck --> CheckReturnStateRightDiagonal
        %% CheckReturnStateRightDiagonal
            CheckReturnStateRightDiagonal --> |Yes🟢| ReturnTrue
            CheckReturnStateRightDiagonal --> |No🔴| ReturnFalse
    end


    CheckingForOtherQueens --> DecisionQueenPositionIsValid
    

    %% DecisionQueenPositionIsValid
        DecisionQueenPositionIsValid --> |Yes🟢| EndPlaceQueen
        DecisionQueenPositionIsValid --> |No🔴| VisuallyRemoveQueen
    
    VisuallyRemoveQueen --> BackTrack
Loading

Future Enhancements🔮

  • Better error handling

    • Implement error handling when a non-valid integer is entered as the number of queens
    • Implement error handling for the function buttonCompute.addActionListener()

Development Issues Faced🚩

  • Stretched out grid when using GridLayout

    • Cause of the problem

      • Grid Layout did not respect the panel's preferred size
    • Solution

      • Set the size of panel with the grid layout with setPreferredSize instead of setSize, example:
        • Use:
          panelChessBoard.setPreferredSize(new Dimension(boardLength, boardLength));
        • Instead of:
          panelChessBoard.setSize(boardLength, boardLength);
      • Add the panel to the Frame using a wrapper:
        JPanel wrapperPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        wrapperPanel.add(panelLivePreview);
        frameMain.add(wrapperPanel);
        frameMain.pack();

Things Learnt🎓

  • Using packages catered for rendering graphical items
  • Handling threads

  • About the problem:
  • Completed project reference:
  • Drawing a chess board in Java:

About

A solution to the N-Queens problem presented in a GUI using Java

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages