# NQueen solution in scala

This is my solution for NQueen problem in scala. I just want to know how to make it more easy to read, modular and areas i need to improve.

object Main {
def main(args: Array[String]): Unit = {
val matrix = Array.ofDim[Int](16,16)

val queen_place = (2,2)

nQueenSol(matrix,matrix.size)

}

def diagonal_rule(x1:Int,y1:Int,x2:Int,y2:Int) = x1 + y1 == x2 + y2 || x1 - y1 == x2 - y2
def vertical_or_horizontal_rule(x1:Int,y1:Int,x2:Int,y2:Int) = x1 == x2 || y1 == y2

def queen_places(x:Int,y:Int,chess_board:Array[Array[Int]]):Array[Array[Int]] = {
for(i <- 0 until chess_board.size;
j <- 0 until chess_board.size) {
if(chess_board(i)(j) == 0)
chess_board(i)(j) = if(diagonal_rule(x,y,i,j)|| vertical_or_horizontal_rule(x,y,i,j))1 else 0

}
chess_board
}
def printChessBoard(array: Array[Array[Int]]): Unit ={
array.foreach(y => {
y.foreach(x => print(x + " "))
println()
}
)
}

def queenNotPresentAt(chessBoard:Array[Array[Int]])(x:Int,y:Int) = chessBoard(x - 1)(y) == 0

def nQueenSol(chessBoard:Array[Array[Int]],nthQueen:Int): Unit ={

if(nthQueen == 0) {printChessBoard(chessBoard);println}
else {

for(i <- 0 until chessBoard.size){
val touched = chessBoard.map(_.clone())

if(queenNotPresentAt(touched)(nthQueen, i)) {
touched(nthQueen - 1)(i) = 2
val newchessBoard = queen_places(nthQueen - 1,i,touched)

nQueenSol(newchessBoard,nthQueen - 1)

}
}
}
}
}

• If you want others to look at your code then you might spend some time making it presentable. You know, removing unnecessary white-space, making sure all comments are relevant to the current code, ... stuff like that. – jwvh Nov 6 '18 at 8:24