英文:
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 <- 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 < 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
}
}
This method, however, is displaying the String mismatch in my current project.
答案1
得分: 1
两件事情你需要做:
- 在你的类
Point
中定义方法+
。 - 避免变异(这取决于你,但你的代码会更易读)。
然后你可以编写类似这样的代码:
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:
- Define in your class
Point
method+
. - 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't mutate the old one
placed.map(p => p + move)
}
case class Point(x: Int, y: Int) {
def +(p: Point) = Point(x + p.x, y + p.y)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论