Example #1
0
func BenchmarkSolve10(b *testing.B) {
	for n := 0; n < b.N; n++ {
		solver := solver.New(10, 10, 10)

		solCount := 0
		for range solver.SolChan() {
			solCount++
		}

		if solCount != 724 {
			b.Fatalf("Expected 724 solutions, got: %d", solCount)
		}
	}
}
Example #2
0
func BenchmarkSolve8(b *testing.B) {
	for n := 0; n < b.N; n++ {
		solver := solver.New(8, 8, 8)

		solCount := 0
		for range solver.SolChan() {
			solCount++
		}

		if solCount != 92 {
			b.Fatalf("Expected 92 solutions, got: %d", solCount)
		}
	}
}
Example #3
0
func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "usage:  %s [options]\n", os.Args[0])
		flag.PrintDefaults()
	}

	flag.IntVar(&options.boardWidth, "width", 8, "board width")
	flag.IntVar(&options.boardHeight, "height", 8, "board height")
	flag.IntVar(&options.queenCount, "queens", 8, "number of queens to place")
	flag.BoolVar(&options.draw, "draw", true, "draw board solutions")
	flag.Parse()

	if options.boardWidth < 1 || solver.MaxBoardWidth < options.boardWidth {
		fmt.Fprintf(os.Stderr, "width must be between 1 and %d", solver.MaxBoardWidth)
		os.Exit(1)
	}
	if options.boardHeight < 1 || solver.MaxBoardHeight < options.boardHeight {
		fmt.Fprintf(os.Stderr, "height must be between 1 and %d", solver.MaxBoardHeight)
		os.Exit(1)
	}
	if options.queenCount < 1 || solver.MaxQueens < options.queenCount {
		fmt.Fprintf(os.Stderr, "queens must be between 1 and %d", solver.MaxQueens)
		os.Exit(1)
	}

	solver := solver.New(int8(options.boardWidth), int8(options.boardHeight), int8(options.queenCount))

	solCount := 0
	rb := newRasterizedBoard(int8(options.boardWidth), int8(options.boardHeight))

	for queens := range solver.SolChan() {
		solCount++

		if options.draw {
			rb.clear()
			for _, q := range queens {
				rb.set(q.X, q.Y)
			}

			fmt.Println(rasterizedBoardToString(rb, 'Q', '-'))
		}
	}

	fmt.Println("Solutions:", solCount)
}