VB.NET Source Code

' eight-queens
' Author: Hannes du Plooy 
' Date: 20 Sep 2016      
' Objective: To solve the following
' Search for a chess board solution with 8 queens and no one
' endangering any other.
Module EightQueens

    Dim board(8) as Integer

    Function EightQueens(line As Integer) As Boolean
        Dim column As Integer
        
        If (line>7) Then
            Return true
        End If
        For column = 0 to 7
            Dim line2 As Integer
            Dim endangered As Boolean = false
            For line2 = 0 to line-1 
                If (board(line2) = column Or Math.Abs(line-line2) = Math.Abs(column-board(line2))) Then
                    endangered = true
                    Exit For
                End If
            Next line2
            If Not endangered Then
                board(line) = column
                If EightQueens(line+1) Then
                    Return true
                End IF
            End If
        Next column
        Return false
    End Function
    
    Sub Main()
        Dim i As Integer
        Dim j As Integer
        
        If EightQueens(0) Then
            For i = 0 to 7 
                For j = 0 to 7
                    If board(i) = j Then
                        Console.Write("Q")
                    Else
                        Console.Write(".")
                    End If
                Next j
                Console.WriteLine()
            Next i
        Else
            Console.WriteLine("No Solution Found")
        End If
    End Sub

End Module