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)
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
* the coordinates of the queen in row k comes first in each List[(ROW, Column)], followed