I am trying to run the following game of chess from the console:
package chess
import java.io.Console
object Game {
def readCorrectMove(c: Console): String = {
val move: String = c.readLine()
if(!(move.length == 4 &&
1 <= move(0).toInt && move(0).toInt <= 8 &&
'a' <= move(0) && move(0) <= 'h' &&
1 <= move(0).toInt && move(0).toInt <= 8 &&
'a' <= move(0) && move(0) <= 'h')) {
println("Wrong move!")
readCorrectMove(c: Console)
} else {
move
}
}
def main(args: Array[String]) {
val c: Console = System.console()
val board = new Board()
c.format(board.toString)
while(true) {
val wm = readCorrectMove(c)
c.format(board.toString)
// if(board.checkMate()) {
// println("White wins!!!")
// return
// }
val bm = readCorrectMove(c)
c.format(board.toString)
// if(board.checkMate()) {
// println("Black wins!!!")
// }
}
}
}
I would like the program to wait for a move from the user, then to output the resulting board.
I'm not sure how to do that though.
If I run the code, I get a NullPointerException
on this line:
c.format(board.toString)
This probably means that I'm not using Console
correctly.
So how can I create an interactive console that takes input from the user, in a Scala class?