Q: Rectangle2D.contains() not properly detecting coordinates in boundaries I am writing a tower defense game for my Intro to CS2 class. I have this piece of code to find the next coordinate on the map for an enemy unit:
  def findNextCoordinate(): (Double, Double) = {
    val numAccessibleCoordinates = 8
    val angle = 360 / numAccessibleCoordinates
    val pixelsPerFrame = speed / MainGame.fps //hypotenuse
    val currentCoor = coordinates

    def getAccessibleCoordinates(c: (Double, Double)): Array[(Double, Double)] = {
      val points = Array.fill(numAccessibleCoordinates)((0.0, 0.0))

      for (i <- 0 until numAccessibleCoordinates) {
        val currAngle = angle * i
        val x = math.cos(currAngle) * pixelsPerFrame
        val y = math.sin(currAngle) * pixelsPerFrame
        points(i) = ((currentCoor._1 + x), (currentCoor._2 + y))
      }
      points
    }

    val accessiblePoints = getAccessibleCoordinates(coordinates)
    var nextCoor = accessiblePoints(0)
    var distFromFinish = Coordinate.distanceBetween(coordinates, MainGame.finishCoordinates)

    for (pt <- accessiblePoints) {
      val ptDistFromFinish = Coordinate.distanceBetween(pt, MainGame.finishCoordinates)
      var containsTower = Tower.containsTower(pt)
      println(containsTower)
      if (ptDistFromFinish <= distFromFinish && !containsTower) {
        nextCoor = pt
        distFromFinish = ptDistFromFinish
      }
    }
    nextCoor
  }

"coordinates" is the variable that stores the coordinates for the given creep (this code snippet is a method in the Creep class).
The problem I am running into is collision detection. The creep walk right through my towers. (Here is the Tower.containsTower method:)
def containsTower(coor:(Double,Double)):Boolean = {
  var flag = false
  for (t <- MainGame.towers) {
    if (t.tower.contains(coor._1,coor._2)) flag = true
  }
  flag
}

Where t.tower is a java Rectangle2D object. This method returns true when the creep walk through it, and yet I'm not getting the desired effect of effectively "blocking off" any coordinate that contains a tower (Rectangle2D) object. 
Can anyone shed some light as to why I'm getting this behavior?

A: Your default case is to go along x when there's no better option than staying where you are.  You'll run right into a tower with your default.  You should change the default to something else, or use something like
getAccessibleCoordinates.filterNot( containsTower ).minBy{ pt =>
  Coordinate.distanceBetween(pt, MainGame.finishCoordinates)
}

assuming that there will always be at least one accessible coordinate.
By the way, pathing is not in general an easy problem, and the solution above will tend to leave you with oscillating solutions where a blocked creep will run back and forth between the two closest spots.  But that's probably better than just running into a tower when any other move would take you farther away than you currently are.
