Eight Queens Problem in Scala
- Cyanny Liang
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.
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+1qeen, we iterate all solutions, if match the condition, yield another list
git clone https://github.com/lgrcyanny/ScalaPractice.git