diff --git a/src/main/java/evironment/antGame/AntState.java b/src/main/java/evironment/antGame/AntState.java index ee8d347..368271b 100644 --- a/src/main/java/evironment/antGame/AntState.java +++ b/src/main/java/evironment/antGame/AntState.java @@ -65,22 +65,22 @@ public class AntState implements State, Visualizable { @Override public boolean equals(Object obj){ - if(obj instanceof AntState){ - AntState toCompare = (AntState) obj; - if(!this.pos.equals(toCompare.pos) || !this.hasFood == toCompare.hasFood){ - return false; - } - for (int i = 0; i < toCompare.knownWorld.length; i++) { - for (int j = 0; j < toCompare.knownWorld[i].length; j++) { - if(!this.knownWorld[i][j].equals(toCompare.knownWorld[i][j])){ - return false; - } + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; - } - } - return true; + AntState toCompare = (AntState) obj; + if(!this.pos.equals(toCompare.pos) || !this.hasFood == toCompare.hasFood){ + return false; } - return super.equals(obj); + for (int i = 0; i < toCompare.knownWorld.length; i++) { + for (int j = 0; j < toCompare.knownWorld[i].length; j++) { + if(!this.knownWorld[i][j].equals(toCompare.knownWorld[i][j])){ + return false; + } + + } + } + return true; } @Override diff --git a/src/main/java/evironment/jumpingDino/DinoState.java b/src/main/java/evironment/jumpingDino/DinoState.java index 0f59dc1..699d99e 100644 --- a/src/main/java/evironment/jumpingDino/DinoState.java +++ b/src/main/java/evironment/jumpingDino/DinoState.java @@ -5,6 +5,7 @@ import lombok.AllArgsConstructor; import lombok.Getter; import java.io.Serializable; +import java.util.Objects; @AllArgsConstructor @Getter @@ -13,20 +14,21 @@ public class DinoState implements State, Serializable { @Override public String toString() { - return Integer.toString(xDistanceToObstacle); + return "DinoState{" + + "xDistanceToObstacle=" + xDistanceToObstacle + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + DinoState dinoState = (DinoState) o; + return xDistanceToObstacle == dinoState.xDistanceToObstacle; } @Override public int hashCode() { - return this.xDistanceToObstacle; - } - - @Override - public boolean equals(Object obj) { - if(obj instanceof DinoState){ - DinoState toCompare = (DinoState) obj; - return toCompare.getXDistanceToObstacle() == this.xDistanceToObstacle; - } - return super.equals(obj); + return Objects.hash(xDistanceToObstacle); } } diff --git a/src/main/java/evironment/jumpingDino/DinoStateWithSpeed.java b/src/main/java/evironment/jumpingDino/DinoStateWithSpeed.java new file mode 100644 index 0000000..debe679 --- /dev/null +++ b/src/main/java/evironment/jumpingDino/DinoStateWithSpeed.java @@ -0,0 +1,36 @@ +package evironment.jumpingDino; + +import lombok.Getter; + +import java.util.Objects; + +@Getter +public class DinoStateWithSpeed extends DinoState{ + private int obstacleSpeed; + + public DinoStateWithSpeed(int xDistanceToObstacle, int obstacleSpeed) { + super(xDistanceToObstacle); + this.obstacleSpeed = obstacleSpeed; + } + + @Override + public String toString() { + return "DinoStateWithSpeed{" + + "obstacleSpeed=" + obstacleSpeed + + "} " + super.toString(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof DinoStateWithSpeed)) return false; + if (!super.equals(o)) return false; + DinoStateWithSpeed that = (DinoStateWithSpeed) o; + return getObstacleSpeed() == that.getObstacleSpeed(); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), getObstacleSpeed()); + } +}