I just started learning Scala, trying to code a simple game to learn it's syntax and some basic rules. I'd be grateful if anyone could help me with a simple problem. I created simple board filled with ".". Now I want to change one "." on the certain index and put there "X". In java it would look like this:
board[col][row] = symbol;
This is the board part:
val board = {
collection.mutable.ArrayBuffer[String]()
collection.mutable.ArrayBuffer.fill(cols, rows)(".")
}
def show(): Unit = {
board.foreach(x => println(x.mkString))
}
def setField(col:Int, row:Int, fieldSymbol:String): Unit = {
//fieldSymbol on index, which is (col*row) as it's 1d array
//thought board.apply(col*row): fieldSymbol might work but it's not
//correct I guess
}
Any help would be much appreciated. Thanks in advance for advice.
board(i)to access the i-th value of a collection (same as Java's[i]).collection.mutable.ArrayBuffer[String]()This is creating an empty array then (since you don't do anything with it), discarding it. You only need thefillline(cols, rows)then it means I have something like 2d array? Like, can I doboard(col, row) = fieldSymbol?Arrayis a JavaArray). So you needboard(col)(row)(I'm working so I'm not going to be a real-time further-question-answering service, post "proper" questions if required (or consult a Scala tutorial!)