0

I am trying to make a simple minesweeper game in Scala and I am trying to make a method that calls a nested method for randomly placing mines into the grid.

I am using the ofDim method on the Array to make it multi-dimensional which works great, until I specify it as the methods return type. The error I get is:

type ofDim is not a member of Object Array

The code is as follows:

class GridBuilder(x:Int, y:Int, mines:Int) {
  def generateGrid: Array.ofDim[String](x, y) = {
    def placeMines(mineLocations: Array.ofDim[String](x, y)): Array.ofDim[String](x, y) = {
      val xcoord = Random.nextInt(x)
      val ycoord = Random.nextInt(y)
      if (mineLocations.count(_ ==  "Bomb") == mines)
        mineLocations
      else if (mineLocations(xcoord) (ycoord) contains "Mine")
        placeMines(mineLocations)
      else 
        placeMines(mineLocations(xcoord)(ycoord) = "Mine")
      }
    placeMines(new Array.ofDim[String](x, y))
  }
}

I haven't found anything about returning multi-dimensional arrays anywhere. Is this possible in Scala? What am I missing here?

2 Answers 2

4
  1. Array.ofDim is a method, not a type,
  2. If you look at how it's implemented, it's a bunch of overloaded methods, each having different return type. In your case Array[Array[String]].
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was banging my head against the wall for ages looking for that! Works perfectly now
0

I threw together a quick little example so you can compare what you end up with. It is by no means perfect, and doesn't cover invalid user input, or game over etc, but here it is anyway.

import scala.annotation.tailrec
import scala.util.Random

sealed trait GridValue
case object Bomb extends GridValue
case object Empty extends GridValue
case object Checked extends GridValue

case class Grid(private var grid: List[List[GridValue]]){
  def click(x: Int, y: Int): String = {
    grid(x)(y) match {
      case Bomb    => "BOOM! You lose!"
      case Empty   =>
        grid = grid.updated(x, grid(x).updated(y, Checked))
        "No bomb! Good Job"
      case Checked => "You've already clicked here!"
    }
  }
}

object Grid{
  def buildGrid(x: Int, y: Int, mines: Int): Grid = {
    @tailrec
    def placeMines(grid: List[List[GridValue]] = List.fill(x)(List.fill(y)(Empty)), minesRemaining: Int = mines): List[List[GridValue]] = {
      if(minesRemaining == 0) {
        grid
      } else {
        val xcoord = Random.nextInt(x)
        val ycoord = Random.nextInt(y)
        grid(xcoord)(ycoord) match {
          case Bomb => placeMines(grid, minesRemaining)
          case Empty => placeMines(grid.updated(xcoord,grid(xcoord).updated(ycoord, Bomb)), minesRemaining - 1)
        }
      }
    }
    Grid(placeMines())
  }
}

//Start game: val game = Grid.buildGrid(100, 100, 5)

//Click: game.click(10, 10)

Comments

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.