Scala类型不匹配,无法解决问题。

huangapple go评论67阅读模式
英文:

Scala Type Mismatch, Can't Resolve the Problem

问题

为了项目中的一个部分,我试图让游戏生成的方块移动。所有坐标都存储在一个列表中,通过 "x" 和 "y" 值,我应该能够累加到坐标上,从而使方块移动。

  def movement(move: Point): Unit = {
    val newList: List[Point] = placed
    val xx = move.x; val yy = move.y
    for (i <- newList.indices) newList(i) += Point(xx, yy)
  }

在这里,"placed" 是存放所有坐标的列表。"Point" 类型是指 "x" 和 "y" 值。

问题在于,当我尝试将新值添加到坐标时,它会显示:
> 类型不匹配。需要:字符串,找到:Point

我觉得这很奇怪,因为我的列表不是以字符串类型初始化的。有没有解决这个问题的方法?

非常感谢。

添加了先前项目的示例:

  var playAnimal: List[Point] = List(Point(2,0), Point(1,0), Point(0,0))

  def checkForWrap(p: Point): Point = {
    var x = p.x; var y = p.y
    x = if (x < 0) nrColumns - 1 else if (x > nrColumns - 1) 0 else x
    y = if (y < 0) nrRows - 1 else if (y > nrRows - 1) 0 else y
    Point(x, y)
  }

  def moveAnimal(): Unit = {
    if (!gameOver) {
      def newAnimalFront: Point = {
        val newHead: Point = currentDir match {
          case East()  => playAnimal.head + Point(1, 0)
          case West()  => playAnimal.head + Point(-1, 0)
          case North() => playAnimal.head + Point(0, -1)
          case South() => playAnimal.head + Point(0, 1) 
        }
        checkForWrap(newHead)
      }
      playAnimal = newAnimalFront +: playAnimal.init
    }
  }

然而,在我的当前项目中,这种方法显示了字符串不匹配的错误。

英文:

So, for a project, I'm trying to make a specific piece of a game generated block move. All coordinates are stored in a List and through "x" and "y" values I should be able to add up to the coordinates, and in turn make the block move.

  def movement(move: Point): Unit = {
    val newList: List[Point] = placed
    val xx = move.x; val yy = move.y
    for (i &lt;- newList.indices) newList(i) += Point(xx, yy)
  }

Here, "placed" is the List where all coordinates are placed. The "Point" type refers to the "x" and "y" values.

The problem here is that when I try to add the new values to the coordinate, it says:
> Type mismatch. Required: String, found: Point

I found this strange, since my list is not initiated with the string type. Is there any way to work around this problem?

Many thanks.

Added example of previous project:

  var playAnimal: List[Point] = List(Point(2,0), Point(1,0), Point(0,0))

  def checkForWrap (p: Point) : Point = {
    var x = p.x; var y = p.y
    x = if (x &lt; 0) nrColumns - 1 else if (x &gt; nrColumns - 1) 0 else x
    y = if (y &lt; 0) nrRows - 1 else if (y &gt; nrRows - 1) 0 else y
    Point(x, y)
  }

  def moveAnimal(): Unit = {
    if(!gameOver) {
      def newAnimalFront: Point = {
        val newHead: Point = currentDir match {
          case East()  =&gt; playAnimal.head + Point( 1,  0)
          case West()  =&gt; playAnimal.head + Point(-1,  0)
          case North() =&gt; playAnimal.head + Point( 0, -1)
          case South() =&gt; playAnimal.head + Point( 0,  1) 
        }
        checkForWrap(newHead)
      }
      playAnimal = newAnimalFront +: playAnimal.init
    }
  }

This method, however, is displaying the String mismatch in my current project.

答案1

得分: 1

两件事情你需要做:

  1. 在你的类Point中定义方法+
  2. 避免变异(这取决于你,但你的代码会更易读)。

然后你可以编写类似这样的代码:

  def main(args: Array[String]): Unit = {
    val placed: List[Point] = List(Point(0, 0), Point(1, 1))
    println(placed.mkString) // Point(0,0)Point(1,1)
    val moved = movement(Point(2, 2), placed)
    println(moved.mkString) //Point(2,2)Point(3,3)
  }
  def movement(move: Point, placed: List[Point]): List[Point] = {
    // 这里你创建了一个新的列表,没有改变旧的列表
    placed.map(p => p + move)
  }
  case class Point(x: Int, y: Int) {
    def +(p: Point) = Point(x + p.x, y + p.y)
  }
英文:

Two things you need to do:

  1. Define in your class Point method +.
  2. Avoid mutations(it's up to you but your code becomes more readable).

Then you can write smth like this:

  def main(args: Array[String]): Unit = {
    val placed: List[Point] = List(Point(0, 0), Point(1, 1))
    println(placed.mkString) // Point(0,0)Point(1,1)
    val moved = movement(Point(2, 2), placed)
    println(moved.mkString) //Point(2,2)Point(3,3)
  }
  def movement(move: Point, placed: List[Point]): List[Point] = {
    // here you create new list and don&#39;t mutate the old one
    placed.map(p =&gt; p + move)
  }
  case class Point(x: Int, y: Int) {
    def +(p: Point) = Point(x + p.x, y + p.y)
  }

huangapple
  • 本文由 发表于 2020年10月5日 02:06:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64198150.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定