Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object Start extends App {
- val fieldSize = 8
- case class Figure(point: (Int, Int), config: FigureConfig) {
- private def withinBounds(p: (Int, Int)): Boolean = {
- def within(i: Int) = i > 0 || i <= fieldSize
- within(p._1) && within(p._2)
- }
- def beats(cell: (Int, Int)): Boolean = {
- withinBounds(cell) && (cell == point || config.beatsCell(cell, point))
- }
- }
- trait FigureConfig {
- def beatsCell(cell: (Int, Int), point: (Int, Int)): Boolean = false
- }
- trait DiagonalFigureConfig extends FigureConfig {
- override def beatsCell(cell: (Int, Int), point: (Int, Int)): Boolean = Math.abs(point._1 - cell._1) == Math.abs(point._2 - cell._2) || super.beatsCell(cell, point)
- }
- trait LineFigureConfig extends FigureConfig {
- override def beatsCell(cell: (Int, Int), point: (Int, Int)): Boolean = (cell._1 == point._1) || (cell._2 == point._2) || super.beatsCell(cell, point)
- }
- case object QueenConfig extends DiagonalFigureConfig with LineFigureConfig
- case object BishopConfig extends DiagonalFigureConfig
- case object RockConfig extends LineFigureConfig
- def solveWithOpenedCells(openCells: List[(Int, Int)], figs: List[FigureConfig]): List[List[Figure]] = {
- if (figs.isEmpty) {
- List(Nil)
- } else {
- for (
- openCell <- openCells;
- r = Figure(openCell, figs.head);
- rest <- solveWithOpenedCells(openCells.filterNot(r.beats), figs.tail)
- ) yield r :: rest
- }
- }
- def solve(figs: List[FigureConfig]): List[List[Figure]] = {
- val opened = for (x <- 1 until fieldSize + 1; y <- 1 until fieldSize + 1) yield (x, y)
- solveWithOpenedCells(opened.toList, figs)
- }
- val figures = List(QueenConfig, QueenConfig, RockConfig)
- solve(figures).foreach(println)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement