Implement equals for Point

Fixes bug in distribution of snippet weights: Multiple equal points could
be in frontier and unvisited, therefore processed twice. Now set of points
adheres to set semantics (compares points by coordinates instead of reference).
This commit is contained in:
Armin Friedl 2017-11-11 09:56:22 +01:00
parent e337739b04
commit c66e87791e
2 changed files with 19 additions and 8 deletions

View file

@ -43,7 +43,9 @@ class SnippetAI extends Tactical {
def runner(frontier: Set[Point], distance: Int): Unit = { def runner(frontier: Set[Point], distance: Int): Unit = {
if (frontier.isEmpty) return if (frontier.isEmpty) return
// process the current frontier and build the new ones dbg(s"""Frontier[dist= $distance]: ${frontier.mkString(",")}""")
// process the current frontier and build the new one
val nextFrontier = val nextFrontier =
frontier filterNot { visited(_).get } flatMap { p => frontier filterNot { visited(_).get } flatMap { p =>
visited(p) put true visited(p) put true

View file

@ -1,14 +1,23 @@
package bot.environment.topology package bot.environment.topology
class Point (val x: Int, val y: Int){ class Point(val x: Int, val y: Int) {
def asTuple: (Int, Int) = (x,y) def asTuple: (Int, Int) = (x, y)
def +(deltaX: Int, deltaY: Int): Point = new Point(x+deltaX, y+deltaY) def +(deltaX: Int, deltaY: Int): Point = new Point(x + deltaX, y + deltaY)
def +(delta: (Int,Int)): Point = this + (delta _1, delta _2) def +(delta: (Int, Int)): Point = this + (delta _1, delta _2)
def +(direction: Direction): Point = this + direction.delta def +(direction: Direction): Point = this + direction.delta
override def toString: String = s"Point{.x=$x, .y=$y}" override def toString: String = s"Point{.x=$x, .y=$y}"
def canEqual(a: Any) = a.isInstanceOf[Point]
override def equals(that: Any): Boolean =
that match {
case that: Point => ( that.canEqual(this)
&& this.x == that.x
&& this.y == that.y )
case _ => false
}
override def hashCode: Int = 41 * (41 + x) + y
} }
object Point { object Point {
def apply(x: Int, y: Int) = new Point(x,y) def apply(x: Int, y: Int) = new Point(x, y)
} }