Click here to Skip to main content
6,293,171 members and growing! (12,250 online)
Email Password   helpLost your password?
Languages » VB.NET » Applications     Intermediate License: The Code Project Open License (CPOL)

Eight Queens Problem using VB.NET

By Ali Tarhini

Backtracking solution approach to solve the eight queens problems and get all unique solutions
VB, .NET
Version:3 (See All)
Posted:4 Jan 2009
Views:8,528
Bookmarked:13 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
29 votes for this article.
Popularity: 6.31 Rating: 4.32 out of 5

1

2
5 votes, 17.2%
3
2 votes, 6.9%
4
22 votes, 75.9%
5

Introduction

The Eight Queens Problem is a famous problem in AI field and a famous example about AI algorithms used to solve such a problem using backtracking method.

Background

The solution shows all unique solutions to the eight queens problem which are exactly 92 solutions, 12 of which are distinct. The application also allows you to play the game yourself and try to find your own solution. The algorithm uses backtracking and depth first limited search to level 8 (8 queens) to find a solution.

Using the Code

The Queen class is the basic object used in the algorithm and all over the project for graphics display too. It's fairly simple:

Public Class Queen
    Private mRow As Integer
    Private mColumn As Integer

    Public Sub New()
        mRow = 0
        mColumn = 0
    End Sub

    Public Sub New(ByVal Row As Byte, ByVal Column As Byte)
        mRow = Row
        mColumn = Column
    End Sub

    Public Property Row() As Integer
        Get
            Return mRow
        End Get
        Set(ByVal value As Integer)
            mRow = value
        End Set
    End Property

    Public Property Column() As Integer
        Get
            Return mColumn
        End Get
        Set(ByVal value As Integer)
            mColumn = value
        End Set
    End Property
End Class	

The ChessBoard user control draws the board and is actually where the algorithm of finding all solutions takes place. The function of interest is the MoveQueen function which moves a queen across the board, then checks if the new position is a good place or that the queen is attacked. The process is repeated recursively until all 8 queens are placed in a safe place.

    Private Sub MoveQueen(ByVal Level As Integer)
        If Level > 7 Then
            For j As Integer = 0 To 7
                For i As Integer = 0 To 7
                    If (Queens(j).Row = j) And (Queens(j).Column = i) Then
                        mCells(i, j) = True
                    Else
                        mCells(i, j) = False
                    End If
                Next
            Next
            Solutions.Add(mCells.Clone)
            Exit Sub
        End If
        For j As Integer = 0 To 7
            If Level < 8 Then
                Queens(Level).Row = Level
                Queens(Level).Column = j
                If CheckAll(Level) Then MoveQueen(Level + 1)
            End If
        Next
    End Sub	 

Finally, a call to GetSolutions will initiate the MoveQueen function starting from the first level, which is the depth of the current position in the depth tree. Reaching a level of 8 means we have found a solution. Otherwise, 1 level is decremented to the previous level and the search continues using different values. This process is called Backtracking.

    Public Sub GetSolutions()
        mUserPlay = False
        Playing = False
        Queens.Clear()
        ResetCells()
        DrawBoard()
        For j As Integer = 0 To 7
            Queens.Add(New Queen)
        Next
        For i As Integer = 0 To 7
            Queens(0).Row = 0
            Queens(0).Column = i
            MoveQueen(1)
        Next
    End Sub 

Points of Interest 

What's interesting here is that the solutions are ordered according to the board from top-left to bottom-right so you can have all solutions by repeating click on solve button.

History

  • 4th January, 2009: Initial post 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ali Tarhini


Member
Owner of the website:

free MCSA MCSE Certification exams
Occupation: Software Developer (Senior)
Company: Microgen
Location: Lebanon Lebanon

Other popular VB.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralThanks PinmemberAli Tarhini10:02 8 Jan '09  
GeneralGood start... PinmvpDave Kreskowiak9:44 8 Jan '09  
Generalvery interesting Pinmemberabou lwiss11:20 7 Jan '09  
GeneralThe code is nice, but the article needs...well...text PinmemberShane Story4:13 6 Jan '09  
GeneralExplanation PinmemberBaconbutty22:23 5 Jan '09  
GeneralRe: Explanation PinmemberInvisionsoft7:52 6 Jan '09  
Generalgreat article PinmemberEngForce14:39 4 Jan '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Jan 2009
Editor: Deeksha Shenoy
Copyright 2009 by Ali Tarhini
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project