Perl 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.
#!/usr/bin/perl

use strict;
use warnings;

my @board = (0,0,0,0,0,0,0,0);

sub EightQueens {
    my $line = $_[0];
    if ($line > 7) {
        return 1;
    }
    for(my $column=0;$column<8;$column++) {
        my $endangered = 0;
        for(my $line2=0;$line2<$line;$line2++) {
            if ($board[$line2] == $column || abs($line-$line2)==abs($column-$board[$line2])) {
                $endangered = 1;
                last;
            }
        }
        if (!$endangered) {
            @board[$line] = $column;
            if (EightQueens($line+1)) {
                return 1;
            } 
        }
    }
    return 0;
}

if (EightQueens(0)) {
    for(my $i=0;$i<8;$i++) {
        for(my $j=0;$j<;$j++) {
            if ($board[$i] == $j) {
                print "Q";
            } else {
                print ".";
            }
        }
        print "\n";
    }
} else {
    print "No Solution\n";
}