I have dedicated in Programming in Scala for about 4 months. My work is busy, but I can’t give up reading more books. Scala is a fabulous language, both object oriented and functional. Eight qeens problem can be expressed in scala easily and concise.

Eight Queens Problem

Given a standard chess-board, place eight queens such that no queen is in check from any other (a queen can check another piece if they are on the same column, row, or diagonal)

Solutions

The problem in scala is recursively.

First each solution is a List[(Row, Column)]

- Each element is a coordinated, the queen position in each row
- The coordicate for row k comes first, followed by row `k-1`, `k-2`, ... 0

Use a Set[List[Row, Column]], represent all solutions

To place next k+1 qeen, we iterate all solutions, if match the condition, yield another list

Lets run the code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

/**

* the coordinates of the queen in row k comes first in each List[(ROW, Column)], followed