Python 3 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.

board = [0,0,0,0,0,0,0,0]

def EightQueens(line):
    if line>7:
        return True
    for column in range(8):
        endangered = False
        for line2 in range(line):
            if board[line2] == column or abs(line-line2) == abs(column-board[line2]):
                endangered = True
                break
        if not endangered:
            board[line] = column
            if EightQueens(line+1):
                return True
    return False
    
if EightQueens(0):
    for i in range(8):
        tmp = ""
        for j in range(8):
            if board[i] == j:
                tmp += "Q"
            else:
                tmp += "."
        print(tmp)