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

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

Scala Type Mismatch, Can't Resolve the Problem

问题

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

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

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

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

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

非常感谢。

添加了先前项目的示例:

  1. var playAnimal: List[Point] = List(Point(2,0), Point(1,0), Point(0,0))
  2. def checkForWrap(p: Point): Point = {
  3. var x = p.x; var y = p.y
  4. x = if (x < 0) nrColumns - 1 else if (x > nrColumns - 1) 0 else x
  5. y = if (y < 0) nrRows - 1 else if (y > nrRows - 1) 0 else y
  6. Point(x, y)
  7. }
  8. def moveAnimal(): Unit = {
  9. if (!gameOver) {
  10. def newAnimalFront: Point = {
  11. val newHead: Point = currentDir match {
  12. case East() => playAnimal.head + Point(1, 0)
  13. case West() => playAnimal.head + Point(-1, 0)
  14. case North() => playAnimal.head + Point(0, -1)
  15. case South() => playAnimal.head + Point(0, 1)
  16. }
  17. checkForWrap(newHead)
  18. }
  19. playAnimal = newAnimalFront +: playAnimal.init
  20. }
  21. }

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

英文:

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.

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

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:

  1. var playAnimal: List[Point] = List(Point(2,0), Point(1,0), Point(0,0))
  2. def checkForWrap (p: Point) : Point = {
  3. var x = p.x; var y = p.y
  4. x = if (x &lt; 0) nrColumns - 1 else if (x &gt; nrColumns - 1) 0 else x
  5. y = if (y &lt; 0) nrRows - 1 else if (y &gt; nrRows - 1) 0 else y
  6. Point(x, y)
  7. }
  8. def moveAnimal(): Unit = {
  9. if(!gameOver) {
  10. def newAnimalFront: Point = {
  11. val newHead: Point = currentDir match {
  12. case East() =&gt; playAnimal.head + Point( 1, 0)
  13. case West() =&gt; playAnimal.head + Point(-1, 0)
  14. case North() =&gt; playAnimal.head + Point( 0, -1)
  15. case South() =&gt; playAnimal.head + Point( 0, 1)
  16. }
  17. checkForWrap(newHead)
  18. }
  19. playAnimal = newAnimalFront +: playAnimal.init
  20. }
  21. }

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

答案1

得分: 1

两件事情你需要做:

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

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

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

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:

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

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:

确定