Problem - Given a chess board and the position of only one queen piece in it, mark all the position where the queen can move with X.
Kindly review this Scala code and suggest improvements.
import scala.collection.mutable._
object ChessQueenAttack {
val board = Array.fill(8)(Array.fill(8)('0'))
def printBoard = {
println
println
board foreach { row => println(row.mkString(" "))}
}
def main(args: Array[String]): Unit = {
printBoard
val qR = args(0).toInt
val qC = args(1).toInt
board(qR)(qC) = 'Q'
printBoard
var (r,c) = (qR, qC)
// horizontal
for ( i <- 0 until 8 ) {
if (i != qC) board(qR)(i) = 'X'
if (i != qR) board(i)(qC) = 'X'
}
// top left
r = qR -1
c = qC -1
while (r >= 0 && c >= 0) {
board(r)(c) ='X'
r -= 1
c -= 1
}
// top right
r = qR - 1
c = qC + 1
while (r >= 0 && c < 8) {
board(r)(c) = 'X'
r -= 1
c += 1
}
// bottom right
r = qR + 1
c = qC + 1
while (r < 8 && c < 8 ) {
board(r)(c) = 'X'
r += 1
c += 1
}
// bottom left
r = qR + 1
c = qC - 1
while (r<8 && c>=0) {
board(r)(c) = 'X'
r += 1
c -= 1
}
printBoard
}
}
Output -
scalac ChessQueenAttack.scala
scala ChessQueenAttack 3 3
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 Q 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
X 0 0 X 0 0 X 0
0 X 0 X 0 X 0 0
0 0 X X X 0 0 0
X X X Q X X X X
0 0 X X X 0 0 0
0 X 0 X 0 X 0 0
X 0 0 X 0 0 X 0
0 0 0 X 0 0 0 X