coord: Changed to record type
This commit is contained in:
parent
3c343d002a
commit
b4e3afb867
@ -1,62 +1,46 @@
|
||||
package comp1110.ass2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Object to store coordinates
|
||||
* This stores the x and y coordinates of a point
|
||||
*/
|
||||
public class Coord {
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
public record Coord(int y, int x) {
|
||||
/**
|
||||
* Constructor for the Coord object
|
||||
*
|
||||
* @param x x coordinate
|
||||
* @param y y coordinate
|
||||
*/
|
||||
public Coord(int y, int x) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
public Coord {
|
||||
}
|
||||
|
||||
// region Getters and Setters
|
||||
|
||||
/**
|
||||
* Get the x coordinate
|
||||
*
|
||||
* @return int x coordinate
|
||||
*/
|
||||
public int getX() {
|
||||
@Override
|
||||
public int x() {
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the y coordinate
|
||||
*
|
||||
* @return int y coordinate
|
||||
*/
|
||||
public int getY() {
|
||||
@Override
|
||||
public int y() {
|
||||
return y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the x coordinate
|
||||
* @param x int x coordinate
|
||||
*/
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the y coordinate
|
||||
* @param y int y coordinate
|
||||
*/
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
/**
|
||||
* Check if two coordinates are equal
|
||||
*
|
||||
* @param coord Coord object to compare to
|
||||
* @return boolean true if equal, false otherwise
|
||||
*/
|
||||
@ -66,6 +50,7 @@ public class Coord {
|
||||
|
||||
/**
|
||||
* Check if two coordinates are adjacent (does not include diagonals)
|
||||
*
|
||||
* @param coord Coord object to compare to
|
||||
*/
|
||||
public boolean isAdjacent(Coord coord) {
|
||||
@ -80,18 +65,54 @@ public class Coord {
|
||||
|
||||
/**
|
||||
* Check if two coordinates are adjacent (includes diagonals)
|
||||
*
|
||||
* @param coord Coord object to compare to
|
||||
*/
|
||||
public boolean isAdjacentDiagonal(Coord coord){
|
||||
public boolean isAdjacentDiagonal(Coord coord) {
|
||||
if (isAdjacent(coord)) return true;
|
||||
if (this.x == coord.x - 1 && this.y == coord.y - 1) return true;
|
||||
if (this.x == coord.x - 1 && this.y == coord.y + 1) return true;
|
||||
if (this.x == coord.x + 1 && this.y == coord.y - 1) return true;
|
||||
return (this.x == coord.x + 1 && this.y == coord.y + 1);
|
||||
return (this.x == coord.x + 1 && this.y == coord.y + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param player Player to check owned by
|
||||
* @return ArrayList of adjacent coordinates
|
||||
*/
|
||||
public Coord[] getClaimedAdjacent(Player player) {
|
||||
Coord[] adjacent = new Coord[8];
|
||||
adjacent[0] = new Coord(this.y, this.x + 1);
|
||||
adjacent[1] = new Coord(this.y, this.x - 1);
|
||||
adjacent[2] = new Coord(this.y + 1, this.x);
|
||||
adjacent[3] = new Coord(this.y - 1, this.x);
|
||||
adjacent[4] = new Coord(this.y + 1, this.x + 1);
|
||||
adjacent[5] = new Coord(this.y + 1, this.x - 1);
|
||||
adjacent[6] = new Coord(this.y - 1, this.x + 1);
|
||||
adjacent[7] = new Coord(this.y - 1, this.x - 1);
|
||||
|
||||
Coord[] adjacentOwned = new Coord[8];
|
||||
int ownedCount = 0;
|
||||
|
||||
for (Coord c : player.getPieces()) {
|
||||
for (int i = 0; i < adjacent.length; i++) {
|
||||
if (c.equals(adjacent[i])) {
|
||||
adjacentOwned[i] = c;
|
||||
ownedCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
Coord[] owned = new Coord[ownedCount];
|
||||
for (int i = 0; i < ownedCount; i++) {
|
||||
owned[i] = adjacentOwned[i];
|
||||
}
|
||||
return adjacentOwned;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a string representation of the coordinate (y,x)
|
||||
*
|
||||
* @return String representation of the coordinate
|
||||
*/
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user