Class Coord

  • Direct Known Subclasses:
    GameElement

    public class Coord
    extends Object
    An x, y coordinate pair.

    Coord also serves as the base class for all game elements, which means that each game element has all of Coord's methods (inheritance) and can be used anywhere a Coord is called for (polymorphism).

    Note that in the game's coordinates,right (E) and down (S) are positive.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int x  
      protected int y  
    • Constructor Summary

      Constructors 
      Constructor Description
      Coord​(int x, int y)  
      Coord​(Coord from)  
    • Field Detail

      • x

        protected int x
      • y

        protected int y
    • Constructor Detail

      • Coord

        public Coord​(int x,
                     int y)
      • Coord

        public Coord​(Coord from)
    • Method Detail

      • getX

        public int getX()
        X coordinate: right (E) is positive.
      • getY

        public int getY()
        Y coordinate: down (S) is positive.
      • isAt

        public boolean isAt​(int x,
                            int y)
        Is this coordinate at the given x and y?
      • isAt

        public boolean isAt​(Coord other)
        Is this coordinate at the same location as another coordinate?
      • rightAngleDistanceFrom

        public int rightAngleDistanceFrom​(int x,
                                          int y)
        Get the distance from this coordinate to another coordinate making only cardinal direction moves (no diagonals). This is useful since the game only allows cardinal direction moves.
      • rightAngleDistanceFrom

        public int rightAngleDistanceFrom​(Coord other)
        Get the distance from this coordinate to another coordinate making only cardinal direction moves (no diagonals). This is useful since the game only allows cardinal direction moves.
      • diagonalDistanceFrom

        public int diagonalDistanceFrom​(int x,
                                        int y)
        Get the distance from this coordinate to another coordinate allowing cardinal and diagonal moves. Game elements cannot actually move diagonally, but this can still be a useful measure.
      • diagonalDistanceFrom

        public int diagonalDistanceFrom​(Coord other)
        Get the distance from this coordinate to another coordinate allowing cardinal and diagonal moves. Game elements cannot actually move diagonally, but this can still be a useful measure.
      • directionTo

        public Direction directionTo​(Coord other)
        Determine the direction most aimed toward another coordinate. In case of a tie, the x-axis coordinate is chosen. If the other coordinate is the same as this, NONE is returned.
      • altDirectionTo

        public Direction altDirectionTo​(Coord other)
        Determine the direction most aimed toward another coordinate. In case of a tie, the y-axis coordinate is chosen. If the other coordinate is the same as this, NONE is returned.
      • randCoordPart

        public static int randCoordPart()
        Generate a random coordinate on a single axis within the bounds of the map.
      • randCoord

        public static Coord randCoord()
        Generate a random coordinate within the bounds of the map.