Class Coordinates

java.lang.Object
it.polimi.ingsw.am13.model.card.Coordinates
All Implemented Interfaces:
Serializable

public class Coordinates extends Object implements Serializable
Represents a 2-tuple of coordinates for a 2D point or vector that could be used in game. For how game works, coordinates (x, y) are valid only if (x+y)%2=0 (their sum must be a multiple of 2). The object, once instantiated, is immutable.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final int
    Coordinate x of point/vector (horizontal axis)
    private final int
    Coordinate y of point/vector (vertical axis)
  • Constructor Summary

    Constructors
    Constructor
    Description
    Coordinates(int posX, int posY)
    Creates an immutable valid object Coordinates.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(int posX, int posY)
    Performs arithmetic sum of coordinates
    Performs arithmetic sum of coordinates
    boolean
     
    Creates the ordered list of 4 coordinates that are near this coordinate in game field.
    int
     
    int
     
    boolean
    hasCoords(int posX, int posY)
    Checks equality with indicated coordinates
    int
     
    Create new coordinates for origin, without throwing exceptions
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • posX

      private final int posX
      Coordinate x of point/vector (horizontal axis)
    • posY

      private final int posY
      Coordinate y of point/vector (vertical axis)
  • Constructor Details

    • Coordinates

      public Coordinates(int posX, int posY) throws InvalidCoordinatesException
      Creates an immutable valid object Coordinates. The sum of parameters must be a multiple of 2
      Parameters:
      posX - Coordinate x of point/vector (horizontal ax)
      posY - Coordinate y of point/vector (vertical axis)
      Throws:
      InvalidCoordinatesException - If posX+posY is not a multiple of 2 (game coordinates are not valid)
  • Method Details

    • origin

      public static Coordinates origin()
      Create new coordinates for origin, without throwing exceptions
      Returns:
      Coordinates for (0,0)
    • getPosX

      public int getPosX()
      Returns:
      Coordinate x of point/vector (horizontal axis)
    • getPosY

      public int getPosY()
      Returns:
      Coordinate y of point/vector (vertical axis)
    • add

      public Coordinates add(Coordinates other)
      Performs arithmetic sum of coordinates
      Parameters:
      other - Other coordinates to be summed
      Returns:
      New object Coordinates with result of sum
    • add

      public Coordinates add(int posX, int posY) throws InvalidCoordinatesException
      Performs arithmetic sum of coordinates
      Parameters:
      posX - Other x coordinate to be summed
      posY - Other y coordinate to be summed
      Returns:
      New object Coordinates with result of sum
      Throws:
      InvalidCoordinatesException - If, performing the sum, the new coordinates are not valid
    • fetchNearCoordinates

      public List<Coordinates> fetchNearCoordinates()
      Creates the ordered list of 4 coordinates that are near this coordinate in game field. The 'near coordinates' are the ones where a new card could be placed with respect to a card in these coordinates.
      Returns:
      List of the 4 near coordinates, in order 'upper left', 'upper right', 'lower right', 'lower left'
    • hasCoords

      public boolean hasCoords(int posX, int posY)
      Checks equality with indicated coordinates
      Parameters:
      posX - x coordinate to be checked
      posY - y coordinate to be checked
      Returns:
      true if these coordinates are equal to the respective coordinates passed as parameter
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object