Go 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.
package main
import "fmt"
var board [8]int
func iabs(val int) int {
if val >= 0 {
return val
}
return -val
}
func EightQueens(line int) bool {
if line > 7 {
return true
}
for column:=0;column<8;column++ {
endangered := false
for line2:=0;line2<line;line2++ {
if board[line2] == column || iabs(line-line2) == iabs(column-board[line2]) {
endangered = true
break
}
}
if !endangered {
board[line] = column
if EightQueens(line+1) {
return true
}
}
}
return false
}
func main() {
if EightQueens(0) {
for i:=0;i<8;i++ {
for j:=0;j<8;j++ {
if board[i]==j {
fmt.Print("Q")
} else {
fmt.Print(".")
}
}
fmt.Println()
}
}
}