2

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.

6
  • Use board(i) to access the i-th value of a collection (same as Java's [i]). Commented Feb 3, 2016 at 13:52
  • Thank you, I didn't know it's that simple. Commented Feb 3, 2016 at 14:01
  • collection.mutable.ArrayBuffer[String]() This is creating an empty array then (since you don't do anything with it), discarding it. You only need the fill line Commented Feb 3, 2016 at 14:01
  • Let me ask some more questions so I won't be creating many trash topics. When I have (cols, rows) then it means I have something like 2d array? Like, can I do board(col, row) = fieldSymbol ? Commented Feb 3, 2016 at 14:05
  • You have a two-d array, but it's represented as an array of arrays. It's just Java (in fact a Scala Array is a Java Array). So you need board(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!) Commented Feb 3, 2016 at 14:09

1 Answer 1

2

You could implement setField for a flat array like this

def setField(c: Int, r: Int, v: String) {
  board(c * rows + row) = v
}

This is however only a good idea if you want to store your multi dimensional data in a single array (which can be interesting if you care about allocation structure and stuff like that). In this case I would recommend to write a generic method setField though.

If you instead want arrays of arrays fill already gives you that

val board = ArrayBuffer.fill(rows, cols)(".")

and then just update like this

board(x)(y) = "something"

You should however ask yourself whether you really need a mutable data structure and your program cannot be expressed more elegantly using immutable data structures. And also, if you really want a two dimensional vector if you want to represent something as a board of objects, especially if most of the board is "empty". It could be much more elegant to use a map:

val board = Map[(Int, Int), String]() // empty board
val nextBoard = bord + ((2,3) -> "something")
Sign up to request clarification or add additional context in comments.

6 Comments

What is the advantage of tabulate over fill? Or maybe fill just will not work here?
tabulate returns a nested structure, i.e. in this case ArrayBuffer[ArrayBuffer[String]] which is what you would typically have in Java as well.
By the way, there is also Array.tabulate(...) which would be preferable if you do not need arrays of variable size.
Yeah, I don't. I just overcomplicated everything. Thanks for help.
There is no advantage of using tabulate here, as you are not doing anything depending on the values of x and y. You should just use fill, as follows: ArrayBuffer.fill(rows, cols)(".")
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.